Java Code Examples for org.apache.pdfbox.pdmodel.PDResources#getFont()
The following examples show how to use
org.apache.pdfbox.pdmodel.PDResources#getFont() .
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: PdfFontExtractor.java From FontVerter with GNU Lesser General Public License v3.0 | 6 votes |
private void extractFontResources(PDResources resources) throws IOException { for (COSName key : resources.getFontNames()) { PDFont font = resources.getFont(key); extractStrategy.extract(font); } for (COSName name : resources.getXObjectNames()) { PDXObject xobject = resources.getXObject(name); if (xobject instanceof PDFormXObject) { PDFormXObject xObjectForm = (PDFormXObject) xobject; PDResources formResources = xObjectForm.getResources(); if (formResources != null) extractFontResources(formResources); } } }
Example 2
Source File: AppearanceGeneratorHelper.java From gcs with Mozilla Public License 2.0 | 5 votes |
private void validateAndEnsureAcroFormResources() { // add font resources which might be available at the field // level but are not at the AcroForm level to the AcroForm // to match Adobe Reader/Acrobat behavior if (field.getAcroForm().getDefaultResources() == null) { return; } PDResources acroFormResources = field.getAcroForm().getDefaultResources(); for (PDAnnotationWidget widget : field.getWidgets()) { if (widget.getNormalAppearanceStream() != null && widget.getNormalAppearanceStream().getResources() != null) { PDResources widgetResources = widget.getNormalAppearanceStream().getResources(); for (COSName fontResourceName : widgetResources.getFontNames()) { try { if (acroFormResources.getFont(fontResourceName) == null) { LOG.debug("Adding font resource " + fontResourceName + " from widget to AcroForm"); acroFormResources.put(fontResourceName, widgetResources.getFont(fontResourceName)); } } catch (IOException e) { LOG.warn("Unable to match field level font with AcroForm font"); } } } } }
Example 3
Source File: PDFBoxTree.java From Pdf2Dom with GNU Lesser General Public License v3.0 | 5 votes |
private void processFontResources(PDResources resources, FontTable table) throws IOException { String fontNotSupportedMessage = "Font: {} skipped because type '{}' is not supported."; for (COSName key : resources.getFontNames()) { PDFont font = resources.getFont(key); if (font instanceof PDTrueTypeFont) { table.addEntry( font); log.debug("Font: " + font.getName() + " TTF"); } else if (font instanceof PDType0Font) { PDCIDFont descendantFont = ((PDType0Font) font).getDescendantFont(); if (descendantFont instanceof PDCIDFontType2) table.addEntry(font); else log.warn(fontNotSupportedMessage, font.getName(), font.getClass().getSimpleName()); } else if (font instanceof PDType1CFont) table.addEntry(font); else log.warn(fontNotSupportedMessage, font.getName(), font.getClass().getSimpleName()); } for (COSName name : resources.getXObjectNames()) { PDXObject xobject = resources.getXObject(name); if (xobject instanceof PDFormXObject) { PDFormXObject xObjectForm = (PDFormXObject) xobject; PDResources formResources = xObjectForm.getResources(); if (formResources != null && formResources != resources && formResources.getCOSObject() != resources.getCOSObject()) processFontResources(formResources, table); } } }
Example 4
Source File: RenderType3Character.java From testarea-pdfbox2 with Apache License 2.0 | 4 votes |
/** * <a href="http://stackoverflow.com/questions/42032729/render-type3-font-character-as-image-using-pdfbox"> * Render Type3 font character as image using PDFBox * </a> * <br/> * <a href="https://drive.google.com/file/d/0B0f6X4SAMh2KRDJTbm4tb3E1a1U/view"> * 4700198773.pdf * </a> * from * <a href="http://stackoverflow.com/questions/37754112/extract-text-with-custom-font-result-non-readble"> * extract text with custom font result non readble * </a> * <p> * This test shows how one can render individual Type 3 font glyphs as bitmaps. * Unfortunately PDFBox out-of-the-box does not provide a class to render contents * of arbitrary XObjects, merely for rendering pages; thus, we simply create a page * with the glyph in question and render that page. * </p> * <p> * As the OP did not provide a sample PDF, we simply use one from another * stackoverflow question. There obviously might remain issues with the * OP's files. * </p> */ @Test public void testRender4700198773() throws IOException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { Method PDPageContentStreamWrite = PDPageContentStream.class.getSuperclass().getDeclaredMethod("write", String.class); PDPageContentStreamWrite.setAccessible(true); try ( InputStream resource = getClass().getResourceAsStream("4700198773.pdf")) { PDDocument document = Loader.loadPDF(resource); PDPage page = document.getPage(0); PDResources pageResources = page.getResources(); COSName f1Name = COSName.getPDFName("F1"); PDType3Font fontF1 = (PDType3Font) pageResources.getFont(f1Name); Map<String, Integer> f1NameToCode = fontF1.getEncoding().getNameToCodeMap(); COSDictionary charProcsDictionary = fontF1.getCharProcs(); for (COSName key : charProcsDictionary.keySet()) { COSStream stream = (COSStream) charProcsDictionary.getDictionaryObject(key); PDType3CharProc charProc = new PDType3CharProc(fontF1, stream); PDRectangle bbox = charProc.getGlyphBBox(); if (bbox == null) bbox = charProc.getBBox(); Integer code = f1NameToCode.get(key.getName()); if (code != null) { PDDocument charDocument = new PDDocument(); PDPage charPage = new PDPage(bbox); charDocument.addPage(charPage); charPage.setResources(pageResources); PDPageContentStream charContentStream = new PDPageContentStream(charDocument, charPage); charContentStream.beginText(); charContentStream.setFont(fontF1, bbox.getHeight()); // charContentStream.write(String.format("<%2X> Tj\n", code).getBytes()); PDPageContentStreamWrite.invoke(charContentStream, String.format("<%2X> Tj\n", code)); charContentStream.endText(); charContentStream.close(); File result = new File(RESULT_FOLDER, String.format("4700198773-%s-%s.png", key.getName(), code)); PDFRenderer renderer = new PDFRenderer(charDocument); BufferedImage image = renderer.renderImageWithDPI(0, 96); ImageIO.write(image, "PNG", result); charDocument.save(new File(RESULT_FOLDER, String.format("4700198773-%s-%s.pdf", key.getName(), code))); charDocument.close(); } } } }
Example 5
Source File: RenderType3Character.java From testarea-pdfbox2 with Apache License 2.0 | 4 votes |
/** * <a href="http://stackoverflow.com/questions/42032729/render-type3-font-character-as-image-using-pdfbox"> * Render Type3 font character as image using PDFBox * </a> * <br/> * <a href="https://drive.google.com/file/d/0B0f6X4SAMh2KRDJTbm4tb3E1a1U/view"> * 4700198773.pdf * </a> * from * <a href="http://stackoverflow.com/questions/37754112/extract-text-with-custom-font-result-non-readble"> * extract text with custom font result non readble * </a> * <p> * This test shows how one can render individual Type 3 font glyphs as bitmaps. * Unfortunately PDFBox out-of-the-box does not provide a class to render contents * of arbitrary XObjects, merely for rendering pages; thus, we simply create a page * with the glyph in question and render that page. * </p> * <p> * As the OP did not provide a sample PDF, we simply use one from another * stackoverflow question. There obviously might remain issues with the * OP's files. * </p> */ @Test public void testRenderSdnList() throws IOException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException { Method PDPageContentStreamWrite = PDPageContentStream.class.getSuperclass().getDeclaredMethod("write", String.class); PDPageContentStreamWrite.setAccessible(true); try ( InputStream resource = getClass().getResourceAsStream("sdnlist.pdf")) { PDDocument document = Loader.loadPDF(resource); PDPage page = document.getPage(1); PDResources pageResources = page.getResources(); COSName f1Name = COSName.getPDFName("R144"); PDType3Font fontF1 = (PDType3Font) pageResources.getFont(f1Name); Map<String, Integer> f1NameToCode = fontF1.getEncoding().getNameToCodeMap(); COSDictionary charProcsDictionary = fontF1.getCharProcs(); for (COSName key : charProcsDictionary.keySet()) { COSStream stream = (COSStream) charProcsDictionary.getDictionaryObject(key); PDType3CharProc charProc = new PDType3CharProc(fontF1, stream); PDRectangle bbox = charProc.getGlyphBBox(); if (bbox == null) bbox = charProc.getBBox(); Integer code = f1NameToCode.get(key.getName()); if (code != null) { PDDocument charDocument = new PDDocument(); PDPage charPage = new PDPage(bbox); charDocument.addPage(charPage); charPage.setResources(pageResources); PDPageContentStream charContentStream = new PDPageContentStream(charDocument, charPage); charContentStream.beginText(); charContentStream.setFont(fontF1, bbox.getHeight()); //charContentStream.getOutputStream().write(String.format("<%2X> Tj\n", code).getBytes()); PDPageContentStreamWrite.invoke(charContentStream, String.format("<%2X> Tj\n", code)); charContentStream.endText(); charContentStream.close(); File result = new File(RESULT_FOLDER, String.format("sdnlist-%s-%s.png", key.getName(), code)); PDFRenderer renderer = new PDFRenderer(charDocument); BufferedImage image = renderer.renderImageWithDPI(0, 96); ImageIO.write(image, "PNG", result); charDocument.save(new File(RESULT_FOLDER, String.format("sdnlist-%s-%s.pdf", key.getName(), code))); charDocument.close(); } } } }