locked
Problem with using FileMode.Append to Pdf... overwriting instead of appending RRS feed

  • Question

  • User1968409173 posted

    Hey there,

    I'm trying to append some text to an existing Pdf file. I'm using FileMode.Append but it is overwriting the file instead of appending to it. I'm using iTextSharp:

     

     

    iTextSharp.text.Rectangle page_size = new iTextSharp.text.Rectangle(PageSize.A3);
                Document myDocument = new Document(page_size.Rotate());
    
                try
                {
                    string name = "My Name";
                    string reportName = "Report.pdf";
                    PdfWriter writer = PdfWriter.GetInstance(myDocument, new FileStream(reportName, FileMode.Append, FileAccess.Write));
                    myDocument.Open();
                    myDocument.Add(new Paragraph(name));
                    myDocument.Close();
    
                    
                    Msg.Text = "Name added successfully";
                    Panel1.Visible = true;
                    return true;
                }
                catch (DocumentException de)
                {
                    Msg.Text = de.Message;
                    Panel1.Visible = true;
                    return false;
                }
                catch (IOException ioe)
                {
                    Msg.Text = ioe.Message;
                    Panel1.Visible = true;
                    return false;
                }


     

    Any ideas why?

     

    Thanks

    Thursday, October 14, 2010 4:41 PM

All replies

  • User1520641890 posted

    hi,

    I'm trying to append some text to an existing Pdf file. I'm using FileMode.Append but it is overwriting the file instead of appending to it.

    iTextSharp doesn't work like that - if you want to manipulate an existing PDF you need to use PdfReader. in other words, FileMode.Append has no effect because you're creating a new document, not using an existing one.

    here is a link with some examples:

    http://kuujinbo.info/iTextInAction2Ed/index.aspx#CH6


    Thursday, October 14, 2010 11:09 PM
  • User1968409173 posted

    hi,

    iTextSharp doesn't work like that - if you want to manipulate an existing PDF you need to use PdfReader. in other words, FileMode.Append has no effect because you're creating a new document, not using an existing one.

    here is a link with some examples:

    http://kuujinbo.info/iTextInAction2Ed/index.aspx#CH6


     

    Hey kuujinbo, thanks for the reply and the link!... I lokoed at it, and what I found was tutorials for concatenating several pdf files together. Can you show me how to simply add a paragraph at the end of an existing pdf file, I don't want to create a new file or a new page, just add the content at the end of the last page of the current file...

     

    Thanks

    Friday, October 15, 2010 11:13 AM
  • User1520641890 posted

    hi,

    Can you show me how to simply add a paragraph at the end of an existing pdf file, I don't want to create a new file or a new page, just add the content at the end of the last page of the current file...

    unfortunately you cannot append content to a PDF document like you would with a text file. PDF has a standard file structure that, among other things, defines how the pages in a PDF are assembled. IIRC the latest version of iTextSharp has some work in-progess (i.e. your mileage may vary) methods to do what you're asking for, but i've never tried it.

    with that being said you can use this code to add a paragraph to a new page of an existing document:

    PdfReader reader = new PdfReader(Server.MapPath(PDF_PATH));
    int n = reader.NumberOfPages;
    PdfStamper stamper = new PdfStamper(reader, context.Response.OutputStream);
    PdfImportedPage page;
    for (int i = 0; i < n;) {
      page = stamper.GetImportedPage(reader, ++i);
    }
    int lastPage = n + 1;
    Rectangle rect = PageSize.LETTER;
    stamper.InsertPage(lastPage, rect);
    PdfContentByte cb =  stamper.GetOverContent(lastPage);
    ColumnText.ShowTextAligned(
      cb, Element.ALIGN_LEFT, new Phrase("YOUR PARAGRAPH"), 
      rect.Left + 36, rect.Top - 72, 0
    );          
    stamper.Close();

    basically the code:

    1. uses a PdfReader to get all the pages of an existing document to add to a new document/copy using PdfStamper.
    2. add your content to a new page at an exact location; rect.Left + 36 => the page's x-coordinate with 1/2 inch margin. rect.Top - 72 => page's y-coordinate with 1 inch margin.

    if you really need to do what you're asking for and/or if you intend to try more complicated things using iTextSharp I suggest you buy the book or start studying the source code.

    Saturday, October 16, 2010 1:39 AM
  • User1968409173 posted

    Thanks a lot kuujinbo for the help, I really appreciate it! I'm just gonna keep things simple for now, and give your code a try Smile

     

    Thanks again!! 

    Monday, October 18, 2010 10:29 AM