Hi All: I have some code I pieced together from various examples online 
to create a single PDF from a 6-page template file that gets customized 
iteratively. The template PDF is about 500K, and I'm trying to use 
pdfSmartCopy to keep the overall size of the final PDF down to a 
manageable level.

The code is called from an ASP.NET page, and writes the final PDF to a 
directory. I'm testing in VS2010 and IISExpress on Windows 7.

I've been testing my code, and find that I can generate a PDF with 300 
records (1800 pages) without a problem, and the final PDF is about 25M 
in size.

But if I try to create a document with more records, eventually I 
receive an error after about 400 records:

iTextSharp.text.exceptions.InvalidPdfException was unhandled by user code
   Message=trailer not found.
   Source=itextsharp
   StackTrace:
        at iTextSharp.text.pdf.PdfReader.RebuildXref()
        at iTextSharp.text.pdf.PdfReader.ReadPdf()
        at iTextSharp.text.pdf.PdfReader..ctor(String filename, Byte[] 
ownerPassword)
        at iTextSharp.text.pdf.PdfReader..ctor(String filename)
        at Project.pdfGen.makeBulkTempIDkit(String filename, DateTime 
tmpDate, Int32 employerID, String fOutput, Guid uID, Int32 idType) in 
C:\project\pdfGen.vb:line 91
        at Project.tempID.genIDs_Click(Object sender, EventArgs e) in 
C:\project\admin\tempID.aspx.vb:line 241
        at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
        at System.Web.UI.WebControls.Button.RaisePostBackEvent(String 
eventArgument)
        at 
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
 
eventArgument)
        at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler 
sourceControl, String eventArgument)
        at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection 
postData)
        at System.Web.UI.Page.ProcessRequestMain(Boolean 
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   InnerException:


I'm not sure if I'm properly closing everything or if I'm causing memory 
issues with my code...thanks in advance for any help.

     Public Shared Function makeBulkTempIDkit(filename As String, 
fOutput As String) As Boolean

             Dim doc As New Document()
             Dim copy As New PdfSmartCopy(doc, New FileStream(fOutput, 
FileMode.Create))
             doc.Open()

             Dim ms As MemoryStream = Nothing
             Dim stamper As PdfStamper = Nothing
             Dim reader As PdfReader = Nothing

             Dim name As String = String.Empty
             Dim address As String = String.Empty
             Dim address2 As String = String.Empty

             For i As Integer = 0 To dt.Rows.Count - 1
                 ms = New MemoryStream()
                 reader = New PdfReader(filename)
                 stamper = New PdfStamper(reader, ms)

                 name = dt.Rows(i)("name")
                 address = dt.Rows(i)("address")
                 address2 = dt.Rows(i)("city") + ", " + 
dt.Rows(i)("state") + " " + dt.Rows(i)("zip")

                 Dim canvas As PdfContentByte = stamper.GetOverContent(1)
                 ColumnText.ShowTextAligned(canvas, Element.ALIGN_LEFT, 
New Phrase(name), 72, 621, 0)
                 ColumnText.ShowTextAligned(canvas, Element.ALIGN_LEFT, 
New Phrase(address), 72, 607, 0)
                 ColumnText.ShowTextAligned(canvas, Element.ALIGN_LEFT, 
New Phrase(address2), 72, 593, 0)

                 canvas = stamper.GetOverContent(2)
                 ColumnText.ShowTextAligned(canvas, Element.ALIGN_LEFT, 
New Phrase(name), 212, 624, 0)
                     ColumnText.ShowTextAligned(canvas, 
Element.ALIGN_LEFT, New Phrase(dt.Rows(i)("tempID").ToString), 212, 592, 0)

                 stamper.FormFlattening = True

                 stamper.Close()
                 reader.Close()
                 reader = New PdfReader(ms.ToArray())

                 For j As Integer = 1 To 6
                     copy.AddPage(copy.GetImportedPage(reader, j))
                 Next
             Next

             doc.Close()

        End Function

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference 
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: 
http://itextpdf.com/themes/keywords.php

Reply via email to