Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Merging PDF's
Message
From
02/04/2010 16:09:27
 
 
To
02/04/2010 15:36:04
Timothy Bryan
Sharpline Consultants
Conroe, Texas, United States
General information
Forum:
ASP.NET
Category:
Other
Title:
Environment versions
Environment:
C# 3.0
Miscellaneous
Thread ID:
01458458
Message ID:
01458557
Views:
49
Hi Tim

I inherited the PDF forms, so they were created originally to be filled out by hand in Adobe Acrobat. I have since modified some of them using Foxit Phantom. The hardest part was figuring out how to deal with the password protection they had put on some of the forms (password has to be converted to a byte array before it is passed in)
( you can also do stamps, overlays, watermarks etc. )

Here's a sample :
Imports System.Diagnostics
Imports InsurtecPW.Business
Imports System.IO
Imports iTextSharp.text.pdf
Imports iTextSharp.text
Imports System.Collections.Generic
Imports System.Data


Public NotInheritable Class PDFFormsEngine
    Public Shared Sub FillpdfForm(ByVal policy As PoliciesBO, ByVal template As String, ByVal output As String, _ < and a number of other params representing businees objects > ...  ByVal Propmaster As PropMasterBO, ByVal Terrorism As TerrorismBO)

        Dim ADDITIONALPREMTRIA As String = policy.ADDITIONALPREMTRIA.ToString()
        Dim ADDPREMAUTO As String = policy.ADDPREMAUTO.ToString()
        Dim AGENCYCODE As String = policy.AGENCYCODE
        Dim AGENTCITY As String = policy.AGENTCITY
        Dim EXPIREDATE As String = String.Format("{0:MM/dd/yy}", policy.EXPIREDATE)

