org.apache.pdfbox.pdmodel.PDResources Java Examples

The following examples show how to use org.apache.pdfbox.pdmodel.PDResources. 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: PdfContentImagePreprocessor.java    From tika-server with Apache License 2.0 7 votes vote down vote up
private void processImagesFromResources(PDResources resources) throws IOException {
    for (COSName xObjectName : resources.getXObjectNames()) {
        PDXObject xObject = resources.getXObject(xObjectName);

        if (xObject instanceof PDFormXObject) {
            processImagesFromResources(((PDFormXObject) xObject).getResources());
        } else if (xObject instanceof PDImageXObject) {
            PDImageXObject img = (PDImageXObject) xObject;
            if (!img.getImage().getColorModel().hasAlpha())
                return;

            PDImageXObject cpy = makeImageObjectCopy(img);
            resources.put(xObjectName, cpy);
            imagesWereChanged = true;
        }
    }
}
 
Example #2
Source File: PDVisibleSigBuilder.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public void createImageForm(PDResources imageFormResources, PDResources innerFormResource,
                            PDStream imageFormStream, PDRectangle bbox, AffineTransform at,
                            PDImageXObject img) throws IOException
{
    PDFormXObject imageForm = new PDFormXObject(imageFormStream);
    imageForm.setBBox(bbox);
    imageForm.setMatrix(at);
    imageForm.setResources(imageFormResources);
    imageForm.setFormType(1);

    imageFormResources.getCOSObject().setDirect(true);

    COSName imageFormName = COSName.getPDFName("n2");
    innerFormResource.put(imageFormName, imageForm);
    COSName imageName = imageFormResources.add(img, "img");
    pdfStructure.setImageForm(imageForm);
    pdfStructure.setImageFormName(imageFormName);
    pdfStructure.setImageName(imageName);
    LOG.info("Created image form");
}
 
Example #3
Source File: PDImageXObject.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Creates an Image XObject with the given stream as its contents and current color spaces. This
 * constructor is for internal PDFBox use and is not for PDF generation. Users who want to
 * create images should look at {@link #createFromFileByExtension(File, PDDocument) }.
 *
 * @param stream the XObject stream to read
 * @param resources the current resources
 * @throws java.io.IOException if there is an error creating the XObject.
 */
public PDImageXObject(PDStream stream, PDResources resources) throws IOException
{
    super(stream, COSName.IMAGE);
    this.resources = resources;
    List<COSName> filters = stream.getFilters();
    if (filters != null && !filters.isEmpty() && COSName.JPX_DECODE.equals(filters.get(filters.size()-1)))
    {
        COSInputStream is = null;
        try
        {
            is = stream.createInputStream();
            DecodeResult decodeResult = is.getDecodeResult();
            stream.getCOSObject().addAll(decodeResult.getParameters());
            this.colorSpace = decodeResult.getJPXColorSpace();
        }
        finally
        {
            IOUtils.closeQuietly(is);
        }
    }
}
 
Example #4
Source File: AppearanceGeneratorHelper.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
private PDAppearanceStream prepareNormalAppearanceStream(PDAnnotationWidget widget)
{
    PDAppearanceStream appearanceStream = new PDAppearanceStream(field.getAcroForm().getDocument());

    // Calculate the entries for the bounding box and the transformation matrix
    // settings for the appearance stream
    int rotation = resolveRotation(widget);
    PDRectangle rect = widget.getRectangle();
    Matrix matrix = Matrix.getRotateInstance(Math.toRadians(rotation), 0, 0);
    Point2D.Float point2D = matrix.transformPoint(rect.getWidth(), rect.getHeight());

    PDRectangle bbox = new PDRectangle(Math.abs((float) point2D.getX()), Math.abs((float) point2D.getY()));
    appearanceStream.setBBox(bbox);

    AffineTransform at = calculateMatrix(bbox, rotation);
    if (!at.isIdentity())
    {
        appearanceStream.setMatrix(at);
    }
    appearanceStream.setFormType(1);
    appearanceStream.setResources(new PDResources());
    return appearanceStream;
}
 
Example #5
Source File: PDFStreamEngine.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Process a content stream.
 *
 * @param contentStream the content stream
 * @throws IOException if there is an exception while processing the stream
 */
private void processStream(PDContentStream contentStream) throws IOException
{
    PDResources parent = pushResources(contentStream);
    Deque<PDGraphicsState> savedStack = saveGraphicsStack();
    Matrix parentMatrix = initialMatrix;

    // transform the CTM using the stream's matrix
    getGraphicsState().getCurrentTransformationMatrix().concatenate(contentStream.getMatrix());

    // the stream's initial matrix includes the parent CTM, e.g. this allows a scaled form
    initialMatrix = getGraphicsState().getCurrentTransformationMatrix().clone();

    // clip to bounding box
    PDRectangle bbox = contentStream.getBBox();
    clipToRect(bbox);

    processStreamOperators(contentStream);

    initialMatrix = parentMatrix;
    restoreGraphicsStack(savedStack);
    popResources(parent);
}
 
Example #6
Source File: PdfFontExtractor.java    From FontVerter with GNU Lesser General Public License v3.0 6 votes vote down vote up
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 #7
Source File: ExtractText.java    From testarea-pdfbox2 with Apache License 2.0 6 votes vote down vote up
/**
 * <a href="https://stackoverflow.com/questions/45895768/pdfbox-2-0-7-extracttext-not-working-but-1-8-13-does-and-pdfreader-as-well">
 * PDFBox 2.0.7 ExtractText not working but 1.8.13 does and PDFReader as well
 * </a>
 * <br/>
 * <a href="https://wetransfer.com/downloads/214674449c23713ee481c5a8f529418320170827201941/b2bea6">
 * test-2.pdf
 * </a>
 * <p>
 * Due to the broken <b>ToUnicode</b> maps the output of immediate text
 * extraction from this document is unsatisfying, cf. {@link #testTest2()}.
 * It can be improved by removing these <b>ToUnicode</b> maps as this test
 * shows.
 * </p>
 */
@Test
public void testNoToUnicodeTest2() throws IOException
{
    try (   InputStream resource = getClass().getResourceAsStream("test-2.pdf")    )
    {
        PDDocument document = Loader.loadPDF(resource);

        for (int pageNr = 0; pageNr < document.getNumberOfPages(); pageNr++)
        {
            PDPage page = document.getPage(pageNr);
            PDResources resources = page.getResources();
            removeToUnicodeMaps(resources);
        }

        PDFTextStripper stripper = new PDFTextStripper();
        String text = stripper.getText(document);

        System.out.printf("\n*\n* test-2.pdf without ToUnicode\n*\n%s\n", text);
        Files.write(new File(RESULT_FOLDER, "test-2_NoToUnicode.txt").toPath(), Collections.singleton(text));
    }
}
 
Example #8
Source File: PDAcroForm.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Check if there needs to be a scaling transformation applied.
 * 
 * @param annotation
 * @param rotation 
 * @return the need for a scaling transformation.
 */    
private boolean resolveNeedsScaling(PDAnnotation annotation, int rotation)
{
    PDAppearanceStream appearanceStream = annotation.getNormalAppearanceStream();
    // Check if there is a transformation within the XObjects content
    PDResources resources = appearanceStream.getResources();
    if (resources != null && resources.getXObjectNames().iterator().hasNext())
    {
        return true;
    }
    PDRectangle bbox = appearanceStream.getBBox();
    PDRectangle fieldRect = annotation.getRectangle();
    if (rotation == 90 || rotation == 270)
    {
        return Float.compare(bbox.getWidth(),  fieldRect.getHeight()) != 0 ||
               Float.compare(bbox.getHeight(), fieldRect.getWidth()) != 0;
    }
    else
    {
        return Float.compare(bbox.getWidth(),  fieldRect.getWidth()) != 0 ||
               Float.compare(bbox.getHeight(), fieldRect.getHeight()) != 0;
    }
}
 
Example #9
Source File: PDFormXObject.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * This will get the resources for this Form XObject.
 * This will return null if no resources are available.
 * 
 * @return The resources for this Form XObject.
 */
@Override
public PDResources getResources()
{
    COSDictionary resources = getCOSObject().getCOSDictionary(COSName.RESOURCES);
    if (resources != null)
    {
        return new PDResources(resources, cache);
    }
    if (getCOSObject().containsKey(COSName.RESOURCES))
    {
        // PDFBOX-4372 if the resource key exists but has nothing, return empty resources,
        // to avoid a self-reference (xobject form Fm0 contains "/Fm0 Do")
        // See also the mention of PDFBOX-1359 in PDFStreamEngine
        return new PDResources();
    }
    return null;
}
 
Example #10
Source File: PDFRenderer.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
private boolean hasBlendMode(PDPage page)
{
    // check the current resources for blend modes
    PDResources resources = page.getResources();
    if (resources == null)
    {
        return false;
    }
    for (COSName name : resources.getExtGStateNames())
    {
        PDExtendedGraphicsState extGState = resources.getExtGState(name);
        if (extGState == null)
        {
            // can happen if key exists but no value 
            // see PDFBOX-3950-23EGDHXSBBYQLKYOKGZUOVYVNE675PRD.pdf
            continue;
        }
        BlendMode blendMode = extGState.getBlendMode();
        if (blendMode != BlendMode.NORMAL)
        {
            return true;
        }
    }
    return false;
}
 
Example #11
Source File: PDDefaultAppearanceString.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Constructor for reading an existing DA string.
 * 
 * @param defaultResources DR entry
 * @param defaultAppearance DA entry
 * @throws IOException If the DA could not be parsed
 */
PDDefaultAppearanceString(COSString defaultAppearance, PDResources defaultResources) throws IOException
{
    if (defaultAppearance == null)
    {
        throw new IllegalArgumentException("/DA is a required entry");
    }
    
    if (defaultResources == null)
    {
        throw new IllegalArgumentException("/DR is a required entry");
    }
    
    this.defaultResources = defaultResources;
    processAppearanceStringOperators(defaultAppearance.getBytes());
}
 
Example #12
Source File: PDVisibleSigBuilder.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void createHolderFormResources()
{
    PDResources holderFormResources = new PDResources();
    pdfStructure.setHolderFormResources(holderFormResources);
    LOG.info("Holder form resources have been created");

}
 
Example #13
Source File: PDVisibleSigBuilder.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void createHolderForm(PDResources holderFormResources, PDStream holderFormStream,
                             PDRectangle bbox)
{
    PDFormXObject holderForm = new PDFormXObject(holderFormStream);
    holderForm.setResources(holderFormResources);
    holderForm.setBBox(bbox);
    holderForm.setFormType(1);
    pdfStructure.setHolderForm(holderForm);
    LOG.info("Holder form has been created");

}
 
Example #14
Source File: PDVisibleSigBuilder.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void createInnerFormResource()
{
    PDResources innerFormResources = new PDResources();
    pdfStructure.setInnerFormResources(innerFormResources);
    LOG.info("Resources of another form (inner form - it will be inside holder form)" +
             "have been created");
}
 
Example #15
Source File: PDVisibleSigBuilder.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void insertInnerFormToHolderResources(PDFormXObject innerForm,
                                             PDResources holderFormResources)
{
    holderFormResources.put(COSName.FRM, innerForm);
    pdfStructure.setInnerFormName(COSName.FRM);
    LOG.info("Now inserted inner form inside holder form");
}
 
Example #16
Source File: PDType3Font.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Returns the optional resources of the type3 stream.
 *
 * @return the resources bound to be used when parsing the type3 stream
 */
public PDResources getResources()
{
    if (resources == null)
    {
        COSBase base = dict.getDictionaryObject(COSName.RESOURCES);
        if (base instanceof COSDictionary)
        {
            this.resources = new PDResources((COSDictionary) base, resourceCache);
        }
    }
    return resources;
}
 
Example #17
Source File: PDVisibleSigBuilder.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void createImageFormResources()
{
    PDResources imageFormResources = new PDResources();
    pdfStructure.setImageFormResources(imageFormResources);
    LOG.info("Created image form resources");
}
 
Example #18
Source File: PDVisibleSigBuilder.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void createInnerForm(PDResources innerFormResources,
                            PDStream innerFormStream,
                            PDRectangle bbox)
{
    PDFormXObject innerForm = new PDFormXObject(innerFormStream);
    innerForm.setResources(innerFormResources);
    innerForm.setBBox(bbox);
    innerForm.setFormType(1);
    pdfStructure.setInnerForm(innerForm);
    LOG.info("Another form (inner form - it will be inside holder form) has been created");
}
 
Example #19
Source File: PDPattern.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Creates a new uncolored tiling pattern color space.
 * 
 * @param resources The current resources.
 * @param colorSpace The underlying color space.
 */
public PDPattern(PDResources resources, PDColorSpace colorSpace)
{
    this.resources = resources;
    this.underlyingColorSpace = colorSpace;
    array = new COSArray();
    array.add(COSName.PATTERN);
    array.add(colorSpace);
}
 
Example #20
Source File: PDVisibleSigBuilder.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void createBackgroundLayerForm(PDResources innerFormResource, PDRectangle bbox)
         throws IOException
{
    // create blank n0 background layer form
    PDFormXObject n0Form = new PDFormXObject(pdfStructure.getTemplate().getDocument().createCOSStream());
    n0Form.setBBox(bbox);
    n0Form.setResources(new PDResources());
    n0Form.setFormType(1);
    innerFormResource.put(COSName.getPDFName("n0"), n0Form);
    LOG.info("Created background layer form");
}
 
Example #21
Source File: PDVisibleSigBuilder.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void injectProcSetArray(PDFormXObject innerForm, PDPage page,
                               PDResources innerFormResources,  PDResources imageFormResources,
                               PDResources holderFormResources, COSArray procSet)
{
    innerForm.getResources().getCOSObject().setItem(COSName.PROC_SET, procSet);
    page.getCOSObject().setItem(COSName.PROC_SET, procSet);
    innerFormResources.getCOSObject().setItem(COSName.PROC_SET, procSet);
    imageFormResources.getCOSObject().setItem(COSName.PROC_SET, procSet);
    holderFormResources.getCOSObject().setItem(COSName.PROC_SET, procSet);
    LOG.info("Inserted ProcSet to PDF");
}
 
Example #22
Source File: PDVisibleSigBuilder.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void createWidgetDictionary(PDSignatureField signatureField,
                                   PDResources holderFormResources) throws IOException
{
    COSDictionary widgetDict = signatureField.getWidgets().get(0).getCOSObject();
    widgetDict.setNeedToBeUpdated(true);
    widgetDict.setItem(COSName.DR, holderFormResources.getCOSObject());

    pdfStructure.setWidgetDictionary(widgetDict);
    LOG.info("WidgetDictionary has been created");
}
 
Example #23
Source File: Overlay.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
private COSName createOverlayXObject(PDPage page, LayoutPage layoutPage)
{
    PDFormXObject xobjForm = new PDFormXObject(layoutPage.overlayContentStream);
    xobjForm.setResources(new PDResources(layoutPage.overlayResources));
    xobjForm.setFormType(1);
    xobjForm.setBBox(layoutPage.overlayMediaBox.createRetranslatedRectangle());
    AffineTransform at = new AffineTransform();
    switch (layoutPage.overlayRotation)
    {
        case 90:
            at.translate(0, layoutPage.overlayMediaBox.getWidth());
            at.rotate(Math.toRadians(-90));
            break;
        case 180:
            at.translate(layoutPage.overlayMediaBox.getWidth(), layoutPage.overlayMediaBox.getHeight());
            at.rotate(Math.toRadians(-180));
            break;
        case 270:
            at.translate(layoutPage.overlayMediaBox.getHeight(), 0);
            at.rotate(Math.toRadians(-270));
            break;
        default:
            break;
    }
    xobjForm.setMatrix(at);
    PDResources resources = page.getResources();
    return resources.add(xobjForm, "OL");
}
 
Example #24
Source File: PDFStreamEngine.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Processes a transparency group stream.
 * 
 * @param group the transparency group.
 * 
 * @throws IOException
 */
protected void processTransparencyGroup(PDTransparencyGroup group) throws IOException
{
    if (currentPage == null)
    {
        throw new IllegalStateException("No current page, call " +
                "#processChildStream(PDContentStream, PDPage) instead");
    }

    PDResources parent = pushResources(group);
    Deque<PDGraphicsState> savedStack = saveGraphicsStack();
    
    Matrix parentMatrix = initialMatrix;

    // the stream's initial matrix includes the parent CTM, e.g. this allows a scaled form
    initialMatrix = getGraphicsState().getCurrentTransformationMatrix().clone();

    // transform the CTM using the stream's matrix
    getGraphicsState().getCurrentTransformationMatrix().concatenate(group.getMatrix());

    // Before execution of the transparency group XObject’s content stream, 
    // the current blend mode in the graphics state shall be initialized to Normal, 
    // the current stroking and nonstroking alpha constants to 1.0, and the current soft mask to None.
    getGraphicsState().setBlendMode(BlendMode.NORMAL);
    getGraphicsState().setAlphaConstant(1);
    getGraphicsState().setNonStrokeAlphaConstant(1);
    getGraphicsState().setSoftMask(null);

    // clip to bounding box
    clipToRect(group.getBBox());

    processStreamOperators(group);
    
    initialMatrix = parentMatrix;

    restoreGraphicsStack(savedStack);
    popResources(parent);
}
 
Example #25
Source File: PdfFontExtractor.java    From FontVerter with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected void tryExtractPageFonts() {
    PDResources resources = pdpage.getResources();
    if (resources == null)
        return;

    try {
        extractFontResources(resources);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example #26
Source File: ExtractText.java    From testarea-pdfbox2 with Apache License 2.0 5 votes vote down vote up
void removeToUnicodeMaps(PDResources pdResources) throws IOException
{
    COSDictionary resources = pdResources.getCOSObject();

    COSDictionary fonts = asDictionary(resources, COSName.FONT);
    if (fonts != null)
    {
        for (COSBase object : fonts.getValues())
        {
            while (object instanceof COSObject)
                object = ((COSObject)object).getObject();
            if (object instanceof COSDictionary)
            {
                COSDictionary font = (COSDictionary)object;
                font.removeItem(COSName.TO_UNICODE);
            }
        }
    }

    for (COSName name : pdResources.getXObjectNames())
    {
        PDXObject xobject = pdResources.getXObject(name);
        if (xobject instanceof PDFormXObject)
        {
            PDResources xobjectPdResources = ((PDFormXObject)xobject).getResources();
            removeToUnicodeMaps(xobjectPdResources);
        }
    }
}
 
Example #27
Source File: ExtractImages.java    From testarea-pdfbox2 with Apache License 2.0 5 votes vote down vote up
/**
 * <a href="http://stackoverflow.com/questions/40531871/how-can-i-check-if-pdf-page-is-imagescanned-by-pdfbox-xpdf">
 * How can I check if PDF page is image(scanned) by PDFBOX, XPDF
 * </a>
 * <br/>
 * <a href="https://drive.google.com/file/d/0B9izTHWJQ7xlT2ZoQkJfbGRYcFE">
 * 10948.pdf
 * </a>
 * <p>
 * The only special thing about the two images returned for the sample PDF is that
 * one image is merely a mask used for the other image, and the other image is the
 * actual image used on the PDF page. If one only wants the images immediately used
 * in the page content, one also has to scan the page content.
 * </p>
 */
@Test
public void testExtractPageImageResources10948() throws IOException
{
    try (   InputStream resource = getClass().getResourceAsStream("10948.pdf"))
    {
        PDDocument document = Loader.loadPDF(resource);
        int page = 1;
        for (PDPage pdPage : document.getPages())
        {
            PDResources resources = pdPage.getResources();
            if (resource != null)
            {
                int index = 0;
                for (COSName cosName : resources.getXObjectNames())
                {
                    PDXObject xobject = resources.getXObject(cosName);
                    if (xobject instanceof PDImageXObject)
                    {
                        PDImageXObject image = (PDImageXObject)xobject;
                        File file = new File(RESULT_FOLDER, String.format("10948-%s-%s.%s", page, index, image.getSuffix()));
                        ImageIO.write(image.getImage(), image.getSuffix(), file);
                        index++;
                    }
                }
            }
            page++;
        }
    }
}
 
Example #28
Source File: ExtractImages.java    From testarea-pdfbox2 with Apache License 2.0 5 votes vote down vote up
/**
 * <a href="http://stackoverflow.com/questions/40531871/how-can-i-check-if-pdf-page-is-imagescanned-by-pdfbox-xpdf">
 * How can I check if PDF page is image(scanned) by PDFBOX, XPDF
 * </a>
 * <br/>
 * <a href="https://drive.google.com/open?id=0B9izTHWJQ7xlYi1XN1BxMmZEUGc">
 * 10948.pdf
 * </a>, renamed "10948-new.pdf" here to prevent a collision
 * <p>
 * Here the code extracts no image at all because the images are not immediate page
 * resources but wrapped in form xobjects.
 * </p>
 */
@Test
public void testExtractPageImageResources10948New() throws IOException
{
    try (   InputStream resource = getClass().getResourceAsStream("10948-new.pdf"))
    {
        PDDocument document = Loader.loadPDF(resource);
        int page = 1;
        for (PDPage pdPage : document.getPages())
        {
            PDResources resources = pdPage.getResources();
            if (resource != null)
            {
                int index = 0;
                for (COSName cosName : resources.getXObjectNames())
                {
                    PDXObject xobject = resources.getXObject(cosName);
                    if (xobject instanceof PDImageXObject)
                    {
                        PDImageXObject image = (PDImageXObject)xobject;
                        File file = new File(RESULT_FOLDER, String.format("10948-new-%s-%s.%s", page, index, image.getSuffix()));
                        ImageIO.write(image.getImage(), image.getSuffix(), file);
                        index++;
                    }
                }
            }
            page++;
        }
    }
}
 
Example #29
Source File: PDFBoxTree.java    From Pdf2Dom with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Updates the font table by adding new fonts used at the current page.
 */
protected void updateFontTable()
{
    PDResources resources = pdpage.getResources();
    if (resources != null)
    {
        try
        {
            processFontResources(resources, fontTable);
        } catch (IOException e) {
            log.error("Error processing font resources: "
                    + "Exception: {} {}", e.getMessage(), e.getClass());
        }
    }
}
 
Example #30
Source File: PDFBoxTree.java    From Pdf2Dom with GNU Lesser General Public License v3.0 5 votes vote down vote up
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);
        }
    }

}