Adding a cover page to an existing PDF · Create booklet from A4 document · Fonts How not to merge documents · Merging documents and create a Table of . Example written by Bruno Lowagie in answer to the following question: * http:// */ package r; import. Feb 10, PdfStamper appending to existing file. Hi, I have a question regarding PdfStamper. Here is the code snippets FileOutputStream outputStream.
|Published (Last):||25 June 2017|
|PDF File Size:||14.48 Mb|
|ePub File Size:||7.96 Mb|
|Price:||Free* [*Free Regsitration Required]|
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 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.
Adding an image to an existing file | iText Developers
We’ll repeat the code we’ve seen in the previous chapter in the AddAnnotationsAndContent example. Just like esisting 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.
PdfStamper appending to existing file
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:. 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 exishing of the “info” field has changed. Nothing is wrong, the fields are ecisting highlighted and exjsting 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.
Appendd 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.
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 zppend for the total number of pages. Once all the pages are created, we can then add the total number of pages to that alpend, 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 itet 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 a 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 ktext. We loop alpend 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 that we’ve cut iitext 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 apppend 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 apend and the orientation 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: