i'm trying to export my website as pdf and into chapters from navigation
currently using itextsharp
the problem is this
Dim Row As Data.DataRow = Nothing
Dim section_search As Integer = Nothing
Dim page_name As String = Nothing
Dim file_name As String = Nothing
Dim FK_section_mapping As Integer = Nothing
Dim hide_navigation As Boolean = Nothing
Dim publishing_control As Integer = Nothing
Dim page_content As String = Nothing
Dim sectionFont As Font = FontFactory.GetFont(FontFactory.HELVETICA, 16, Font.NORMAL, New Color(0, 0, 255))
Dim subsectionFont As Font = FontFactory.GetFont(FontFactory.HELVETICA, 12, Font.BOLD, New Color(0, 64, 64))
For Each Row In pagesDS.Tables(0).Rows
section_search = Row.Item("FK_section_mapping")
If KEY_section_mapping = section_search Then
page_name = Row.Item("page_name")
file_name = Row.Item("file_name")
hide_navigation = Row.Item("hide_navigation")
publishing_control = Row.Item("publishing_control")
page_content = Row.Item("page_content")
Dim sub_chapter As New Paragraph(page_name, sectionFont)
Dim sub_chapter_section As Section = current_chapter.AddSection(sub_chapter, 2)
''this is the content for this section
Dim sub_chapter_content As New Paragraph(Server.HtmlDecode(page_content), subsectionFont)
sub_chapter_section.Add(sub_chapter_content)
End If
Next
line 31. page_content has the html source code (which i want it to render it as html and display it on the pdf how a web brower would render it)
Which version of Visual Studio are you using? 2005 or 2008?
Click "Mark as Answer" on the post that helped you.
This earns you a point and marks your thread as Resolved so we will all know you have been helped.
FAQ on the correct forum http://forums.asp.net/p/1337412/2699239.aspx#2699239
You will need to post the whole of the class. (Preferably without the line numbers)
Click "Mark as Answer" on the post that helped you.
This earns you a point and marks your thread as Resolved so we will all know you have been helped.
FAQ on the correct forum http://forums.asp.net/p/1337412/2699239.aspx#2699239
You will need to post the whole of the class. (Preferably without the line numbers)
here is the entire code behind
Imports MySql.Data.MySqlClient
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.text.html
Partial Class pdf_icecore
Inherits System.Web.UI.Page
Dim sectionDS As New Data.DataSet
Dim pagesDS As New Data.DataSet
Protected Sub btn_pdf_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_pdf.Click
Session("current_site") = 8
Session("key1") = "server=localhost...etc.."
build_pdf_site()
End Sub
Sub build_pdf_site()
get_section_data()
get_page_data()
Dim PK_section_mapping As Integer = Nothing
Dim site_display_name As String = Nothing
Dim hyperlink_text_value As String = Nothing
Dim Row As Data.DataRow = Nothing
Dim template_search As Integer = Nothing
'' create our response so it displays the pdf
Response.Clear()
'' step 1: creation of a document-object and set margins
Dim document As New Document(PageSize.A4, 50, 50, 50, 50)
'' meta data for the pdf
document.AddTitle(site_display_name)
document.AddSubject("This example explains step 6 in Chapter 1")
document.AddKeywords("Metadata, iText, step 6, tutorial")
document.AddCreator("My program using iText#")
document.AddAuthor("Bruno Lowagie")
document.AddHeader("Expires", "0")
'' step 2: we create a writer that listens to the document
Dim writer As PdfWriter = PdfWriter.GetInstance(document, Response.OutputStream)
writer.SetPdfVersion(PdfWriter.PDF_VERSION_1_7)
'' step 3: we open the document
document.Open()
Dim chapterFont As Font = FontFactory.GetFont(FontFactory.HELVETICA, 24, Font.NORMAL, New Color(255, 0, 0))
Dim i As Integer = 1
For Each Row In sectionDS.Tables(0).Rows
PK_section_mapping = Row.Item("PK_section_mapping")
site_display_name = Row.Item("site_display_name")
hyperlink_text_value = Row.Item("hyperlink_text_value")
'' step 4: we add content to the document
'''''''''' create the main chapter
Dim title_chapter As New Paragraph(hyperlink_text_value, chapterFont)
Dim chapter As New Chapter(title_chapter, i)
chapter.NumberDepth = 0
Dim title_chapter_sub_text As New Paragraph("text under chapter heading")
chapter.Add(title_chapter_sub_text)
generate_sub_chapter(PK_section_mapping, chapter)
''''''''''put all the sub chapter(s) generated as a new chapater
document.Add(chapter)
i = i + 1
Next
'' step 5: we close the document
document.Close()
'''''''''' output the file for users to download
Response.ContentType = "application/pdf"
Response.AddHeader("Content-Disposition", "attachment; filename=""" & site_display_name & ".pdf""")
Response.End()
End Sub
Sub generate_sub_chapter(ByVal KEY_section_mapping As Integer, ByVal current_chapter As Chapter)
Dim Row As Data.DataRow = Nothing
Dim section_search As Integer = Nothing
Dim page_name As String = Nothing
Dim FK_section_mapping As Integer = Nothing
Dim page_content As String = Nothing
Dim sectionFont As Font = FontFactory.GetFont(FontFactory.HELVETICA, 16, Font.NORMAL, New Color(0, 0, 255))
Dim subsectionFont As Font = FontFactory.GetFont(FontFactory.HELVETICA, 12, Font.BOLD, New Color(0, 64, 64))
For Each Row In pagesDS.Tables(0).Rows
section_search = Row.Item("FK_section_mapping")
If KEY_section_mapping = section_search Then
page_name = Row.Item("page_name")
page_content = Row.Item("page_content") '<----this is all html markup
''''''''''sub chapters name
Dim sub_chapter As New Paragraph(page_name, sectionFont)
Dim sub_chapter_section As Section = current_chapter.AddSection(sub_chapter, 2)
''''''''''this is the content for this section'''''' want to render the html here and show in it the section
Dim sub_chapter_content As New Paragraph(Server.HtmlDecode(page_content), subsectionFont)
sub_chapter_section.Add(sub_chapter_content)
End If
Next
End Sub
Sub get_section_data()
Dim connectionString As String = Session("key1")
Dim connectionObject As New MySql.Data.MySqlClient.MySqlConnection(connectionString)
Dim adapter As New MySqlDataAdapter()
Dim commandText As String = "prc_pdf_sections"
Dim command As New MySqlCommand(commandText, connectionObject)
Dim parameter As MySqlParameter = Nothing
command.CommandType = System.Data.CommandType.StoredProcedure
parameter = New MySqlParameter
parameter.Direction = System.Data.ParameterDirection.Input
command.Parameters.AddWithValue("?PK_sites_COL", Session("current_site"))
adapter.SelectCommand = command
adapter.Fill(sectionDS, "itemTable")
adapter.SelectCommand.Connection.Close()
End Sub
Sub get_page_data()
Dim connectionString As String = Session("key1")
Dim connectionObject As New MySql.Data.MySqlClient.MySqlConnection(connectionString)
Dim adapter As New MySqlDataAdapter()
Dim commandText As String = "prc_pdf_pages"
Dim command As New MySqlCommand(commandText, connectionObject)
Dim parameter As MySqlParameter = Nothing
command.CommandType = System.Data.CommandType.StoredProcedure
parameter = New MySqlParameter
parameter.Direction = System.Data.ParameterDirection.Input
command.Parameters.AddWithValue("?FK_sites_COL", Session("current_site"))
adapter.SelectCommand = command
adapter.Fill(pagesDS, "itemTable")
adapter.SelectCommand.Connection.Close()
End Sub
End Class
I don't use iTextSharp. However, using iText (java library), I've been unable to take HTML, render using HTMLWorker and put into a paragraph with the intended HTML format. However, I have been able to do this and put into the document.
NOTE -- you'll want to make certain that your HTML is well-formed. If it's missing " around the attributes for a tag, it won't be rendered (ex: <h1 height=10> vs. <h1 height="10"/>)
:: Putting to a paragraph won't work
PdfPTable table = new TableData(2,100f).getTable();
float[] widths = {50f,50f};
table.setWidths(widths);
table.addCell("list of values");
Paragraph para = new Paragraph();
StringReader reader = new StringReader("<ul><li>one</li><li>two</li></ul>");
ArchivePageUtil.parseHTML(para, reader);
table.addCell(para);
doc.add(table);
:: Putting directly on the document will work ::
StringReader reader = new StringReader("<ul><li>one</li><li>two</li></ul>");
ArchivePageUtil.parseHTML(doc, reader);
Here's the helper methods ArchivePageUtil
public StyleSheet defaultStyleSheet = new StyleSheet();
public static void parseHTML(Paragraph para, Reader reader) {
try {
ArrayList arrayList = HTMLWorker.parseToList(reader,ArchiveConstants.defaultStyleSheet);
//add the elements to the page
for (int k=0;k para.add((Element) arrayList.get(k));
}
} catch (IOException ioe) {
//TODO: log the exception and give back some ApplicationException
Logger.exception(ArchivePageUtil.class, ioe, "parseHTML");
}
}
public static void parseHTML(Document doc, Reader reader) {
try {
ArrayList arrayList = HTMLWorker.parseToList(reader, ArchiveConstants.defaultStyleSheet);
//add the elements to the page
for (int k=0;k doc.add((Element) arrayList.get(k));
}
} catch (IOException ioe) {
Logger.exception(ArchivePageUtil.class, ioe, "parseHTML");
} catch (DocumentException de) {
Logger.exception(ArchivePageUtil.class, de, "parseHTML");
}
}
:: There's also an example in the iText book where you can put the HTML in a column object instead of a PdfPTable
I hadn't done this (as putting to the doc was good enough), but you could write your own HTMLWorker class and utilize that. HTMLWorker doesn't appear to be "supported" anymore. But you have the source code, so you can change the way it currently works. If are
able to fix it, give back to the community.
lindows
Member
169 Points
356 Posts
itextsharp render html source and display in a paragraph
Apr 18, 2009 12:58 PM|LINK
hi experts
i'm trying to export my website as pdf and into chapters from navigation
currently using itextsharp
the problem is this
Dim Row As Data.DataRow = Nothing Dim section_search As Integer = Nothing Dim page_name As String = Nothing Dim file_name As String = Nothing Dim FK_section_mapping As Integer = Nothing Dim hide_navigation As Boolean = Nothing Dim publishing_control As Integer = Nothing Dim page_content As String = Nothing Dim sectionFont As Font = FontFactory.GetFont(FontFactory.HELVETICA, 16, Font.NORMAL, New Color(0, 0, 255)) Dim subsectionFont As Font = FontFactory.GetFont(FontFactory.HELVETICA, 12, Font.BOLD, New Color(0, 64, 64)) For Each Row In pagesDS.Tables(0).Rows section_search = Row.Item("FK_section_mapping") If KEY_section_mapping = section_search Then page_name = Row.Item("page_name") file_name = Row.Item("file_name") hide_navigation = Row.Item("hide_navigation") publishing_control = Row.Item("publishing_control") page_content = Row.Item("page_content") Dim sub_chapter As New Paragraph(page_name, sectionFont) Dim sub_chapter_section As Section = current_chapter.AddSection(sub_chapter, 2) ''this is the content for this section Dim sub_chapter_content As New Paragraph(Server.HtmlDecode(page_content), subsectionFont) sub_chapter_section.Add(sub_chapter_content) End If Nextline 31. page_content has the html source code (which i want it to render it as html and display it on the pdf how a web brower would render it)eg...
<p>Latest ssss</p>
<p><img height="133" alt="" width="200" src="/content_files/100_4292.jpg" /></p>
<p><span style="font-family: SimHei">x + y update</span></p>
the aim is that when users want to export to pdf, the formatting is retained
current, the pdf content is just being spit out as html source for my section,
anyone know how to fix this?
iTextSharp pdf converter html to pdf PDF Component for ASP.NET
TATWORTH
All-Star
72405 Points
14018 Posts
MVP
Re: itextsharp render html source and display in a section of pdf
Apr 18, 2009 06:55 PM|LINK
Which version of Visual Studio are you using? 2005 or 2008?
This earns you a point and marks your thread as Resolved so we will all know you have been helped.
FAQ on the correct forum http://forums.asp.net/p/1337412/2699239.aspx#2699239
lindows
Member
169 Points
356 Posts
Re: itextsharp render html source and display in a section of pdf
Apr 18, 2009 07:45 PM|LINK
VS 2008
TATWORTH
All-Star
72405 Points
14018 Posts
MVP
Re: itextsharp render html source and display in a section of pdf
Apr 18, 2009 08:14 PM|LINK
You will need to post the whole of the class. (Preferably without the line numbers)
This earns you a point and marks your thread as Resolved so we will all know you have been helped.
FAQ on the correct forum http://forums.asp.net/p/1337412/2699239.aspx#2699239
lindows
Member
169 Points
356 Posts
Re: itextsharp render html source and display in a section of pdf
Apr 19, 2009 04:33 AM|LINK
here is the entire code behind
Imports MySql.Data.MySqlClient Imports iTextSharp.text Imports iTextSharp.text.pdf Imports iTextSharp.text.html Partial Class pdf_icecore Inherits System.Web.UI.Page Dim sectionDS As New Data.DataSet Dim pagesDS As New Data.DataSet Protected Sub btn_pdf_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_pdf.Click Session("current_site") = 8 Session("key1") = "server=localhost...etc.." build_pdf_site() End Sub Sub build_pdf_site() get_section_data() get_page_data() Dim PK_section_mapping As Integer = Nothing Dim site_display_name As String = Nothing Dim hyperlink_text_value As String = Nothing Dim Row As Data.DataRow = Nothing Dim template_search As Integer = Nothing '' create our response so it displays the pdf Response.Clear() '' step 1: creation of a document-object and set margins Dim document As New Document(PageSize.A4, 50, 50, 50, 50) '' meta data for the pdf document.AddTitle(site_display_name) document.AddSubject("This example explains step 6 in Chapter 1") document.AddKeywords("Metadata, iText, step 6, tutorial") document.AddCreator("My program using iText#") document.AddAuthor("Bruno Lowagie") document.AddHeader("Expires", "0") '' step 2: we create a writer that listens to the document Dim writer As PdfWriter = PdfWriter.GetInstance(document, Response.OutputStream) writer.SetPdfVersion(PdfWriter.PDF_VERSION_1_7) '' step 3: we open the document document.Open() Dim chapterFont As Font = FontFactory.GetFont(FontFactory.HELVETICA, 24, Font.NORMAL, New Color(255, 0, 0)) Dim i As Integer = 1 For Each Row In sectionDS.Tables(0).Rows PK_section_mapping = Row.Item("PK_section_mapping") site_display_name = Row.Item("site_display_name") hyperlink_text_value = Row.Item("hyperlink_text_value") '' step 4: we add content to the document '''''''''' create the main chapter Dim title_chapter As New Paragraph(hyperlink_text_value, chapterFont) Dim chapter As New Chapter(title_chapter, i) chapter.NumberDepth = 0 Dim title_chapter_sub_text As New Paragraph("text under chapter heading") chapter.Add(title_chapter_sub_text) generate_sub_chapter(PK_section_mapping, chapter) ''''''''''put all the sub chapter(s) generated as a new chapater document.Add(chapter) i = i + 1 Next '' step 5: we close the document document.Close() '''''''''' output the file for users to download Response.ContentType = "application/pdf" Response.AddHeader("Content-Disposition", "attachment; filename=""" & site_display_name & ".pdf""") Response.End() End Sub Sub generate_sub_chapter(ByVal KEY_section_mapping As Integer, ByVal current_chapter As Chapter) Dim Row As Data.DataRow = Nothing Dim section_search As Integer = Nothing Dim page_name As String = Nothing Dim FK_section_mapping As Integer = Nothing Dim page_content As String = Nothing Dim sectionFont As Font = FontFactory.GetFont(FontFactory.HELVETICA, 16, Font.NORMAL, New Color(0, 0, 255)) Dim subsectionFont As Font = FontFactory.GetFont(FontFactory.HELVETICA, 12, Font.BOLD, New Color(0, 64, 64)) For Each Row In pagesDS.Tables(0).Rows section_search = Row.Item("FK_section_mapping") If KEY_section_mapping = section_search Then page_name = Row.Item("page_name") page_content = Row.Item("page_content") '<----this is all html markup ''''''''''sub chapters name Dim sub_chapter As New Paragraph(page_name, sectionFont) Dim sub_chapter_section As Section = current_chapter.AddSection(sub_chapter, 2) ''''''''''this is the content for this section'''''' want to render the html here and show in it the section Dim sub_chapter_content As New Paragraph(Server.HtmlDecode(page_content), subsectionFont) sub_chapter_section.Add(sub_chapter_content) End If Next End Sub Sub get_section_data() Dim connectionString As String = Session("key1") Dim connectionObject As New MySql.Data.MySqlClient.MySqlConnection(connectionString) Dim adapter As New MySqlDataAdapter() Dim commandText As String = "prc_pdf_sections" Dim command As New MySqlCommand(commandText, connectionObject) Dim parameter As MySqlParameter = Nothing command.CommandType = System.Data.CommandType.StoredProcedure parameter = New MySqlParameter parameter.Direction = System.Data.ParameterDirection.Input command.Parameters.AddWithValue("?PK_sites_COL", Session("current_site")) adapter.SelectCommand = command adapter.Fill(sectionDS, "itemTable") adapter.SelectCommand.Connection.Close() End Sub Sub get_page_data() Dim connectionString As String = Session("key1") Dim connectionObject As New MySql.Data.MySqlClient.MySqlConnection(connectionString) Dim adapter As New MySqlDataAdapter() Dim commandText As String = "prc_pdf_pages" Dim command As New MySqlCommand(commandText, connectionObject) Dim parameter As MySqlParameter = Nothing command.CommandType = System.Data.CommandType.StoredProcedure parameter = New MySqlParameter parameter.Direction = System.Data.ParameterDirection.Input command.Parameters.AddWithValue("?FK_sites_COL", Session("current_site")) adapter.SelectCommand = command adapter.Fill(pagesDS, "itemTable") adapter.SelectCommand.Connection.Close() End Sub End Classlindows
Member
169 Points
356 Posts
Re: itextsharp render html source and display in a section of pdf
Apr 19, 2009 03:50 PM|LINK
any ideas?
lindows
Member
169 Points
356 Posts
Re: itextsharp render html source and display in a section of pdf
Apr 23, 2009 08:03 AM|LINK
this is what the pdf looks like once generated,
http://www.speedyshare.com/578698156.html
http://www.flyupload.com/get?fid=143363661
http://www.sendspace.com/file/n4df9s
scott44017
Member
2 Points
1 Post
Re: itextsharp render html source and display in a paragraph
May 01, 2009 07:09 PM|LINK
I don't use iTextSharp. However, using iText (java library), I've been unable to take HTML, render using HTMLWorker and put into a paragraph with the intended HTML format. However, I have been able to do this and put into the document.
NOTE -- you'll want to make certain that your HTML is well-formed. If it's missing " around the attributes for a tag, it won't be rendered (ex: <h1 height=10> vs. <h1 height="10"/>)
:: Putting to a paragraph won't work
PdfPTable table = new TableData(2,100f).getTable();
float[] widths = {50f,50f};
table.setWidths(widths);
table.addCell("list of values");
Paragraph para = new Paragraph();
StringReader reader = new StringReader("<ul><li>one</li><li>two</li></ul>");
ArchivePageUtil.parseHTML(para, reader);
table.addCell(para);
doc.add(table);
:: Putting directly on the document will work ::
StringReader reader = new StringReader("<ul><li>one</li><li>two</li></ul>");
ArchivePageUtil.parseHTML(doc, reader);
Here's the helper methods ArchivePageUtil
public StyleSheet defaultStyleSheet = new StyleSheet();
public static void parseHTML(Paragraph para, Reader reader) {
try {
ArrayList arrayList = HTMLWorker.parseToList(reader,ArchiveConstants.defaultStyleSheet);
//add the elements to the page
for (int k=0;k para.add((Element) arrayList.get(k));
}
} catch (IOException ioe) {
//TODO: log the exception and give back some ApplicationException
Logger.exception(ArchivePageUtil.class, ioe, "parseHTML");
}
}
public static void parseHTML(Document doc, Reader reader) {
try {
ArrayList arrayList = HTMLWorker.parseToList(reader, ArchiveConstants.defaultStyleSheet);
//add the elements to the page
for (int k=0;k doc.add((Element) arrayList.get(k));
}
} catch (IOException ioe) {
Logger.exception(ArchivePageUtil.class, ioe, "parseHTML");
} catch (DocumentException de) {
Logger.exception(ArchivePageUtil.class, de, "parseHTML");
}
}
:: There's also an example in the iText book where you can put the HTML in a column object instead of a PdfPTable
I hadn't done this (as putting to the doc was good enough), but you could write your own HTMLWorker class and utilize that. HTMLWorker doesn't appear to be "supported" anymore. But you have the source code, so you can change the way it currently works. If are able to fix it, give back to the community.
Scott
iText PdfPTable HTMLWorker iTextSharp HTML pdf
lindows
Member
169 Points
356 Posts
Re: itextsharp render html source and display in a paragraph
May 01, 2009 09:19 PM|LINK
thanks scott,
apologies as i'm not very good with C#
would you be able to modfiy my code with your method?
lindows
Member
169 Points
356 Posts
Re: itextsharp render html source and display in a paragraph
Jul 11, 2009 12:41 PM|LINK
have found my solution with duo pdf
http://www.duodimension.com/html_pdf_asp.net/component_html_pdf.aspx