![]() ![]() * This class is used to create a pdf file using iText jar. Image img = Image.getInstance(sFileName) ĭtPageSize(new Rectangle(img.getWidth(), img.Import java.io.File import java.io.FileOutputStream import java.io.OutputStream import import import .PdfWriter /** Create PDFWriter instance and pass Document and OutputStream instance to its constructor. This low-level class took care of all PDF-related structures. ![]() PdfContentByte cb = writer.getDirectContent() įloat colwidth = (document.getPageSize().getRight() - document.getPageSize().getLeft() - gutter) / 2 įloat left = It represents the current document to which we are adding content. When using a PdfWriter, a PdfDocument was created internally. We can find the latest available version of iText 7 in the Maven Central. PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(new File(RESULT_FOLDER, "interline-user3208131-fixed.pdf"))) įont font = new Font(FontFamily.UNDEFINED, 4, Font.UNDEFINED, null) To get started with the library we need to add itext7-core Maven dependency to your pom.xml as defined in the code below. After the intermezzo about page boundaries, containing some self-glorifying examples, its time to return to the. This library is particularly useful to create a consistent. The library offers some powerful features to generate read-only, platform-independent documents which contain not only text but also lists, tables, and images. * void testLikeUser3208131Fixed() throws DocumentException, FileNotFoundException Adding page events to PdfWriter (iText 5). The iText is a Java library that enables a developer to generate and manipulate PDF documents through Java code on the fly. Thus, he has to use ColumnText#setLeading(float, float)} PdfWriter / This class is used to create a pdf file using iText jar. * Indeed, the OP's Phrase#setLeading(float, float)} calls are ignored, Create PDFWriter instance and pass Document and OutputStream instance to its. Now let's deactivate the event.")) ĭocument.add(new Paragraph("This paragraph no longer has a background.")) Isn't that fantastic? By changing the event, we can even draw a border, change the line width of the border and many other things. As long as the event isn't activated, nothing special happens, but let's make the event active and see what happens:")) ĭocument.add(new Paragraph("This paragraph now has a background. The ITextWritePdfFile class creates a file called itext-test.pdf and creates an output stream to write to this file. You add the paragraphs/chunks/etc to the ColumnText, and pass it the PdfContentByte (and some positional parameters) to draw the text. ParagraphBackground border = new ParagraphBackground() ĭocument.add(new Paragraph("In this document, we'll add several paragraphs that will trigger page events. You need to create a ColumnText object, and get a PdfContentByte by calling myStamper.getOverContent (pageNum). PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(new File(RESULT_FOLDER, "document-with-paragraph-backgrounds.pdf"))) ![]() It can wrap an extra line if it needs to so this text is long.", void testParagraphBackgroundEventListener() throws DocumentException, FileNotFoundException PdfContentByte canvas = writer.getDirectContent() ĬolumnText.showTextAligned(canvas, Element.ALIGN_CENTER, new Phrase("36222-0", fontQouteItems), 60, 450, 0) ĬolumnText.showTextAligned(canvas, Element.ALIGN_CENTER, new Phrase("47", fontQouteItems), 143, 450, 0) ĬolumnText ct = new ColumnText(canvas) // Uses a simple column box to provide proper text wrappingĬt.setSimpleColumn(new Rectangle(193, 070, 390, 450)) Ĭt.setText(new Phrase("In-Situ : Poly Cable - 100'\nPoly vented rugged black gable 100ft\nThis is an additional description. ![]() PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(new File(RESULT_FOLDER, "ColumnTextTopAligned.pdf"))) įont fontQouteItems = new Font(BaseFont.createFont(), 12) * void testShowTextAlignedVsSimpleColumnTopAlignment() throws DocumentException, IOExceptionĭocument document = new Document(PageSize.A4) * denotes the baseline while ColumnText#setSimpleColumn(Rectangle)} surrounds * ColumnText#showTextAligned(PdfContentByte, int, Phrase, float, float, float)} Also, the structure of iText allows us to generate any of the above-mentioned type of documents with the same code. We can choose from various fonts to be used in the document. PdfWriter / Java Program to generate PDF document using iText library. iText is freely available from The iText library is powerful and supports the generation of HTML, RTF, and XML documents and generating PDFs. * Indeed, the coordinates do not line up. Earlier, I have shared about iText vs Apache FOP, two of the most popular. * ColumnText.ShowTextAligned vs ColumnText.SetSimpleColumn Top Alignment ![]()
0 Comments
Leave a Reply. |