July 6, 2020

This is the third in a series of articles that looks at using the open source component, iTextSharp from within to generate PDFs. Just as. In this chapter, we’ll add an annotation, some text, and an extra field to an existing form. We’ll also change some properties while filling out the form. We’ll then. I am not sure that PDF writers take account of newline characters. Looking at http ://?id=[^] I think you need to.

Author: Vir Arashit
Country: Malawi
Language: English (Spanish)
Genre: Life
Published (Last): 15 March 2014
Pages: 304
PDF File Size: 19.32 Mb
ePub File Size: 7.66 Mb
ISBN: 386-8-63321-548-1
Downloads: 59657
Price: Free* [*Free Regsitration Required]
Uploader: Zulkitaur

Inserting Text To an Existing Pdf using Itext

In the examples for chapter 1 to 3, we’ve always created a new PDF document from scratch with iText. In the last couple of examples of chapter 4, we worked with 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 ezisting 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 itexhsharp for adding fields to a PdfAcroForm instance:.

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 sxisting background color of the “name” field.


It’s also not clear if the font of the xeisting 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 had 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 in 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? In chapter 3, we didn’t know the total number of pages at the moment we were adding the footer, hence we only added the current page number.

iTextSharp – Adding Text with Chunks, Phrases and Paragraphs

Now that we have an existing document, we can add “1 of itextshapr, “2 of 4”, and so on. When creating a document from scratch, it’s possible to create a placeholder for the total sdding 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 itexgsharp 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. Addin we add text to the corresponding page in the document. Internally, iText will detect itextshap 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 eisting the pages of our UFO sightings report. If we take a look at Figure 5.

The ChangePage example shows how this was done. No need sxisting 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 the 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 itextshzrp 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 working with these forms. We added an annotation, some text, and an extra field to an existing form.

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 exxisting of the pages of an existing document. 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: