Save this code in a file with the name create_PDF.java. It creates a PDF document with the name sample.pdf, adds an empty page to it, and saves it in the path C:/itextExamples/ Instantiate the Document class by passing the object of the class PdfDocument created in the previous steps as shown below.ĭocument document = new Document(pdfDoc) Ĭlose the document using the close() method of the Document class as shown below.įollowing is the Java program which demonstrates the creation of a PDF Document. Concept: PdfWriter and Document Add high-level objects (e.g. One of the constructors of this class accepts an object of the class PdfDocument. Another look at iText 7 from the Great Indian Developer Summit in 2016. The Document class of the package is the root element while creating a self-sufficient PDF. The addNewPage() method of the PdfDocument class is used to create an empty page in the PDF document.Īdd an empty page to the PDF document created in the previous step as shown below. Once a PdfDocument object is created, you can add various elements like page, font, file attachment, and event handler using the respective methods provided by its class. PdfDocument pdfDoc = new PdfDocument(writer) Instantiate the PdfDocument class by passing the above created PdfWriter object to its constructor, as shown below. To instantiate this class (in writing mode), you need to pass an object of the class PdfWriter to its constructor. The PdfDocument class is the class that represents the PDF Document in iText. When an object of this type is passed to a PdfDocument (class), every element added to this document will be written to the file specified. String dest = "C:/itextExamples/sample.pdf" Instantiate the PdfWriter class by passing a string value (representing the path where you need to create a PDF) to its constructor, as shown below. You can rate examples to help us improve the quality of examples. These are the top rated real world C (CSharp) examples of extracted from open source projects. The constructor of this class accepts a string, representing the path of the file where the PDF is to be created. C (CSharp) iTextSharp.text Document.SetMargins - 30 examples found. The PdfWriter class represents the Doc Writer for a PDF. While instantiating this class, you need to pass a PdfDocument object as a parameter to its constructor.įollowing are the steps to create an empty PDF document. You can create an empty PDF Document by instantiating the Document class. If you need to add more fields, tables, images, or complicated PDF designs you can find a lot of examples here, or contact me, I will be happy to help you.Let us now understand how to create a PDF document using the iText library. AddParagraph("Lorem ipsum dolor sit amet") SetOrientation(PageOrientation.Portrait) I see you have more than one issue using iText7, what version are you using? and if it's not working properly you can try a different library, such as ironPDF or PDFFlow, I use PDFFlow and this simple code to create a PDF file with setting margins to 50 and the orientation to portrait: //Create a document builder: Paragraph p = new Paragraph("YourTextHere") PdfDocument pdfDocument new PdfDocument(pdfReader, pdfWriter, new StampingProperties(). PdfWriter.getInstance(document, new FileOutputStream(dest)) Setting font of the text PdfFont font PdfFontFactory.createFont (FontConstants.HELVETICABOLD) Now, set font to the text using the setFont () method of. Create the PdfFont object using the createFont () method of the class PdfFontFactory of the package as shown below. Hi Jackson, this code will help you set the page size and add margins Document document = new Document() Step 5: Setting the font and color to the text.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |