Apache PDFbox is an open source java library used for manipulation of PDF’s. In this post we will see how to use the base fonts and load various font files in Apache PDFBox.
Use of Base Fonts in Apache PDFBox
PDFBox provides support for inbuilt font using the PDType1Font class. Few of the fonts supported by this class are
PDType1Font.TIMES_ROMAN
PDType1Font.TIMES_BOLD
PDType1Font.TIMES_ITALIC
PDType1Font.TIMES_BOLD_ITALIC
PDType1Font.HELVETICA
PDType1Font.HELVETICA_BOLD
PDType1Font.HELVETICA_OBLIQUE
PDType1Font.HELVETICA_BOLD_OBLIQUE
PDType1Font.COURIER Courier
PDType1Font.COURIER_BOLD
PDType1Font.COURIER_OBLIQUE
PDType1Font.COURIER_BOLD_OBLIQUE
PDType1Font.SYMBOL
Lets see an example on how to use these fonts. See the highlighted lines on how to use base fonts.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
package com.kscodes.examples.pdfbox; import java.io.IOException; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDPageContentStream; import org.apache.pdfbox.pdmodel.font.PDType1Font; public class PdfFontExample { public static void main(String args[]) { PDDocument pdDocument = new PDDocument(); PDPage pdPage = new PDPage(); pdDocument.addPage(pdPage); try { PDPageContentStream pdPageContentStream = new PDPageContentStream(pdDocument, pdPage); pdPageContentStream.beginText(); pdPageContentStream.newLineAtOffset(25, 700); // Set a Font and its Size pdPageContentStream.setFont(PDType1Font.HELVETICA, 12); pdPageContentStream.showText("This is a simple text example - kscodes"); pdPageContentStream.endText(); // Lets try a different font and size pdPageContentStream.beginText(); pdPageContentStream.newLineAtOffset(25, 600); pdPageContentStream.setFont(PDType1Font.COURIER_BOLD_OBLIQUE, 20); pdPageContentStream.showText("This is a simple text example - kscodes"); pdPageContentStream.endText(); // Once all the content is written, close the stream pdPageContentStream.close(); pdDocument.save("K:\\Kscodes\\pdf\\sample.pdf"); pdDocument.close(); System.out.println("PDF saved to the location !!!"); } catch (IOException ioe) { System.out.println("Error while saving pdf" + ioe.getMessage()); } } } |
Load various font files in Apache PDFBox
Now lets see how to load various font files in Apache PDFBox
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
package com.kscodes.examples.pdfbox; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDPageContentStream; import org.apache.pdfbox.pdmodel.font.PDFont; import org.apache.pdfbox.pdmodel.font.PDTrueTypeFont; import org.apache.pdfbox.pdmodel.font.PDType1Font; public class PdfFontExample { public static void main(String args[]) { PDDocument pdDocument = new PDDocument(); PDPage pdPage = new PDPage(); pdDocument.addPage(pdPage); try { PDPageContentStream pdPageContentStream = new PDPageContentStream(pdDocument, pdPage); pdPageContentStream.beginText(); pdPageContentStream.newLineAtOffset(25, 700); // Load a Truetype font PDFont trueTypeFont = PDTrueTypeFont.loadTTF(pdDocument, new FileInputStream(new File("trueType.ttf"))); pdPageContentStream.setFont(trueTypeFont, 12); pdPageContentStream.showText("This is a simple text example - kscodes"); pdPageContentStream.endText(); // Load a Type 0 font pdPageContentStream.beginText(); pdPageContentStream.newLineAtOffset(25, 700); PDType1Font font = new PDType1Font(pdDocument, new FileInputStream(new File("type1.ttf"))); pdPageContentStream.setFont(font, 12); pdPageContentStream.showText("This is a simple text example - kscodes"); pdPageContentStream.endText(); // Once all the content is written, close the stream pdPageContentStream.close(); pdDocument.save("K:\\Kscodes\\pdf\\sample.pdf"); pdDocument.close(); System.out.println("PDF saved to the location !!!"); } catch (IOException ioe) { System.out.println("Error while saving pdf" + ioe.getMessage()); } } } |