locked
Open existing document with Itextsharp? RRS feed

  • Question

  • User-130510759 posted

    Hi,

    Need some help with this one please...

    I've got some code with which I'd like to do the following using Itextsharp. So far I'm good up to step 5.

    1) Create a pdf doc

    2) Add some content

    3) Close the pdf

    4) Create a reader to get some info about the doc created in steps 1-3.

    5) Close the reader

    6) OPEN THE ORIGINAL PDF again for editing.

    I'm good up to step 5. Just don't know the syntax to open an EXISTING doc for editing. I know the path to the doc, just not sure how to create the instance again and specify the path so I can open it for editing.

    Any ideas please?

    Please note I do not want to create a new doc and edit that. I want to open the original.

    Many thanks!

    Jason


    Friday, August 27, 2010 2:28 AM

Answers

All replies

  • User1520641890 posted

    If you need to edit an existing PDF the pattern is:

    // read the existing PDF, you specify correct path
    PdfReader reader = new PdfReader(PDF_FILE_PATH);
    
    // create PdfStamper instance to edit
    PdfStamper stamper = new PdfStamper(reader, Response.OutputStream);
    
    // do stuff
    ....
    
    // close PdfStamper  instance
    stamper.Close(); 
     
    
    
    
    



    Friday, August 27, 2010 4:32 AM
  • User-130510759 posted

    Thanks... that's sounding promising.

    Do you know where I can find a vb.net example of how to use the stamper please? For the life of me I can't find anything online along the lines of what I want. All the examples seem to talk about filling in form fields, etc, not what I need to do.

    Specifically this is what I want to do...

    The existing doc might have 10 blank pages.

    Using a simple loop, I want to pull data from my database, add it to the page, move to the next record on a new page. I know how to do this with a new doc using the pdfwriter and contentbyte, I  just don't know the pdfstamper syntax equivalent.

    Thanks a bunch...

     'create the writer
            Dim writer As PdfWriter = PdfWriter.GetInstance(Document, New FileStream(strPathNewDoc, FileMode.Create))
    
            Document.Open()
    
           
            Dim cb As PdfContentByte = writer.DirectContent
            Dim bf As BaseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED)
    
    
            Document.NewPage()
    
            cb.SetFontAndSize(bf, 20)
            cb.SetTextMatrix(100, 100)
    
    
    cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "some database text", 200, 200, 0)
    
    
            cb.BeginText()
    
            cb.EndText()
    
    
    
    
    
    


    Friday, August 27, 2010 1:30 PM
  • User1520641890 posted

    sorry, there aren't that many examples in C#, much less VB. here's an example in C# to do what you want, but you'll have to change it a bit. if you need VB, just dump the source into one of the many online converters:

    http://converter.telerik.com/
    http://www.carlosag.net/Tools/CodeTranslator/
    http://www.developerfusion.com/tools/convert/csharp-to-vb/

    basically you can use most of the same code you've currently got, but use PdfStamper instead.




    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 27, 2010 1:47 PM