locked
problem with iTextSharp RRS feed

  • Question

  • User-1579656392 posted

    Hey I am using iTextSharp version 4.1.2 and here I got HeaderFooter method. It is working fine but when i tried to get the data from the repeater to the same pdf file, it gave error.

    my Code is:

    <pre>

                DataRelation dRel = new DataRelation("Item", ds.Tables["Category"].Columns["ID"], ds.Tables["Item"].Columns["ID"]);
                ds.Relations.Add(dRel);
                rptDepartmentInfo.DataSource = ds;
                rptDepartmentInfo.DataBind();

                HttpContext.Current.Response.Clear(); //clear anything in io buffer
                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=example.pdf_" + DateTime.Now.ToString("MM-dd-yyyy") + "Time" + DateTime.Now.ToString("h-mmtt"));
                HttpContext.Current.Response.Charset = "";
                HttpContext.Current.Response.ContentType = "application/pdf";
                StringWriter stringwrite = new StringWriter();
                HtmlTextWriter htmlwrite = new HtmlTextWriter(stringwrite);
                StringWriter sw = new StringWriter();
                HtmlTextWriter hw = new HtmlTextWriter(sw);
                rptDepartmentInfo.RenderControl(hw);
                StringReader sr = new StringReader(sw.ToString());
                Document pdfDoc = new Document(PageSize.A4);
                PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
               
                Phrase phr = new Phrase("Welcome");
                HeaderFooter header = new HeaderFooter(phr, false);
                pdfDoc.Header = header;
               
                HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
               
                pdfDoc.Open();

                htmlparser.Parse(sr);
              
                pdfDoc.Close();
    </pre>

    i got error when i used htmlparser.Parse(sr).

    Error was:
    <pre>
    Server Error in '/ICS' Application.
    Font size too small: 0
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.ArgumentException: Font size too small: 0

    Source Error:

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

    Stack Trace:

    [ArgumentException: Font size too small: 0]
       iTextSharp.text.pdf.PdfContentByte.SetFontAndSize(BaseFont bf, Single size) +279
       iTextSharp.text.pdf.PdfDocument.WriteLineToContent(PdfLine line, PdfContentByte text, PdfContentByte graphics, Object[] currentValues, Single ratio) +7581
       iTextSharp.text.pdf.ColumnText.Go(Boolean simulate) +957
       iTextSharp.text.pdf.ColumnText.GoComposite(Boolean simulate) +695
       iTextSharp.text.pdf.ColumnText.Go(Boolean simulate) +53
       iTextSharp.text.pdf.PdfPRow.WriteCells(Int32 colStart, Int32 colEnd, Single xPos, Single yPos, PdfContentByte[] canvases) +3245
       iTextSharp.text.pdf.PdfPTable.WriteSelectedRows(Int32 colStart, Int32 colEnd, Int32 rowStart, Int32 rowEnd, Single xPos, Single yPos, PdfContentByte[] canvases) +230
       iTextSharp.text.pdf.PdfPTable.WriteSelectedRows(Int32 colStart, Int32 colEnd, Int32 rowStart, Int32 rowEnd, Single xPos, Single yPos, PdfContentByte canvas) +258
       iTextSharp.text.pdf.ColumnText.GoComposite(Boolean simulate) +4426
       iTextSharp.text.pdf.ColumnText.Go(Boolean simulate) +53
       iTextSharp.text.pdf.PdfDocument.AddPTable(PdfPTable ptable) +360
       iTextSharp.text.pdf.PdfDocument.Add(IElement element) +4445
       iTextSharp.text.Document.Add(IElement element) +132
       iTextSharp.text.html.simpleparser.HTMLWorker.EndElement(String tag) +2108
       iTextSharp.text.xml.simpleparser.SimpleXMLParser.Go(TextReader reader) +1108
       iTextSharp.text.xml.simpleparser.SimpleXMLParser.Parse(ISimpleXMLDocHandler doc, ISimpleXMLDocHandlerComment comment, TextReader r, Boolean html) +48
       iTextSharp.text.html.simpleparser.HTMLWorker.Parse(TextReader reader) +10
       category.Default_View.filetesting() in C:\Documents and Settings\smanandhar\My Documents\Visual Studio 2008\Projects\category\Default_View.ascx.cs:1631
       category.Default_View.lnkDataPdf_Click(Object sender, EventArgs e) in C:\Documents and Settings\smanandhar\My Documents\Visual Studio 2008\Projects\category\Default_View.ascx.cs:1866
       System.Web.UI.WebControls.LinkButton.OnClick(EventArgs e) +111
       System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +79
       System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +175
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
    </pre>

    But When I used iTextSharp version 5.0.2.0, it is running fine but the problem was i do not have HeaderFooter method there. In version 4.1.2, i can have HeaderFooter but it is not taking htmlparser though it has that method defined.

    Please help!! Its urgent.

    Wednesday, September 1, 2010 10:38 AM

