This sample java program uses jPDFWriter to open a text file and create a PDF file from the text contained in the text file. It takes care of wrapping text into lines and of creating new pages as needed (pagination).
// Page dimensions and margins, in inches float pageWidth = 8.5f; float pageHeight = 11f; float marginLeft = 1; float marginTop = 1; float marginBottom = 1; float marginRight = 1; // Define page format for PDF document Paper p = new Paper (); // set paper size p.setSize(pageWidth * 72, pageHeight * 72); // set no margin to paper (we're taking care of the margins when writing) p.setImageableArea(0, 0, pageWidth * 72, pageHeight * 72); PageFormat pageFormat = new PageFormat (); pageFormat.setPaper(p); // Create the PDF document PDFDocument pdfDoc = new PDFDocument(); // Create font Font font = PDFGraphics.HELVETICA.deriveFont(Font.PLAIN, 11f); // Init page information PDFPage newPage = null; Graphics2D g2 = null; FontMetrics fm = null; float currentY = marginTop * 72; float wrapWidth = (pageWidth - (marginLeft + marginRight)) * 72f; // Create a reader to read the input text file (uses the default encoding) // !! This assumes that the text file is using the default OS encoding // otherwise you will need to specify the encoding BufferedReader reader = new BufferedReader (new FileReader("C:\\myfolder\\input.txt")); String line = reader.readLine(); while (line != null) { // Create new page when needed if (newPage == null) { newPage = pdfDoc.createPage(pageFormat); pdfDoc.addPage(newPage); g2 = newPage.createGraphics(); g2.setFont(font); fm = g2.getFontMetrics(); currentY = marginTop * 72; } if (line.length() <= 0) { // Advance to next line currentY += fm.getHeight(); line = reader.readLine(); continue; } AttributedString attrString = new AttributedString(line); attrString.addAttribute(TextAttribute.FONT, font, 0, line.length()); LineBreakMeasurer lbm = new LineBreakMeasurer(attrString.getIterator(), g2.getFontRenderContext()); int offset = 0; while (offset < line.length()) { offset = lbm.nextOffset(wrapWidth); // Draw the line g2.drawString(line.substring(lbm.getPosition(), offset), marginLeft * 72, currentY); // update the line LineBreakMeasurer position lbm.setPosition(offset); // Advance to next line currentY += fm.getHeight(); if (currentY >= ((pageHeight - marginBottom) * 72)) { newPage = pdfDoc.createPage(pageFormat); pdfDoc.addPage(newPage); g2 = newPage.createGraphics(); g2.setFont(font); fm = g2.getFontMetrics(); currentY = marginTop * 72; } } // Read the next line line = reader.readLine(); } // Close the text file reader.close(); // Save the document pdfDoc.saveDocument("C:\\myfolder\\output.pdf"); |
Note: Helvetica® is a trademark Monotype Imaging Inc.