org.apache.pdfbox.pdmodel.font.PDTrueTypeFont Java Examples

The following examples show how to use org.apache.pdfbox.pdmodel.font.PDTrueTypeFont. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AddSpecialCharacterWithoutEmbedding.java    From testarea-pdfbox2 with Apache License 2.0 6 votes vote down vote up
/**
 * <a href="https://stackoverflow.com/questions/57295834/without-embeded-fonts-is-pdf-limited-to-only-4281-characters-of-agl-how-to-d">
 * Without embeded fonts, is PDF limited to only 4281 characters (of AGL)? How to display more glyphs?
 * </a>
 * <p>
 * This test constructs a type 1 font without embedded font program
 * that uses a non AGL name in its encoding's Differences array to
 * provide a glyph not to be found on the AGL. This is a counterexample
 * to the OP's original question.
 * </p>
 */
@Test
public void testWithArial() throws IOException {
    try (   PDDocument doc = new PDDocument();
            FileOutputStream os = new FileOutputStream(new File(RESULT_FOLDER, "SpecialCharacterWithArial.pdf"))    ) {
        COSDictionary fontDict = buildUnembeddedArialWithSpecialEncoding();
        PDTrueTypeFont font = new PDTrueTypeFont(fontDict);
        PDPage page = new PDPage();
        doc.addPage(page);
        try (   PDPageContentStream contentStream = new PDPageContentStream(doc, page)) {
            contentStream.beginText();
            contentStream.setFont(font, 24);
            contentStream.newLineAtOffset(30, 600);
            contentStream.appendRawCommands("( ) Tj\n");
            contentStream.endText();
        }

        doc.save(os);
    }
}
 
Example #2
Source File: PDFUtil.java    From ctsms with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static PDFont loadFont(String fontFileName, PDDocument doc, PDFont defaultBaseFont) throws IOException {
	PDFont font = null;
	if (fontFileName != null && fontFileName.length() > 0) {
		font = PDType1Font.getStandardFont(fontFileName);
		if (font == null) {
			font = PDTrueTypeFont.loadTTF(doc, fontFileName);
			// http://stackoverflow.com/questions/5570225/workaround-for-pdfbox-pdtruetypefont-bad-widths-bug
		}
	} else {
		font = defaultBaseFont;
	}
	return font;
}
 
Example #3
Source File: TTFGlyph2D.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
private int getGIDForCharacterCode(int code) throws IOException
{
    if (isCIDFont)
    {
        return ((PDType0Font)font).codeToGID(code);
    }
    else
    {
        return ((PDTrueTypeFont)font).codeToGID(code);
    }
}
 
Example #4
Source File: PdfUtilExport.java    From zest-writer with GNU General Public License v3.0 5 votes vote down vote up
private void addCoverpage() throws IOException {
    float leading = 1.5f * FONT_SIZE_TITLE;
    PDDocument document = new PDDocument();
    PDPage page = new PDPage();
    document.addPage(page);
    FONT_STYLE_COVER = PDTrueTypeFont.loadTTF(document, MainApp.class.getResourceAsStream(FONT_MERRIWEATHER_BOLD));
    PDPageContentStream contentStream = new PDPageContentStream(document, page);
    contentStream.setNonStrokingColor(25, 81, 107);
    contentStream.fillRect(0, 0, page.getMediaBox().getWidth(), (page.getMediaBox().getHeight() / 2) - 10);
    contentStream.fillRect(0, (page.getMediaBox().getHeight() / 2) + 10, page.getMediaBox().getWidth(), (page.getMediaBox().getHeight() / 2) - 10);
    contentStream.setNonStrokingColor(248, 173, 50);
    contentStream.fillRect(0, (page.getMediaBox().getHeight() / 2) - 10, page.getMediaBox().getWidth(), 20);

    contentStream.beginText();
    contentStream.setNonStrokingColor(Color.WHITE);
    contentStream.setFont(FONT_STYLE_COVER, FONT_SIZE_AUTHOR);
    contentStream.newLineAtOffset(20, 20);
    contentStream.showText(authorContent);
    contentStream.setFont(FONT_STYLE_COVER, FONT_SIZE_TITLE);
    contentStream.newLineAtOffset((page.getMediaBox().getWidth() / 2) - 20, 600);
    List<String> lines = wrapText((page.getMediaBox().getWidth() / 2) - 20);
    for (String line : lines) {
        contentStream.showText(line);
        contentStream.newLineAtOffset(0, -leading);
    }
    contentStream.endText();

    contentStream.close();
    File temp = File.createTempFile("coverpage-zds", ".pdf");
    document.save(temp);
    document.close();

    PDFMergerUtility mergerUtility = new PDFMergerUtility();
    mergerUtility.addSource(temp);
    mergerUtility.addSource(destPdfPath);
    mergerUtility.setDestinationFileName(destPdfPath);
    mergerUtility.mergeDocuments();
}
 
Example #5
Source File: TTFGlyph2D.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Constructor.
 *
 * @param ttfFont TrueType font
 */
TTFGlyph2D(PDTrueTypeFont ttfFont) throws IOException
{
    this(ttfFont.getTrueTypeFont(), ttfFont, false);
    vectorFont = ttfFont;
}
 
Example #6
Source File: PageDrawer.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Provide a Glyph2D for the given font.
 * 
 * @param font the font
 * @return the implementation of the Glyph2D interface for the given font
 * @throws IOException if something went wrong
 */
private Glyph2D createGlyph2D(PDFont font) throws IOException
{
    Glyph2D glyph2D = fontGlyph2D.get(font);
    // Is there already a Glyph2D for the given font?
    if (glyph2D != null)
    {
        return glyph2D;
    }

    if (font instanceof PDTrueTypeFont)
    {
        PDTrueTypeFont ttfFont = (PDTrueTypeFont)font;
        glyph2D = new TTFGlyph2D(ttfFont);  // TTF is never null
    }
    else if (font instanceof PDType1Font)
    {
        PDType1Font pdType1Font = (PDType1Font)font;
        glyph2D = new Type1Glyph2D(pdType1Font); // T1 is never null
    }
    else if (font instanceof PDType1CFont)
    {
        PDType1CFont type1CFont = (PDType1CFont)font;
        glyph2D = new Type1Glyph2D(type1CFont);
    }
    else if (font instanceof PDType0Font)
    {
        PDType0Font type0Font = (PDType0Font) font;
        if (type0Font.getDescendantFont() instanceof PDCIDFontType2)
        {
            glyph2D = new TTFGlyph2D(type0Font); // TTF is never null
        }
        else if (type0Font.getDescendantFont() instanceof PDCIDFontType0)
        {
            // a Type0 CIDFont contains CFF font
            PDCIDFontType0 cidType0Font = (PDCIDFontType0)type0Font.getDescendantFont();
            glyph2D = new CIDType0Glyph2D(cidType0Font); // todo: could be null (need incorporate fallback)
        }
    }
    else
    {
        throw new IllegalStateException("Bad font type: " + font.getClass().getSimpleName());
    }

    // cache the Glyph2D instance
    if (glyph2D != null)
    {
        fontGlyph2D.put(font, glyph2D);
    }

    if (glyph2D == null)
    {
        // todo: make sure this never happens
        throw new UnsupportedOperationException("No font for " + font.getName());
    }

    return glyph2D;
}