Skip to content

Commit

Permalink
PDFBOX-5873: add more resources; don't save fonts twice
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1920213 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
THausherr committed Aug 27, 2024
1 parent 40576c4 commit 3a4414e
Showing 1 changed file with 56 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashSet;
import java.util.Set;

import org.apache.pdfbox.Loader;
import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageTree;
import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.pdmodel.common.PDStream;
import org.apache.pdfbox.pdmodel.font.PDCIDFont;
Expand All @@ -36,6 +40,13 @@
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import org.apache.pdfbox.pdmodel.graphics.PDXObject;
import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject;
import org.apache.pdfbox.pdmodel.graphics.form.PDTransparencyGroup;
import org.apache.pdfbox.pdmodel.graphics.pattern.PDAbstractPattern;
import org.apache.pdfbox.pdmodel.graphics.pattern.PDTilingPattern;
import org.apache.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState;
import org.apache.pdfbox.pdmodel.graphics.state.PDSoftMask;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream;

/**
* This will extract all true type-fonts of a pdf.
Expand All @@ -44,6 +55,8 @@
public final class ExtractTTFFonts
{
private int fontCounter = 1;
private final Set<COSDictionary> fontSet = new HashSet<>();
private int currentPage;

@SuppressWarnings({"squid:S2068"})
private static final String PASSWORD = "-password";
Expand Down Expand Up @@ -123,11 +136,21 @@ private void extractFonts(String[] args) throws IOException
}
try (PDDocument document = Loader.loadPDF(new File(pdfFile), password))
{
for (PDPage page : document.getPages())
PDPageTree pageTree = document.getPages();
for (PDPage page : pageTree)
{
PDResources resources = page.getResources();
currentPage = pageTree.indexOf(page) + 1;
// extract all fonts which are part of the page resources
processResources(resources, prefix, addKey);
processResources(page.getResources(), prefix, addKey);

for (PDAnnotation ann : page.getAnnotations())
{
PDAppearanceStream nas = ann.getNormalAppearanceStream();
if (nas != null)
{
processResources(nas.getResources(), prefix, addKey);
}
}
}
}
}
Expand All @@ -144,6 +167,12 @@ private void processResources(PDResources resources, String prefix, boolean addK
for (COSName key : resources.getFontNames())
{
PDFont font = resources.getFont(key);
System.out.println(font.getName() + " on page " + currentPage);
if (fontSet.contains(font.getCOSObject()))
{
continue;
}
fontSet.add(font.getCOSObject());
// write the font
if (font instanceof PDTrueTypeFont)
{
Expand Down Expand Up @@ -183,11 +212,33 @@ else if (font instanceof PDType0Font)
if (xobject instanceof PDFormXObject)
{
PDFormXObject xObjectForm = (PDFormXObject) xobject;
PDResources formResources = xObjectForm.getResources();
processResources(formResources, prefix, addKey);
processResources(xObjectForm.getResources(), prefix, addKey);
}
}

for (COSName name : resources.getPatternNames())
{
PDAbstractPattern pattern = resources.getPattern(name);
if (pattern instanceof PDTilingPattern)
{
PDTilingPattern tilingPattern = (PDTilingPattern) pattern;
processResources(tilingPattern.getResources(), prefix, addKey);
}
}

for (COSName name : resources.getExtGStateNames())
{
PDExtendedGraphicsState extGState = resources.getExtGState(name);
PDSoftMask softMask = extGState.getSoftMask();
if (softMask != null)
{
PDTransparencyGroup group = softMask.getGroup();
if (group != null)
{
processResources(group.getResources(), prefix, addKey);
}
}
}
}

private void writeFont(PDFontDescriptor fd, String name) throws IOException
Expand Down

0 comments on commit 3a4414e

Please sign in to comment.