' Here's the iTextsharper stuff 

         Dim pword As Byte()

        pword = Base.Utility.StrToByteArray("jellopudding")

        Dim pdfReader As New PdfReader(template, pword)

        Dim pdfStamper As New PdfStamper(pdfReader, New FileStream( _
                                                                   output, FileMode.Create))

    'These are your fields to fill out

    Dim pdfFormFields As AcroFields = pdfStamper.AcroFields

        ' set single fields 

        pdfFormFields.SetField("ADDITIONAL PREM TRIA", ADDITIONALPREMTRIA)
        pdfFormFields.SetField("ADD PREM AUTO", ADDPREMAUTO)
        pdfFormFields.SetField("AGENCY CODE", AGENCYCODE)
        pdfFormFields.SetField("AGENT CITY", AGENTCITY)
 ...
       pdfFormFields.SetField("APP DATE", APPDATE)
        pdfFormFields.SetField("BUSINESS DESCRIPTION", BUSINESSDESCRIPTION)
        pdfFormFields.SetField("BUSINESSINCOMEANDEXPENSE", 
...
' Here are examples of multilines on form filled from child BOs

        ' Set group fields
        ''-- Iterate child bos used by grids in PDFFormsEngine
        Dim rownum As Int32

        '-- InsuredName
        rownum = 1
        For Each bo As InsuredNameBO In InsuredName.GetEnumerable()
            pdfFormFields.SetField("INSURED NAME" & rownum, bo.InsuredName)
            rownum += 1
        Next

        ''-- DEC
        rownum = 1
        For Each bo As DECBO In DEC.GetEnumerable()
            pdfFormFields.SetField("COVERAGE PART" & rownum, bo.CoveragePart)
            pdfFormFields.SetField("COVERAGE PREM" & rownum, bo.CoveragePrem.ToString())
            rownum += 1
        Next

' The trick to checkboxes on the form is to just set it to "Yes" if true

        '-- FILL FORMOFBUSINESS FIELDS BASED ON ENUM

        Select Case policy.FORMOFBUSINESS
            Case InsurtecPW.Business.FormofBusiness.CORPORATION
                pdfFormFields.SetField("CORPORATION", "Yes")
            Case InsurtecPW.Business.FormofBusiness.JOINTVENTURE
                pdfFormFields.SetField("JOINT VENTURE", "Yes")
            Case InsurtecPW.Business.FormofBusiness.LLC
                pdfFormFields.SetField("LLC", "Yes")
            Case InsurtecPW.Business.FormofBusiness.ORGANIZATION
                pdfFormFields.SetField("ORGANIZATION", "Yes")
            Case InsurtecPW.Business.FormofBusiness.PARTNERSHIP
                pdfFormFields.SetField("PARTNERSHIP", "Yes")
            Case InsurtecPW.Business.FormofBusiness.PROPRIETORSHIP
                pdfFormFields.SetField("PROPRIETORSHIP", "Yes")
        End Select

' This closes the form so it is now just a regular PDF

        pdfStamper.FormFlattening = True

        pdfStamper.Close()

' see the result
        ' process.start(output)

    End Sub
End Class
>Hi Charles,
>
>Programatically filling in pdf forms is something I was about to start searching for so you saved me some effort also. I appreciate it. How easy did you figure out how to do that with this tool? What does it take to have a pdf form that accepts being filled out?
>Tim
>
>>>This client wants to merge pdf files.
>>>There database-related logic in the algorithm that determines what files from what folders should be combined.
>>>I found one open source tool that claims to be able to do it, but there is no documentation and before I dive into it, I'm wondering if anyone has any better ideas?
>>>Thanks
>>
>>If the tool is iTextsharper it definitely can do it. Google around and you'll find code on how to do it. If you get stuck let me know and I can show you my vb code. I am using iTextsharper to programmtically fill out 40 or so PDF forms and merge them into one big PDF. Not difficult at all to hook up.
>>
>>(found it faster than I thought I would. this is the routine I am using to join the PDFs. i pass in the name of the masterPDF I want to create and an array of the names of the PDFs to be merged )_
>>
>>
>>
>>
>>Imports System
>>Imports System.IO
>>Imports iTextSharp.text
>>Imports iTextSharp.text.pdf
>>Imports System.Collections.Generic
>>
>>Public NotInheritable Class PDFJoiner
>>
>>    Public Shared Sub JoinPDFs(ByVal Masterpdf As String, ByVal sourcefiles As String())
>>
>>        Dim f As Integer = 0
>>
>>        ' we create a reader for a certain document
>>        ' owner password as second param requires a byte array 
>>
>>        Dim pword As Byte()
>>
>>        pword = Base.Utility.StrToByteArray("jellopudding")
>>
>>        Dim reader As New PdfReader(sourcefiles(0), pword)
>>
>>        ' we retrieve the total number of pages
>>        Dim n As Integer = reader.NumberOfPages
>>        '      Console.WriteLine("There are " & n & " pages in the original file.")
>>        ' step 1: creation of a document-object
>>        Dim document As New Document(reader.GetPageSizeWithRotation(1))
>>
>>        ' step 2: we create a writer that listens to the document
>>
>>        Dim writer As PdfWriter = PdfWriter.GetInstance(document, New FileStream(Masterpdf, FileMode.Create))
>>        ' step 3: we open the document
>>        document.Open()
>>
>>        Dim cb As PdfContentByte = writer.DirectContent
>>        Dim page As PdfImportedPage
>>        Dim rotation As Integer
>>        ' step 4: we add content
>>
>>        f = 1
>>        While f < sourcefiles.Length
>>
>>            Dim i As Integer = 0
>>            While i < n
>>                i += 1
>>                document.SetPageSize(reader.GetPageSizeWithRotation(i))
>>
>>                document.NewPage()
>>
>>                page = writer.GetImportedPage(reader, i)
>>                rotation = reader.GetPageRotation(i)
>>                If rotation = 90 Or rotation = 270 Then
>>                    cb.AddTemplate(page, 0, -1, 1, 0, 0, reader.GetPageSizeWithRotation(i).Height)
>>                Else
>>                    cb.AddTemplate(page, 1, 0, 0, 1, 0, 0)
>>                End If
>>
>>            End While
>>
>>            System.Diagnostics.Debug.WriteLine("Processed document " & f & " - " & sourcefiles(f))
>>
>>            f += 1
>>
>>            If f < sourcefiles.Length Then
>>
>>                reader = New PdfReader(sourcefiles(f), pword)
>>
>>                ' we retrieve the total number of pages
>>                n = reader.NumberOfPages
>>
>>                System.Diagnostics.Debug.WriteLine("There are " & n & " pages in the original file.")
>>            End If
>>
>>       End While
>>
>>        document.Close()
>>
>>    End Sub
>>
>>
>>End Class
>>
>>


Charles Hankey

Though a good deal is too strange to be believed, nothing is too strange to have happened.
- Thomas Hardy

Half the harm that is done in this world is due to people who want to feel important. They don't mean to do harm-- but the harm does not interest them. Or they do not see it, or they justify it because they are absorbed in the endless struggle to think well of themselves.

-- T. S. Eliot
Democracy is two wolves and a sheep voting on what to have for lunch.
Liberty is a well-armed sheep contesting the vote.
- Ben Franklin

Pardon him, Theodotus. He is a barbarian, and thinks that the customs of his tribe and island are the laws of nature.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform