In this chapter, we'll do some more document manipulation, but there will be a subtle difference in approach. We manipulated a single document. In this chapter, we'll always create at least two PdfDocument instances: one or more for the source document s , and one for the destination document. Now we want to create a PDF file with three pages. In page one, the original page is scaled down to

Author:Monris Goltijas
Country:El Salvador
Language:English (Spanish)
Published (Last):28 December 2011
PDF File Size:8.59 Mb
ePub File Size:12.76 Mb
Price:Free* [*Free Regsitration Required]

In this chapter, we'll do some more document manipulation, but there will be a subtle difference in approach. We manipulated a single document. In this chapter, we'll always create at least two PdfDocument instances: one or more for the source document s , and one for the destination document.

Now we want to create a PDF file with three pages. In page one, the original page is scaled down to On page 2, the original page size is preserved. On page 3, the original page is scaled up to We get a PdfPage instance for the first page of the existing PDF line 3 , and we get its dimensions line 4. We then add three pages to the new PDF document:. We add an A4 page using landscape orientation line 7 and we create a PdfCanvas object for that page.

Instead of calculating the a , b , c , d , e , and f value for a transformation matrix that will scale the coordinate system, we use an AffineTransform instance using the getScaleInstance method line We apply that transformation line 13 , we create a Form XObject containing the original page line 14 and we add that XObject to the new page line Adding the original page in its original dimensions is much easier.

We just create a new page by copying the origPage to the new PdfDocument instance, and we add it to the pdf using the addPage method line Scaling up and shrinking is done in the exact same way. This time, we add a new A2 page using landscape orientation line 21 and we use the exact same code we had before to scale the coordinate system line We reuse the pageCopy object and add it to the canvas line We close the pdf to finalize the new document line 30 and we close the origPdf to release the resources of the original document.

Tiling a PDF page means that you distribute the content of one page over different pages. For instance: if you have a PDF with a single page of size A3, you can create a PDF with four pages of a different size —or even the same size—, each showing one quarter of the original A3 page.

This is what we've done in Figure 6. We've seen lines before; we already used them in the previous example. In line 7, we define a tile size, and we create a transformationMatrix to scale the coordinate system depending on the original size and the tile size. Then we add the tiles, one by one: line , line , line , and line are identical, except for one detail: the offset used in the addXObject method.

Let's do the opposite of tiling: let's N-up a PDF. Figure 6. In the next example, we're going to put N pages on one single page. We will put 4 pages on one single page. So far, we've only reused a single page from a single PDF in this chapter. In the next series of examples, we'll assemble different PDF files into one.

The PdfMerger class is new. It's a class that will make it easier for us to reuse pages from existing documents line 2. Just like before, we create a PdfDocument for the source file line 4, line 7 ; we then add all the pages to the merger instance line 5, line 8. Once we're done adding pages, we merge line 10 and close line We don't need to add all the pages if we don't want to. We can easily add only a limited selection of pages. Now the resulting document only has six pages.

Pages 1, 5, 7, 1 from the first document the first page is repeated , and pages 1 and 15 from the second document. PdfMerger is a convenience class that makes merging documents a no-brainer.

In some cases however, you'll want to add pages one by one. Let's examine it step by step. Suppose that we have a TreeMap of all the categories the movie "The Revenant" was nominated for, where the key is the nomination and the value is the page number of the document where the nomination is mentioned.

But we need to take a really close look once we start to loop over the entries in the TreeMap. Line 1: we create a PdfDocument with the source file containing all the info about all the nominations. Line we get the page that corresponds with the nomination, and we add a copy to the PdfDocument. Line we create an iText Text element containing the page number. We subtract 1 from that page number, because the first page in our document is the unnumbered page containing the TOC.

