Pdfbox extract the visualization of a digital signature image. The pdfbox version is 2.0.20
Asked
Active
Viewed 496 times
-1
-
What do you mean by *the digital signature image*? Do you have a signature field with a visible appearance and you want to store that appearance somehow? Or render it as a bitmap image? Or does that appearance contain a bitmap image and you want to extract that? Or does the signer certificate contain an image attribute which you want to extract? – mkl Sep 03 '21 at 05:19
-
thanks for your answer, I mean like a seal,this seal is the visualization of a digital signature, i.e. a widget annotation of the corresponding signature field.I want to extract the visible signature as an image,such as `png,jpg`. – Mark Sep 03 '21 at 06:19
-
Do you want to *extract an existing bitmap in the widget appearance*? Or do you want to *render the whole widget appearance as a new bitmap image*? – mkl Sep 03 '21 at 08:37
-
I need extract an existing bitmap to local – Mark Sep 03 '21 at 09:06
-
Did you try ExtractImages? The current version is 2.0.24. – Tilman Hausherr Sep 03 '21 at 18:13
-
1@TilmanHausherr If I see it correctly, ExtractImages does not extract from annotation appearances. Not yet. ;) – mkl Sep 03 '21 at 19:00
-
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Sep 07 '21 at 09:07
1 Answers
0
There is a bitmap extraction tool (inspired by the PDFBox ExtractImages example) in this answer which we can easily change to inspect the page annotations instead of the static page content:
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 helper method)
You can apply it like this:
PDDocument document = ...;
extractPageAnnotationImages(document, "image-%s-%s%s.%s");
This creates the file names as image-[page: number]-[annotation: number-Down/Normal/Rollover[.variant]][reflection status].[extension]

mkl
- 90,588
- 15
- 125
- 265