Results 1 to 19 of 19

Thread: iTextSharp help

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    iTextSharp help

    Here is what I need to do: I need to create a PDF drawing of our products with dimensions filled in, based on template files. After the user selects the proper template file for the desired product, a window should pop up that prompts the user for the necessary dimensions, such as width, length, etc. Then the program would fill in all the fields on the template form with the proper values. Some fields would just be filled with the exact values that the user enters in, but other fields may require a calculated value based on the entered values. ie. Overall width = nominal width + 6. So somehow I need to figure out how to make the template hold the equations. Can anyone offer me some guidance on how to make this work the way I described?
    Can iTextSharp determine if a PDF file has fields in it? Can it loop through all the fields & get the field names? Any help would be appreciated. Thanks...

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: iTextSharp help

    Yes, iTextSharp can fill in the fields of an AcroForm. You'd use a PdfReader object to open a pdf file, the call the GetFields method to get all the fields in that pdf. You now can iterate thru the fields and get info from them (what type they are, their names...) You then use a PdfStamper object to make a copy of the original pdf, fill in values for the fields and write it to a new pdf file.
    If you do a lot pdf manipulation using iTextSharp as I do, I suggest that you buy the book "iText in Action" written by Bruno Lowagie who is the creator of iText (from which iTextSharp was ported).
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: iTextSharp help

    Thanks stanav ... I was hoping you would reply. You seem to be the iTextSharp expert here. I will try you suggestions, but I will probably have more questions. Also, any suggestions for how to handle the fields that need to be calculated?

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: iTextSharp help

    Simplest way is just calculate the value and assign the result to the field as you fill it. The SetField method of an AcroForm object takes only strings as the parameters, but if the calculation is simple, you can do an inline calculation then call the ToString method of it. For example
    Code:
    'somewherer in your code, you have the quantity of a product store in a variable named "quantity"
    acrFrm.SetField("product.totalcost", (3.00 x quantity).ToString)
    Otherwise, you can always calculate the result then set the filed value separately.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: iTextSharp help

    It really won't be feasible for me to put the calculations in code because the calculations are form dependant. On one form a field might need to be 'quantity x 3' & on another form it might need to be 'quantity x 5'. And there will probably be 40-50 different forms. So somehow the form itself needs to contain the equations. This might be a stretch, but do you think it's possible to use the field name to hold the equation? ie. name a field 'qtyx5' or 'width+6' & then convert the parts into the equation?

  6. #6
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: iTextSharp help

    Are the template files created by you or you have some control over its creation? If yes then I beleive you can add a property to a field when you create the template, and when you fill the form, you just read back that property from the field to determine what to be done.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: iTextSharp help

    Stanav ... yes, we will be creating the forms ourselves. We will start with an existing PDF file & add fields to it with Adobe Acrobat. Is that where/when we would add the property you speak of?

  8. #8
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: iTextSharp help

    Yes, for example, if you want a textfield that has a multiplier property (which store a numeric value), you'd create a custom text field which consists of a standard text field object and a standard numeric field object. You set the default value for the numeric field and hidden from layout. Save the whole thing as a custom object. I don't know how well iTextSharp can work with custom objects though.
    You can also try to make use of the name of the field as you said in post#5, name it something that you can parse out the needed info from the name.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: iTextSharp help

    stanav ... I am having a heck of a time figuring out how to get the fields in the form. I haven't been able to find any documentation or code examples online. This is what I have so far:

    Code:
            Dim myPdfForm As String = "c:\myFile.pdf"
            Dim fs As New FileStream(pdfFile, FileMode.Create, FileAccess.Write)
    
            Dim PdfrReader As PdfReader = New PdfReader(myPdfForm)
            Dim PdfStamper As New PdfStamper(PdfrReader, fs)
            Dim af As AcroFields = PdfStamper.AcroFields
    Can you help me out a bit?

  10. #10
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: iTextSharp help

    Can you upload a sample pdf file, Nbrege?
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: iTextSharp help

    Quote Originally Posted by stanav
    Can you upload a sample pdf file, Nbrege?
    A sample with fields or without?

  12. #12
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: iTextSharp help

    Quote Originally Posted by nbrege
    A sample with fields or without?
    One with fields, of course. We're trying to fill in the fields, aren't we?
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  13. #13

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: iTextSharp help

    Of course ... how silly of me. Must be a Friday.
    OK, here is a quick test form I made with 2 fields in it, Length & Length2.
    Attached Files Attached Files

  14. #14
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: iTextSharp help

    OK, here is some quick stuff I put together for you.

    Code:
    Public Sub FillAcroForm(ByVal sourcePdf As String, ByVal fieldData As DataTable, ByVal outputPdf As String)
            Try
                'Open the pdf using pdfreader
                Dim reader As New iTextSharp.text.pdf.PdfReader(sourcePdf)
                'create a filestream for output
                Dim fs As New System.IO.FileStream(outputPdf, IO.FileMode.Create, IO.FileAccess.Write)
                'use stamper to copy the source pdf to output
                Dim stamper As New iTextSharp.text.pdf.PdfStamper(reader, fs)
                'Get the form from the pdf
                Dim frm As iTextSharp.text.pdf.AcroFields = stamper.AcroFields
                'get the fields from the form
                Dim fields As System.Collections.Hashtable = frm.Fields
                Dim matchedRows() As DataRow = Nothing
                'Read each field name, look for the data in datatable, then set the field value
                For Each key As String In fields.Keys
                    matchedRows = fieldData.Select("FieldName = '" & key & "'")
                    If matchedRows.Length > 0 Then
                        frm.SetField(key, matchedRows(0).Item("FieldValue").ToString())
                    End If
                Next
                stamper.Close()
                fs.Close()
                reader.Close()
            Catch ex As Exception
                Debug.Write(ex.Message)
            End Try
    End Sub
    And You call the sub like this
    Code:
    'Create a datatable to hold the values for each field
            Dim dt As New DataTable()
            With dt.Columns
                .Add("FieldName", GetType(String))
                .Add("FieldValue", GetType(String))
            End With
    
            dt.Rows.Add("Length", "10")
            dt.Rows.Add("Length2", "30")
    
            'Fill the form, passing in the datatable
            FillAcroForm("e:\formtest.pdf", dt, "e:\filled.pdf")
    If you name your fields in using a splittable scheme, you can split the field name and look at the extra stuff to adjust your values when setting them. For example, instead of name a field "Length2", you can name it "Length_2". And when you read the field name, you spit it by "_", test the length of the array, if > 1 then you know you have some extra stuff. Read the 2nd element of the array to find out what it is.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  15. #15

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: iTextSharp help

    Thanks again stanav. I will try out your code & let you know how I fare...

  16. #16

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: iTextSharp help

    stanav ... I have few more questions for you.

    1) If I have a multi-page PDF file with fields can I get just the fields from a specific page?

    2) Is there a way to specify a name or a tag for each page in a multi-page PDF file? ie. some sort of text string that is embedded in each page that I can retrieve programatically.

    Thanks for any help...

  17. #17
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: iTextSharp help

    To answer you questions:

    1. According to the PDF Reference, a PDF document may contains any number of fields appearing on any combination of pages, all of which make up a single, global interactive form spanning the entire document. So, the only way to get the fields from some specific pages is to use a naming scheme such that when you look at the field names, you can tell which page that field belongs to.

    2. You can refer to a specific pdf page in a multi-page pdf file by using the page index, and that's about it. If all you know is some string of text and you want to get the pdf page that contains that text, you can try looping thru the pages and extract the text from each page, test if it contains your string. This is doggy, but it should get the job done. And no, pdf pages don't have anything like the "Tag" property on .net winform controls that you can use.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  18. #18

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: iTextSharp help

    Not what I wanted to hear, but thanks for the reply...

  19. #19
    New Member
    Join Date
    May 2010
    Posts
    1

    iTextSharp help

    Hi itextsharp guys!!! seems m following a rather old thred, but i've stuck on a point n while searching i found dis thread helpful(2 some xtent though).

    my issue is li'l diff from wat has been discussed here...

    i want 2 create n application where forms r not fixed, in d sense dat, d other day, admin can upload cmpletely new forms for the others 2 fill(it means no DB table can be used to save every info)
    while registering user will fill some details. if ny PDF form has dose fields to fill it will be filled automatically and oders will be filled manually by d user(in the form showing in browser).

    i m using "itextsharp".

    now problem is dat, i can fill d values from backend(coz m keeping d name of acro form fields same s DB table fields, so can map b/w them) but what about oder fields. i mean if i show the PDF on browser, it is dere for every1 2 c n 1 can also fill d textboxes but "HOW 2 FETCH D VALUES DAT IS FILLED IN THESE TEXTBOXES PROGRAMATICALLY"???

    for ex, like v've in ASP.net,
    String strName=txtName.Text;

    so i want equivalent of dis line using "itextsharp"...

    i know, u'll say dat v've "AcroFields.GetField()" function is there, but this is to get the value from d field once a form is saved with value. i want to have a way by which i can fetch d value from the control dat is filled by d user 8 d time.....

    (currently showing d PDF form in iframe, i know this way its IMPOSSIBLE to achieve wat i want, so wat 2 do n HOW 2 DO??)

    my description here may b a mesh, but problem is REALLLLY importaqnt for me....

    hope 2 listen from u SOOOOOON...

    THNX IN ADVANCE!!!!!!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width