iTextSharp Fill in a Form and merge it -- How to?

  • Follow


Hello,

I have a one page pdf form that I am using as a template for a report
I need to create.  I started looking at tools to fill in the form and
started using itextsharp.  After some digging around i came up with
this code to open the pdf, fill in the data and send it to the
requester.  By the way I am doing this in VB.net ASP.net.

                Response.ClearContent()
                Response.ClearHeaders()
                Response.ContentType = "application/pdf"
                Response.AddHeader("Content-Disposition", "attachment;
filename=CardEx.pdf")

                Dim file As String = MapPath("./form1.pdf").ToString
                Dim reader As PdfReader = New PdfReader(file)
                ' then create a PdfStamper from the created reader to
modify the form fields
                Dim outStamper As PdfStamper = New PdfStamper(reader,
Response.OutputStream)
                outStamper.AcroFields.SetField("Name", "Steven M.
Thomas")



                outStamper.Close()
                Response.End()

Now I need to fix this so i can create on document for each record I
bring back from the database.   I have been reading about merging, but
what I have found really talks about merging two static documents.
Has anyone done this?  Do you have any ideas on how to do this, could
you send me some example code if you do.

Thanks

0
Reply stmthomas (1) 7/10/2007 10:23:30 AM

On Jul 10, 11:23 am, stmtho...@gmail.com wrote:
> Hello,
>
> I have a one page pdf form that I am using as a template for a report
> I need to create.  I started looking at tools to fill in the form and
> started using itextsharp.  After some digging around i came up with
> this code to open the pdf, fill in the data and send it to the
> requester.  By the way I am doing this in VB.net ASP.net.
>
>                 Response.ClearContent()
>                 Response.ClearHeaders()
>                 Response.ContentType = "application/pdf"
>                 Response.AddHeader("Content-Disposition", "attachment;
> filename=CardEx.pdf")
>
>                 Dim file As String = MapPath("./form1.pdf").ToString
>                 Dim reader As PdfReader = New PdfReader(file)
>                 ' then create a PdfStamper from the created reader to
> modify the form fields
>                 Dim outStamper As PdfStamper = New PdfStamper(reader,
> Response.OutputStream)
>                 outStamper.AcroFields.SetField("Name", "Steven M.
> Thomas")
>
>                 outStamper.Close()
>                 Response.End()
>
> Now I need to fix this so i can create on document for each record I
> bring back from the database.   I have been reading about merging, but
> what I have found really talks about merging two static documents.
> Has anyone done this?  Do you have any ideas on how to do this, could
> you send me some example code if you do.
>
> Thanks

It's better to post this questions in the itextsharp mailing list at
https://lists.sourceforge.net/lists/listinfo/itextsharp-questions.
There's also an archive at http://news.gmane.org/gmane.comp.windows.dotnet.itextsharp.general/.

Paulo

0
Reply Paulo 7/10/2007 10:50:22 AM


On Jul 10, 6:50 am, Paulo Soares <vari...@yahoo.com> wrote:
> On Jul 10, 11:23 am, stmtho...@gmail.com wrote:
>
>
>
> > Hello,
>
> > I have a one pagepdfform that I am using as a template for a report
> > I need to create.  I started looking at tools to fill in the form and
> > started using itextsharp.  After some digging around i came up with
> > this code to open thepdf, fill in the data and send it to the
> > requester.  By the way I am doing this in VB.net ASP.net.
>
> >                 Response.ClearContent()
> >                 Response.ClearHeaders()
> >                 Response.ContentType = "application/pdf"
> >                 Response.AddHeader("Content-Disposition", "attachment;
> > filename=CardEx.pdf")
>
> >                 Dim file As String = MapPath("./form1.pdf").ToString
> >                 Dim reader As PdfReader = New PdfReader(file)
> >                 ' then create a PdfStamper from the created reader to
> > modify the form fields
> >                 Dim outStamper As PdfStamper = New PdfStamper(reader,
> > Response.OutputStream)
> >                 outStamper.AcroFields.SetField("Name", "StevenM.
> >Thomas")
>
> >                 outStamper.Close()
> >                 Response.End()
>
> > Now I need to fix this so i can create on document for each record I
> > bring back from the database.   I have been reading about merging, but
> > what I have found really talks about merging two static documents.
> > Has anyone done this?  Do you have any ideas on how to do this, could
> > you send me some example code if you do.
>
> > Thanks
>
> It's better to post this questions in the itextsharp mailing list athttps://lists.sourceforge.net/lists/listinfo/itextsharp-questions.
> There's also an archive athttp://news.gmane.org/gmane.comp.windows.dotnet.itextsharp.general/.
>
> Paulo

Thanks Paulo
I sent the message to the at group and nothing yet.  However I did
create some code that will do this.  It maynot be the best way, but it
works.

If anyone is looking for a way to do this, this is how I did it:

            Try
                Dim uniqueid As Guid
                uniqueid = Guid.NewGuid
                Dim uniqueidstr As String = uniqueid.ToString
                Response.ClearContent()
                Response.ClearHeaders()
                Response.ContentType = "application/pdf"
                Response.AddHeader("Content-Disposition", "attachment;
filename=CardEx.pdf")

                Dim cnt As Integer = 0
                ' SET THE PATH TO THE TEMPLATE FILE
                Dim file As String = MapPath("./form1.pdf").ToString
                ' SETUP THE PATH TO THE MASTER PDF FILE YOUR ARE GOING
TO CREATE
                Dim MasterPdfFile As String = MapPath("./MstrCopyFile"
+ uniqueidstr + ".pdf").ToString
                ' SETUP A NEW COPY FIELD INSTANCE TO COPY EACH PAGE
INTO THE NEW FILE
                Dim mypdfcopy As PdfCopyFields = New PdfCopyFields(New
FileStream(MasterPdfFile, FileMode.CreateNew))

                ' LOOP THROUGH THE RECORDSET AND CREATE EACH PAGE AND
THEN ADD IT TO THE MASTER FILE
                While cnt < 5
                    ' YOU NEED A TEMP READER AND FILE PATH TO THE FILE
YOU CREATE FOR EACH PAGE
                    ' DO NOT DELETE THE TEMP FILE UNTIL YOU CLOSE THE
COPYFIELDS.  THE PAGES ARE NOT REALLY ADDED
                    ' UNTIL YOU CLOSE THE COPIER
                    Dim tmpreader As PdfReader = New PdfReader(file)
                    Dim tmpfilepath As String = MapPath("./tmpFile" +
uniqueidstr + CStr(cnt) + ".pdf").ToString
                    ' CREATE TEMP FILE FOR PAGE AND STAMP IT WITH THE
DATA YOU WANT
                    Dim tmppgoutputfile As New FileStream(tmpfilepath,
FileMode.CreateNew)
                    Dim tmpoutStamper As PdfStamper = New
PdfStamper(tmpreader, tmppgoutputfile)
                    tmpoutStamper.AcroFields.SetField("Name", "Steven
M. Thomas: page " + CStr(cnt))
                    ' CLOSE THE STAMPER AND CREATE THE TEMP FILE SO
YOU CAN READ THE PAGE FROM IT.
                    tmpoutStamper.Close()
                    ' OPEN READER TO READ NEW PAGE AND ADD IT TO THE
MASTER FILE
                    Dim nwpgreader As PdfReader = New
PdfReader(tmpfilepath)
                    Dim page As PdfTemplate =
tmpoutStamper.GetImportedPage(nwpgreader, 1)

                    mypdfcopy.AddDocument(nwpgreader)
                    cnt += 1
                End While
                'CLOSE THE MASTER FILE AND DELETE THE TEMP FILES
                mypdfcopy.Close()
                cnt = 0
                While cnt < 5
                    Dim tmpfilepath As String = MapPath("./tmpFile" +
uniqueidstr + CStr(cnt) + ".pdf").ToString
                    System.IO.File.Delete(tmpfilepath)
                    cnt += 1
                End While

                ' CREATE A READER NOW TO READ THE MASTER FILE INTO THE
RESPONSE STREAM
                Dim respreader As PdfReader = New
PdfReader(MasterPdfFile)
                Dim respOutStamper As PdfStamper = New
PdfStamper(respreader, Response.OutputStream)
                respOutStamper.Close()
                Response.End()
                System.IO.File.Delete(MasterPdfFile)
            Catch ex As Exception

0
Reply stmthomas 7/14/2007 12:44:15 PM

2 Replies
1361 Views

(page loaded in 0.174 seconds)

Similiar Articles:













7/20/2012 1:10:26 PM


Reply: