You are here:   Blogs
Register   |  Login



How to use PdfDocumentEditor to Edit Pdf Documents

Dec 2

Written by:
12/2/2010 8:08 PM  RssIcon

Nasosoft.Documents.Pdf offers the capability to manipulate pdf documents. It allows you to:
  1. Merge two pdf documents
  2. Insert pages from another pdf document to certain location of current one
  3. Append pages from another pdf document to the end of current one
  4. Extract specified pages to new pdf document
  5. Split pdf document to single-page documents

Here's a simple example:

1     PdfDocumentEditor pdfEditor = new PdfDocumentEditor("file1.pdf");

2     pdfEditor.Insert(1, "file2.pdf", new int[] { 0, 1 });

3     pdfEditor.Insert(5, "file2.pdf", new int[] { 2, 3 });

4     pdfEditor.Merge("file3.pdf");

5     pdfEditor.Append("file4.pdf", 0, 4);


7     pdfEditor.Save("Insert_Merge_Append.pdf");

8     pdfEditor.Close();

Line1: Create a new PdfDocumentEditor object, set "file1.pdf" as current pdf document.

Line2: Insert page 0 and 1 from file2.pdf to file1.pdf, at location page 1. (All page index is 0-based)

Line3: Again, insert page 2 and 3 from file2.pdf to file1.pdf at location page 5.

Line4: Merge file3.pdf to current pdf object (That is, the file1.pdf after insert operations)

Line5: Append page 0~4 from file4.pdf to the end of current pdf object

Line7: Save the result to a new file.

The operations on pdfEditor won't reflect to the original file on disk, but you have to save it to a new one.

After pdfEditor.close(), the object is considered disposed, no longer available.

Here's another example on how to use page split:

1     PdfDocumentEditor pdfEditor = new PdfDocumentEditor("file1.pdf");

2     pdfEditor.SplitToPages("file1_page_{0}.pdf");

3     pdfEditor.Close();

This one is easy, the only thing need to mention is "{0}" inside "file1_page_{0}.pdf", it act as a placeholder for page index (0, 1, 2, ...) in destination filename. So the single-page pdf document will be named after "file1_page_0.pdf", "file1_page_1.pdf", ...

Also, there is an overloaded version gives you an array of MemoryStream, with each MemoryStream contains one single-page pdf document. It offers you the versatility how to handle them:

1     MemoryStream[] outStreams = null;

2     PdfDocumentEditor pdfEditor = new PdfDocumentEditor("file1.pdf");


4     pdfEditor.SplitToPages(out outStreams);

5     for (int index = 0; index < outStreams.Length; ++index)

6     {

7           FileStream file = new FileStream(String.Format("out_{0}.pdf", index), FileMode.Create);

8           outStreams[index].WriteTo(file);

9           file.Close();

10    }

11    pdfEditor.Close();