Nov 18, I am not sure that PDF writers take account of newline characters. Looking at http ://?id=[^] I think you need to. In the last couple of examples of chapter 4, we worked with an existing PDF document. We’ll start by adding a text annotation, some text, and a new check box. In addition to the excellent answers above, the following shows how to add text to each page of a multi-page document: using (var reader = new.

Author: Gardabei Kigazragore
Country: Spain
Language: English (Spanish)
Genre: Health and Food
Published (Last): 17 December 2015
Pages: 326
PDF File Size: 16.61 Mb
ePub File Size: 20.81 Mb
ISBN: 446-2-98341-900-4
Downloads: 86401
Price: Free* [*Free Regsitration Required]
Uploader: Gora

In the examples for chapter 1 to 3, we’ve always created a new PDF document from scratch na iText. In the last couple of examples of chapter 4, we worked with an existing PDF document.

C# tutorial: add content to an existing PDF document

We took an existing interactive PDF form and filled it out, either resulting in a pre-filled form, or resulting in a flattened document that was no longer interactive. In this example, we’ll continue working with existing PDFs. We’ll load an existing file using PdfReader and we’ll use the reader object to create a new PdfDocument. In this chapter, we’ll take it a step further. We’ll start by adding a text annotation, some text, and a new check box. This is shown in Figure 5.

We’ll repeat the code we’ve seen in the previous chapter in the AddAnnotationsAndContent example. Just like in chapter 4, we add the annotation to a page obtained from the PdfDocument instance:. If we want to add content to a content stream, we need to create a PdfCanvas object. We can do this using a PdfPage object as a parameter for the PdfCanvas constructor:. The code to add the text is similar to what we did in chapter 2.

Whether you’re creating a document from scratch, or adding content to an existing document, has no impact on the instructions we use. The same goes for adding fields to a PdfAcroForm instance:.

Adding content with PdfStamper Part 1 (iText 5)

In the FillAndModifyForm example, we return to the FillForm example from chapter 4, but instead of merely filling out the form, we also change the properties of the fields:. We convert this List to a PdfArray line 16 and we use this array to update the options of the “shift” field line We see that the “shift” field now has more options, but we don’t see the background color of the “name” field. It’s also not clear if the font of the “info” field has changed.


Nothing is wrong, the fields are currently highlighted and the blue highlighting covers the background color. Let’s click “Highlight Existing Fields” and see what happens.

We wouldn’t have isnert this problem if we had added form. We’ll make some more forms examples in the next chapter, but for now, let’s see what we can do with existing documents that don’t contain a form. Do you remember the report of the UFO sightings sn the 20th century we created in chapter 3?

We’ll use a similar report for the next couple of examples: As you can see, it’s not so fancy as the report we made in chapter 3. What if we’d like to add a header, a watermark and a footer saying “page X of Y” to this existing report?

appending text in Existing Pdf file using C#, itextSharp | The Forums

In chapter 3, we didn’t know the total number of pages at the moment we were adding the footer, hence we only nisert the current page number. Now that we have an existing document, we can add “1 of 4”, “2 of 4”, and so on. When creating a document from scratch, it’s possible to create a placeholder for the total number of pages.

Once all the pages are created, we can then add the total number of pages to that placeholder, but that’s outside the scope of this introductory tutorial.

The AddContent example shows how we can add content to every page in an existing document. We use the pdfDoc object to create a Document instance. We’ll use that document object to add some content. We also use the pdfDoc object to find the number of pages in the original PDF. We loop over all the pages, and we get the PdfPage object of each page. A header line A footer line line A footer with the page number A watermark lin Then we change the opacity of the canvas.

Finally we add the Paragraph to the document, centered in the middle of the page and with an angle of 45 degrees, using the showTextAligned method. We’re doing something special when we add the watermark.


We’re changing the graphics state of the canvas object obtained from the page. Then we add text to the corresponding page in the document. Internally, iText will detect that we’re already using the PdfCanvas instance of that page and the showTextAligned method will write to that same canvas. This way, we can use a mix of low-level and convenience methods.

In the final example of this chapter, we’ll change the page size and orientation of the pages of our UFO sightings report.

If we take existng look at Figure 5. The ChangePage example shows how this was done. No need for a Document instance here, we work with the PdfDocument instance only. We loop over all the pages line 4 and get the PdfPage instance of each page line 5. A page can have different page boundaries, one of which isn’t optional: We get inserh value of this page boundary as a Rectangle line 7 and we create a new Rectangle that is an inch larger on each side line We use the setMediaBox method to change the page size.

We create a PdfCanvas object for the page line 13and we stroke a gray line using the dimensions of the original mediaBox line We have tried to keep the examples simple, but that also means that we’ve cut some corners. We’ll need a more in-depth tutorial to cover topics like these. In the previous chapter, we learned about interactive PDF forms. In this chapter, we continued itexfsharp with these forms.

We igextsharp an annotation, some text, and an extra field to an existing form.

Inserting Text To an Existing Pdf using Itext

We also changed some properties while filling out a form. We then moved on to PDFs without any interactivity.

First, we added a header, a footer, and a watermark. Then, we played with the size and the orientation of the pages of an existing itextshqrp. In the next chapter, we’ll scale and tile existing documents, and we’ll discover how to assemble multiple documents into a single PDF. Skip to main content.

Jump-Start Tutorial Chapter 5: Manipulating an existing PDF document Chapter 5: Manipulating an existing PDF document Tags: