We use the tEncryption method to do this. To encrypt a pdf file we need to set the encryption option for the document using the PdfWriter class. tEncryption("chandan".getBytes(), "chandansingh".getBytes(), PdfWriter.ALLOW_ASSEMBLY, PdfWriter.ENCRYPTION_AES_256) Ĭatch (FileNotFoundException | DocumentException e) In this section, we will see how we can encrypt the PDF Files we generate using the PdfWriter class. One of the most common operation we may want to perform on the PDF we generate is to protect them from unauthorized eyes.The best way to achieve this is to password protect the file. This throws NullPointerException when the attachment object is added to the body.įig 3 : Attachment to a PDF 4. One important thing to note here is that the document should be open for writing before get the instance of the PdfFileSpecification, else the document body is null. Then we attach the Specification to the writer using PdfWriter#addFileAttachment method which in turn attaches to the Document body. We get the .PdfFileSpecification instance by passing the source of the file and the description which we want to see in the attachment section of the PDF. PdfWriter.addFileAttachment("Sample Attachment", fileSpecification) PdfFileSpecification fileSpecification = PdfFileSpecification.fileEmbedded(pdfWriter, file.getAbsolutePath(),file.getName(), null) PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream("HelloWorld.pdf")) Let’s see the code :ĪddAttachment.java package In this section we will demonstrate how we can attach a file to a PDF document while creating it. Note: Decreasing the opacity of the image prior to using it in the document will improve the aesthetics of the document. Here’s how the book-marked document looks like : Next we need to register this event listener with the PdfWriter class. Writer.getDirectContentUnder().addImage(background, width, 0, 0, height, 0, 0) Image background = Image.getInstance("src\\main\\resources\\JavaCodeGeeks-water3.png") Import .PdfPageEventHelper Ĭlass PDFEventListener extends void onEndPage(PdfWriter writer, Document document) This event listener class will listen for the page end event and add the water-mark when it encounters such an event. To add a water-mark to the PDF generated, we shall create a event-listener class. We will see how we can add water-mark to our HelloWorld PDF generated above. When generating PDF documents, many-a-times it is important to add water-mark of your organization or your client’s organization to the document. Once the Document is closed, nothing can be written to the body anymore. When we close the document(line 31), all the elements written to the document get flushed and thus written to the PDF File. Adding an element to the document is enough to get it written to the PDF. Then we add a Paragraph element to the document and indent it to the center. Then pass this Documentinstance to the PDFWriter class along-with the FileOutputStream object. Paragraph otherParagraph = new Paragraph() Public static void main(String args) throws FileNotFoundException, DocumentExceptionĭocument document = new pdfWriter = PdfWriter.getInstance(document, new FileOutputStream("HelloWorld.pdf")) We will look at a simple code that will create a PDF with “Hello-World”. We will see in a while how we can achieve this, let’s have a look at the table of contents. The Jar provides implementation of the encryption algorithms we will use, to encrypt the PDF document we generate. We need this Jar for encrypting the PDF Files that we have created. This pom.xml has one more dependency for the bouncycastle.jar.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |