Home » Asp.netRSS

iTextSharp question about pagebreak

Hi,

how can i generate a pagebreak in the pdf-File with iTextSharp?Embarassed

I have googled some Postings
http://forums.asp.net/p/1251571/2314636.aspx
http://robrobertson.wordpress.com/2008/10/28/itextsharp-newpage-pagebreak/
http://hamang.net/tag/pdf/

But i don't know, how can i use it.

For example I created a new class

Public Class HTMLWorkerExtended
    Inherits HTMLWorker
    Public Sub New(ByVal document As IDocListener)
        MyBase.New(document)
    End Sub
    Public Overloads Overrides Sub StartElement(ByVal tag As [String], ByVal h As Hashtable)
        If tag.Equals("newpage") Then
            document.Add(Chunk.NEXTPAGE)
        Else
            MyBase.StartElement(tag, h)
        End If
    End Sub
End Class


but <newpage/> doesn't run.

This is my Scriptcode

Protected Sub GetDetails()
        Dim txt1 As String = _
        "<h3>PTSsmart | Informationen</h3>"
        Dim str As String = "_"
        Dim str1 As String = str.PadLeft(90, "_") '& "<br/>"
        Dim myTimeStamp As DateTime = DateTime.Now
        Dim myString As String = myTimeStamp.ToString("MMMM yyyy")
        Response.ContentType = "application/pdf"
        Response.AddHeader("content-disposition", "attachment;filename=PTSSmart-Training.pdf")
        Response.Cache.SetCacheability(HttpCacheability.NoCache)
        Dim sw As New StringWriter()
        Dim hw As New HtmlTextWriter(sw)
        sw.Write(txt1 & str.PadLeft(50, "_"))
        sw.Write(repTermine(Request.QueryString("id"), Request.QueryString("kursid")))
        sw.write("<newpage/>")
        sw.Write("<br/><b>Anmeldeformular: Fax-Nr. 02932 51674</b><br/>")
       ....
        Dim sr As New StringReader(sw.ToString())
        Dim pdfDoc As New Document(PageSize.A4, 28, 28, 28, 28)
        'Dim pdfDoc As New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0.0F)
        Dim htmlparser As New HTMLWorker(pdfDoc)
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
        pdfDoc.Open()
        htmlparser.Parse(sr)
        pdfDoc.Close()
        Response.Write(pdfDoc)
        Response.End()
    End Sub

Thank you in advance for helping me. Smile

 

6 Answers Found

 

Answer 1

luzie:
but <newpage/> doesn't run.

 

 

What about?

document.NewPage()

 

Answer 2

Thank you for answering.

But i don't know  how can i use this in my scriptcode.?
I have tried to set a new string- and textwriter and an new streamreader. But it doesn't run.

I get following erromessage
System.ArgumentException: Illegales Zeichen im Pfad.
Dim srx As New StreamReader(swx.ToString())...

This is my Scriptcode.

Protected Sub GetDetails()
        Dim txt1 As string  = _
        "<h3>PTSsmart | Informationen</h3>"
        Dim str  As String = "_"
        Dim str1 As String = str.PadLeft(90, "_") '& "<br/>"
        Dim myTimeStamp As DateTime = DateTime.Now
        Dim myString As String = myTimeStamp.ToString("MMMM yyyy")
        Response.ContentType = "application/pdf"
        Response.AddHeader("content-disposition", "attachment;filename=PTSSmart-Training.pdf")
        Response.Cache.SetCacheability(HttpCacheability.NoCache)
        Dim sw As New StringWriter()
        Dim hw As New HtmlTextWriter(sw)
        sw.Write(txt1 & str.PadLeft(50, "_"))
        sw.Write(repTermine(Request.QueryString("id"), Request.QueryString("kursid")))

        Dim swx As New StringWriter()
        Dim hwx As New HtmlTextWriter(swx)

        swx.Write("<br/><b>Anmeldeformular: Fax-Nr. 02932 51674</b><br/>")
       [...]
        Dim sr As New StringReader(sw.ToString())
        Dim srx As New StreamReader(swx.ToString())

        Dim pdfDoc As New Document(PageSize.A4, 28, 28, 28, 28)
        Dim htmlparser As New HTMLWorker(pdfDoc)

        PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
        pdfDoc.Open()
        htmlparser.Parse(sr)
        pdfDoc.NewPage()

        htmlparser.Parse(srx)
        pdfDoc.Close()
        Response.Write(pdfDoc)

        Response.End()
    End Sub

Can you say what is wrong?

 

Answer 3

You can also ask at the iTestSharp forum at http://www.nabble.com/iTextSharp-f4188.html

 

Answer 4

Hi


thank you for answering.

Most of the iTextSharp-Forum questions are not replied. I think it has no purpose to ask there too.

I have found a solution myself.

 Protected Sub GetDetails()
        Response.ContentType = "application/pdf"
        Response.AddHeader("content-disposition", "attachment;filename=PTSSmart-Training.pdf")
        Response.Cache.SetCacheability(HttpCacheability.NoCache)

        Dim sw As New StringWriter()
        Dim hw As New HtmlTextWriter(sw)
        sw.Write("<b>some text on page 1</b>")

        Dim sw2 As New StringWriter()
        Dim hw2 As New HtmlTextWriter(sw2)
        sw2.Write("<b>some text on page 2</b>")
        
        Dim sr2 As New StringReader(sw2.ToString())
        Dim sr As New StringReader(sw.ToString())

        Dim pdfDoc As New Document(PageSize.A4, 28, 28, 28, 28)
        Dim htmlparser As New HTMLWorker(pdfDoc)
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
        pdfDoc.Open()
        htmlparser.Parse(sr)
        pdfDoc.NewPage()
        htmlparser.Parse(sr2)       
        pdfDoc.Close()
        Response.Write(pdfDoc)
        Response.End()
    End Sub



 

Answer 5

Excellent! - Please mark your thread as answered by click at least one "Mark as Answer"

 

Answer 6

what should be the condition if I am pulling data from the repeater and want the page break in the middle of the data.. In my case, i have only one html parser. Any idea ?

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter