使用 PDFBox 从 PDF 中获取数字签名图像?

Use PDFBox to get a digital signature image from a PDF?

pdfbox提取数字签名图像的可视化。 pdfbox版本为2.0.20

中有一个位图提取工具​​(受 PDFBox ExtractImages 示例启发),我们可以轻松更改它以检查页面注释而不是静态页面内容:

void extractPageAnnotationImages(PDDocument document, String fileNameFormat) throws IOException
{
    int page = 1;
    for (final PDPage pdPage : document.getPages())
    {
        final int currentPage = page;
        Map<String, PDAppearanceStream> allStreams = new HashMap<>();
        int annot = 1;
        for (PDAnnotation pdAnnotation  : pdPage.getAnnotations()) {
            PDAppearanceDictionary appearancesDictionary = pdAnnotation.getAppearance();
            Map<String, PDAppearanceEntry> dictsOrStreams = Map.of("Down", appearancesDictionary.getDownAppearance(), "Normal", appearancesDictionary.getNormalAppearance(), "Rollover", appearancesDictionary.getRolloverAppearance());
            for (Map.Entry<String, PDAppearanceEntry> entry : dictsOrStreams.entrySet()) {
                if (entry.getValue().isStream()) {
                    allStreams.put(String.format("%d-%s", annot, entry.getKey()), entry.getValue().getAppearanceStream());
                } else {
                    for (Map.Entry<COSName, PDAppearanceStream> subEntry : entry.getValue().getSubDictionary().entrySet()) {
                        allStreams.put(String.format("%d-%s.%s", annot, entry.getKey(), subEntry.getKey().getName()), subEntry.getValue());
                    }
                }
            }
            annot++;
        }

        PDFGraphicsStreamEngine pdfGraphicsStreamEngine = new PDFGraphicsStreamEngine(pdPage)
        {
            String current = null;
            
            @Override
            public void processPage(PDPage page) throws IOException {
                for (Map.Entry<String,PDAppearanceStream> entry : allStreams.entrySet()) {
                    current = entry.getKey();
                    processChildStream(entry.getValue(), pdPage);
                }
            }

            @Override
            public void drawImage(PDImage pdImage) throws IOException
            {
                if (pdImage instanceof PDImageXObject)
                {
                    Matrix ctm = getGraphicsState().getCurrentTransformationMatrix();
                    String flips = "";
                    if (ctm.getScaleX() < 0)
                        flips += "h";
                    if (ctm.getScaleY() < 0)
                        flips += "v";
                    if (flips.length() > 0)
                        flips = "-" + flips;
                    PDImageXObject image = (PDImageXObject)pdImage;
                    File file = new File(String.format(fileNameFormat, currentPage, current, flips, image.getSuffix()));
                    ImageIOUtil.writeImage(image.getImage(), image.getSuffix(), new FileOutputStream(file));
                }
            }

            @Override
            public void appendRectangle(Point2D p0, Point2D p1, Point2D p2, Point2D p3) throws IOException { }

            @Override
            public void clip(int windingRule) throws IOException { }

            @Override
            public void moveTo(float x, float y) throws IOException {  }

            @Override
            public void lineTo(float x, float y) throws IOException { }

            @Override
            public void curveTo(float x1, float y1, float x2, float y2, float x3, float y3) throws IOException {  }

            @Override
            public Point2D getCurrentPoint() throws IOException { return new Point2D.Float(); }

            @Override
            public void closePath() throws IOException { }

            @Override
            public void endPath() throws IOException { }

            @Override
            public void strokePath() throws IOException { }

            @Override
            public void fillPath(int windingRule) throws IOException { }

            @Override
            public void fillAndStrokePath(int windingRule) throws IOException { }

            @Override
            public void shadingFill(COSName shadingName) throws IOException { }
        };
        pdfGraphicsStreamEngine.processPage(pdPage);
        page++;
    }
}

(ExtractImages辅助方法)

你可以这样应用:

PDDocument document = ...;
extractPageAnnotationImages(document, "image-%s-%s%s.%s");

这会创建文件名 image-[page: number]-[annotation: number-Down/Normal/Rollover[.variant]][reflection status].[extension]