问题
Is it possible to get the image of any digital signatures in a pdf file with itextsharp using c# code?
PdfReader pdf = new PdfReader("location.pdf");
AcroFields acroFields = pdf.AcroFields;
List<string> names = acroFields.GetSignatureNames();
foreach (var name in names)
{
PdfDictionary dict = acroFields.GetSignatureDictionary(name);
}
With this simple lines i can get the signature dictionaries but from this object i am not able to get the content of the image. Can anyone help?
回答1:
I answer my own question... if it could be usefull to someone else i did it like this. I found a Java class to do what i was looking for and I translated it in C#.
class XyzmoSignatureDataExtractor
{
private PdfReader reader;
public XyzmoSignatureDataExtractor(PdfReader reader)
{
this.reader = reader;
}
public PdfImageObject extractImage(String signatureName)
{
MyImageRenderListener listener = new MyImageRenderListener();
PdfDictionary sigFieldDic = reader.AcroFields.GetFieldItem(signatureName).GetMerged(0);
PdfDictionary appearancesDic = sigFieldDic.GetAsDict(PdfName.AP);
PdfStream normalAppearance = appearancesDic.GetAsStream(PdfName.N);
PdfDictionary resourcesDic = normalAppearance.GetAsDict(PdfName.RESOURCES);
PdfContentStreamProcessor processor = new PdfContentStreamProcessor(listener);
processor.ProcessContent(ContentByteUtils.GetContentBytesFromContentObject(normalAppearance), resourcesDic);
return listener.image;
}
class MyImageRenderListener : IRenderListener
{
public void BeginTextBlock() { }
public void EndTextBlock() { }
public void RenderImage(ImageRenderInfo renderInfo)
{
try
{
image = renderInfo.GetImage();
}
catch (Exception e)
{
throw new Exception("Failure retrieving image", e);
}
}
public void RenderText(TextRenderInfo renderInfo) { }
public PdfImageObject image = null;
}
}
To use the class and save the image i just do like that:
PdfReader reader = new PdfReader("location.pdf");
XyzmoSignatureDataExtractor extractor = new XyzmoSignatureDataExtractor(reader);
AcroFields acroFields = reader.AcroFields;
foreach (string name in acroFields.GetSignatureNames())
{
PdfImageObject image = extractor.extractImage(name);
var _image = image.GetDrawingImage();
string file_name = "sig." + image.GetFileType();
_image.Save(file_name);
}
来源:https://stackoverflow.com/questions/38430974/c-sharp-itextsharp-how-to-get-digital-signature-image