Line 9: we set the background color to Color. This will cause an opaque white rectangle to be drawn with the same size of the Text. We do this to cover the original page number. Line we add this text at a fixed position on the the current page in the PdfDocument. Line we create a PdfArray containing information about the destination. We'll refer to the page we've just added line 15 , we'll define the destination using an X,Y coordinate and a zoom factor line 16 , we add the values of X line 17 , Y line 18 , and the zoom factor line Line we add an action that will be triggered when someone clicks on the Paragraph.

We've been introducing a lot of new functionality that really requires a more in-depth tutorial, but we're looking at this example for one main reason: to show that there's a significant difference between the PdfDocument object, to which a new page is added with every pass through the loop, and the Document object, to which we keep adding Paragraph objects on the first page.

This code snippet adds the check list with the overview of all the nominations. This example introduces a couple of new concepts for educational purposes.

It shouldn't be used in a real-world application, because it contains a major flaw. We make the assumption that the TOC will consist of only one page. Suppose that we added more lines to the document object, then you would see a strange phenomenon: the text that doesn't fit on the first page, would be added on the second page.

This second page wouldn't be a new page, it would be the first page that we added in the loop. In other words: the content of the first imported page would be overwritten. This is a problem that can be fixed, but it's outside the scope of this short introductory tutorial.

Merging forms is special. That's not the case for PDF. In a PDF file, there can be only one form. If you want to merge two forms and you want to preserve the forms, you need to use a special method and a special IPdfPageExtraCopier implementation.

In this code snippet, we use the copyPageTo method. The third parameter defines the destination document. The fourth parameter indicates that we are copying forms and that the two different forms in the two different documents should be merged into a single form. PdfPageFormCopier is an implementation of the IPdfPageExtraCopier interface that makes sure that the two different forms are merged into one single form. Merging two forms isn't always trivial, because the name of each field needs to be unique.

Suppose that we would merge the same form twice. Then we would have two widget annotations for each field. A field with a specific name, for instance "name" , can be visualized using different widget annotations, but it can only have one value.

Suppose that you would have a widget annotation for the field "name" on page one, and a widget annotation for the same field on page two, then changing the value shown in the widget annotation on one page would automatically also change the value shown in the widget annotations on the other page.

In the next example, we are going to fill out and merge the same form, state. We made sure that this doesn't happen by renaming all the fields before merging the forms. Let's start by looking at the code inside the while loop. We skip the first line that contains the information for the column headers line The next couple of lines are interesting.

So far, we've always been writing PDF files to disk. As mentioned before, we start by renaming all the fields. Note that we've skipped some lines for brevity in the code snippet. Once we've renamed all the fields, we set their value line We can now copy the pages of that new sourcePdfDocument to our destination pdfDocument.

This is a rather artificial example, but it's a good example to explain some of the usual pitfalls when merging forms:. One field can only have one value, no matter how many times that field is visualized using a widget annotation.

A more common use case, is to fill out and flatten the same form multiple times in memory, simultaneously merging all the resulting documents in one PDF.

We flattened these filled out forms, and we merged them into one single document. From the outside, these documents look identical, but if we look at their file size in Figure 12, we see a huge difference. What is causing this difference in file size?

The destPdfDocument instance line is created the same way we've been creating PdfDocument instances all along. The destPdfDocumentSmartMode instance line is also created that way, but we've turned on the smart mode.

We loop over the lines of the CSV file like we did before line 8 , but since we're going to flatten the forms, we no longer have to rename the fields. The fields will be lost due to the flattening process anyway. We create a new PDF document in memory line and we fill out the fields line We flatten the fields line 25 and close the document created in memory line We use the file created in memory to create a new source file.


Splitting PDF File In C# Using iTextSharp

By using our site, you acknowledge that you have read and understand our Cookie Policy , Privacy Policy , and our Terms of Service. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. My requirement is as follows: I have a document named "Test" which has 2 different pages. I need to clone the each pages i.


Develop Reference

Develop Reference html css asp. Home About Us Contact Us. This PDF will have n-pages where all of the pages are exactly the same, except for a small text change. So I wanted to clone my Document object, do the change and add as a new page to the document, is it possible?

Related Articles