gpt4 book ai didi

pdf - 使用 PDFBox 从 PDF 获取可见签名?

转载 作者:行者123 更新时间:2023-12-02 02:38:08 26 4
gpt4 key购买 nike

是否可以使用OSS库PDFBox提取签名PDF的可见签名(图像)?

工作流程:

  1. 列出文件的所有签名
  2. 显示哪些签名包含可见签名
  3. 显示哪些是有效的
  4. 提取签名图像(需要为每个签名提取正确的图像)

像下面这样的 oop 风格的东西会很棒:

PDFSignatures [] sigs = document.getPDFSignatures()
sig[0].getCN()
...
(Buffered)Image visibleSig = sig[0].getVisibleSignature()

找到了 PDSignature 类以及如何对 PDF 进行签名,但没有找到将可见签名提取为图像的解决方案。

最佳答案

由于没有人回答,我自己在评论中尝试了我的建议。第一个结果:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdfviewer.PageDrawer;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.graphics.PDGraphicsState;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream;

public class AnnotationDrawer extends PageDrawer
{
public AnnotationDrawer(int imageType, int resolution) throws IOException
{
super();
this.imageType = imageType;
this.resolution = resolution;
}

public Map<String, BufferedImage> convertToImages(PDPage p) throws IOException
{
page = p;
final Map<String, BufferedImage> result = new HashMap<String, BufferedImage>();

List<PDAnnotation> annotations = page.getAnnotations();
for (PDAnnotation annotation: annotations)
{
String appearanceName = annotation.getAppearanceStream();
PDAppearanceDictionary appearDictionary = annotation.getAppearance();
if( appearDictionary != null )
{
if( appearanceName == null )
{
appearanceName = "default";
}
Map<String, PDAppearanceStream> appearanceMap = appearDictionary.getNormalAppearance();
if (appearanceMap != null)
{
PDAppearanceStream appearance =
(PDAppearanceStream)appearanceMap.get( appearanceName );
if( appearance != null )
{
BufferedImage image = initializeGraphics(annotation);
setTextMatrix(null);
setTextLineMatrix(null);
getGraphicsStack().clear();
processSubStream( page, appearance.getResources(), appearance.getStream() );

String name = annotation.getAnnotationName();
if (name == null || name.length() == 0)
{
name = annotation.getDictionary().getString(COSName.T);
if (name == null || name.length() == 0)
{
name = Long.toHexString(annotation.hashCode());
}
}

result.put(name, image);
}
}
}
}

return result;
}

BufferedImage initializeGraphics(PDAnnotation annotation)
{
PDRectangle rect = annotation.getRectangle();
float widthPt = rect.getWidth();
float heightPt = rect.getHeight();
float scaling = resolution / (float)DEFAULT_USER_SPACE_UNIT_DPI;
int widthPx = Math.round(widthPt * scaling);
int heightPx = Math.round(heightPt * scaling);
//TODO The following reduces accuracy. It should really be a Dimension2D.Float.
Dimension pageDimension = new Dimension( (int)widthPt, (int)heightPt );
BufferedImage retval = new BufferedImage( widthPx, heightPx, imageType );
Graphics2D graphics = (Graphics2D)retval.getGraphics();
graphics.setBackground( TRANSPARENT_WHITE );
graphics.clearRect( 0, 0, retval.getWidth(), retval.getHeight() );
graphics.scale( scaling, scaling );
setGraphics(graphics);
pageSize = pageDimension;
graphics.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
graphics.setRenderingHint( RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON );
setGraphicsState(new PDGraphicsState(new PDRectangle(widthPt, heightPt)));

return retval;
}

void setGraphics(Graphics2D graphics)
{
try {
Field field = PageDrawer.class.getDeclaredField("graphics");
field.setAccessible(true);
field.set(this, graphics);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

private static final int DEFAULT_USER_SPACE_UNIT_DPI = 72;
private static final Color TRANSPARENT_WHITE = new Color( 255, 255, 255, 0 );

private int imageType;
private int resolution;
}

如果您想呈现给定 PDPage 页面的注释,您只需执行以下操作:

AnnotationDrawer drawer = new AnnotationDrawer(8, 288);
Map<String, BufferedImage> images = drawer.convertToImages(page);

构造函数参数对应于 PDPage.convertToImage(int imageType, int resolution) 的参数。

当心,这有

a.基于PDFBox 1.8.2一起被黑客攻击;它可能包含特定于版本的代码;b.只是检查了我这里的一些可见的签名注释;它可能不完整,尤其对于其他注释类型可能会失败。

关于pdf - 使用 PDFBox 从 PDF 获取可见签名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16949888/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com