Answers

  • User1520641890 posted

    Hey how to get the page number in each page??

    in the private nested class _events in prior code sample:

    [1] create a private variable to increment the page number, say '_pageNo'

    [2] create a public property to access that variable, say 'PageNo

    [3] then in OnEndPage() add this:

    string.Format("Page {0}", ++PageNo);

    the PdfPageEventHelper class in just like any ASP.NET event handler. so OnEndPage() gets called every time a iTextSharp creates a new page.


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 2, 2010 6:55 PM

All replies

  • User1520641890 posted

    Hey I am using iTextSharp version 4.1.2 and here I got HeaderFooter method. It is working fine but when i tried to get the data from the repeater to the same pdf file, it gave error.

    ......

    But When I used iTextSharp version 5.0.2.0, it is running fine but the problem was i do not have HeaderFooter method there. In version 4.1.2, i can have HeaderFooter but it is not taking htmlparser though it has that method defined.

    Please help!! Its urgent.

    that's why you should use the latest version. adding a page header is not so hard, you just need to implement page events. here is a simple example using a HTTP handler:

    <%@ WebHandler Language='C#' Class='testHeader' %>  
    using System;  
    using System.Collections.Generic;  
    using System.IO;  
    using System.Web;  
    using iTextSharp.text;  
    using iTextSharp.text.html.simpleparser;  
    using iTextSharp.text.pdf;  
     
    public class testHeader : IHttpHandler {  
      public void ProcessRequest (HttpContext context) {  
        context.Response.ContentType = "application/pdf";  
        Document document = new Document();  
        PdfWriter pw = PdfWriter.GetInstance(
          document, context.Response.OutputStream
        );  
        document.Open(); 
        _events e = new _events();
        e.HeaderText = "My Header Text";
        pw.PageEvent = e;
        for (int i = 0; i < 5;) {
          document.Add(new Paragraph(string.Format("PAGE {0} CONTENT",  ++i)));
          document.NewPage();
        }
        document.Close();
      }  
      public bool IsReusable { 
        get { return false; } 
      }
    /*
     * implement PDF page header
     */
      private class _events : PdfPageEventHelper {
        private string _headerText;
        public string HeaderText {
          get { return _headerText; }
          set { _headerText = value; }
        }
        public override void OnEndPage(PdfWriter writer, Document document) {
          Rectangle page = document.PageSize;
          ColumnText.ShowTextAligned(
            writer.DirectContent, Element.ALIGN_CENTER, 
            new Phrase(HeaderText),
    /* x write coordinate => center */
            (page.Left + page.Right) / 2,
    /* y write coordinate => top */
            page.Height - document.TopMargin,
            0
          );
        }
      }
    }
    if you have urgent requests like these you should think about investing in the book. or subscribe to the mailing list.

    Thursday, September 2, 2010 12:58 AM
  • User-1579656392 posted

    I almost finish the iText in Action book but its in java.. i searched all the articles i could.. but didn't know about the mailing list. Thank you so very much. Really appreciate your help. Hey how to get the page number in each page??

    Thursday, September 2, 2010 9:24 AM
  • User1520641890 posted

    Hey how to get the page number in each page??

    in the private nested class _events in prior code sample:

    [1] create a private variable to increment the page number, say '_pageNo'

    [2] create a public property to access that variable, say 'PageNo

    [3] then in OnEndPage() add this:

    string.Format("Page {0}", ++PageNo);

    the PdfPageEventHelper class in just like any ASP.NET event handler. so OnEndPage() gets called every time a iTextSharp creates a new page.


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 2, 2010 6:55 PM
  • User-1579656392 posted

    Thank you so much. That worked. I really appreciate your help.

    Thursday, September 2, 2010 8:20 PM