![]() ![]() Creating a Sub HeaderĬreate a Sub Header with text alignment center and set the font size to 15. Figure 4 is the image of a PDF document with header text.įigure 4: Showing the header text B. Run the program and go to the path specified in PdfWriter and open the PDF document. Paragraph header = new paragraph("HEADER") PdfDocument pdf = new PdfDocument(writer) PdfWriter writer = new PdfWriter("C:\\demo.pdf") Must have write permissions to the path folder Finally, we need to close the document object by calling the Close() method. Following is the code snippet to create a paragraph object and add it to the document object. We can achieve this by creating a paragraph object. Header Content is center aligned to the document and I set the font size to 20. Adding a HeaderĪdd a header to the PDF document. I will add a Header, SubHeader, Line Separator, Image, Table, Hyperlink, and finally page numbers to the PDF document. SetTextAlignment: Sets the text alignment, such as Left, Right, Center, and so forth.SetFontSize: Sets the font size of the text.Paragraph: Creates a paragraph, initialized with some text.Document: Creates a document from in-memory PdfDocument.It will open a PDF document in writing mode. PdfDocument: In-memory representation of the PDF document.PdfWriter: To pass the file name and write content to the document.Select “Browse” and, in the search box, type itext7 and select itext7 from the searched results and install (see Figure 3).įollowing are the helpful classes and methods to generate the PDF document: Right-click the project name and select “Manage NuGet Packages…”. On the “New Project window”, select the Console App(.NET Framework) and give the project a name, as shown in Figure 1.įigure 1: Selecting the Console App(.NET Framework) Step 2: Install iText 7 Using “Manage NuGet Packages…” In Visual Studio, go to File -> New -> Project. Setting Up the Project Step 1: Create the Console App Using Visual Studio ![]() Installed iText 7 Library using NuGet Package Manager.If you are struggling with C#, consider checking out the TechRepublic Academy. iText is a library for creating and manipulating PDF files in. ![]() In my research, I came to know about iText. I had a requirement to generate an invoice as a PDF document for one of the projects I worked on. tEncryption(PdfWriter.STRENGTH128BITS, "Hello", "World", PdfWriter.AllowCopy | PdfWriter.AllowPrinting) ĭocument.add(new Paragraph(new Date().This article is about generating PDF documents using C#. ![]() PdfWriter writer=PdfWriter.getInstance(document,new FileOutputStream(path)) OutputStream file = new FileOutputStream(new File(path)) ĭocument.add(new Paragraph("Hello World")) ĭocument.add(new Paragraph(new Date().toString())) Ĭreate a Password Protected PDF in java import java.io.File String path="C:Documents and SettingsanshumanDesktopTest.pdf" After you execute this, a file Test.pdf will be created in C: drive (If you are using Linux, you may want to have /usr/test.pdf as path). All you have to do is to put itext.jar in your class path and paste following code in GeneratePDF.java class and compile and execute it. It is very easy to generate a simple PDF file in Java using iText. Generate simple PDF in Java using free Java-PDF library ![]()
0 Comments
Leave a Reply. |