0

How can I merge two PDF files and add my document text to new PDF so as to generate one long PDF file without losing annotation from existing two pdf files?

I used PDFwriter for this but it loses the annotation, but when I used PDFSmartCopy, it merges the PDF files (without losing the annotation) but it doesn't display document text. Here is my code:

Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
PdfWriter wri = new PdfSmartCopy(doc, new FileStream("test.pdf"), FileMode.Create));
doc.Open();

PdfReader reader = new PdfReader(binadypdffile);

PdfContentByte content = wri.DirectContent;

doc.Add(new Paragraph("my text here on new page", FontFactory.GetFont("Arial", 12, Font.BOLD)));

int numberOfPages = reader.NumberOfPages;

for (int currentPageIndex = 1; currentPageIndex <= numberOfPages; currentPageIndex++)
{
    doc.NewPage();
    PdfImportedPage importedPage = wri.GetImportedPage(reader, currentPageIndex);
    int pageOrientation = reader.GetPageRotation(currentPageIndex);
    ((PdfSmartCopy)wri).AddPage(importedPage);
}
doc.Close();
Chris Haas
  • 53,986
  • 12
  • 141
  • 274
  • I can't write sample code right now (sorry) but I can hopefully help you. The `Document` class is part of iTextSharp's PDF abstraction helpers. Along with `Paragraph` and `PdfPTable` and other abstractions, these abstractions maintain a "current position" and know how to "wrap text" and "center". The `PdfContentByte` class, however, is used to write raw PDF commands. Generally speaking, `Document` is only useful for creating new PDFs. See this post for a solution to partially mixing the abstractions with the raw commands: http://stackoverflow.com/a/2762518/231316 – Chris Haas Mar 20 '14 at 00:37
  • Sorry, but it didn't worked, my problem is merging the PDF and doc.Add(new Paragraph("my text here on new page", FontFactory.GetFont("Arial", 12, Font.BOLD))); without loosing the annotation on to new PDF file i – user3439315 Mar 20 '14 at 13:48
  • 1
    Since you want annotations `PdfSmartCopy` and `PdfCopy` are your only two choices. These two are intended for copying existing documents and not working with the abstractions. If you want to add text then you'll need to use `ColumnText`, see http://stackoverflow.com/a/3249346/231316. If you really want to use the abstractions then you'll just have to make two passes. The first pass creates a new document and the second pass merges that document with your other document. – Chris Haas Mar 20 '14 at 14:09

0 Answers0