问题
How can I export the “stamp annotation from image” to an image file?
I have a pdf which contains stamp annotions (in fact it's an image), I want to export all these kinds of images to file and get the x/y position.
I am new to pdf. Any idea or code will be appreciated.
[--- Edited on 2019/08/08 ---]
private void btnExtractAnnotaion_Click(object sender, EventArgs e) { PdfReader reader = new PdfReader(this.txtPdf.Text); PdfDictionary pageDict = reader.GetPageN(reader.NumberOfPages); PdfArray annotArray = pageDict.GetAsArray(PdfName.ANNOTS); PdfObject annot = null; Console.WriteLine("Annotation count:{0}", annotArray.Size); for (int i = 0; i < annotArray.Size; i++) { annot = annotArray.GetDirectObject(i); Console.WriteLine(annot.ToString()); //curAnnot = annotArray.GetAsDict(i); //Console.WriteLine(curAnnot.ToString()); bool btmp = annot.IsDictionary(); if (btmp) { PdfDictionary pdfDic = ((PdfDictionary)annot); PdfName stamp = pdfDic.GetAsName(PdfName.SUBTYPE); if (stamp.Equals(PdfName.STAMP)) { //PdfObject img = pdfDic.GetDirectObject(PdfName.RECT); // How Can I get the image(png, jpg...) of Stamp? } }
回答1:
iText7 is exactly the thing you want to use for that. ;)
https://www.nuget.org/packages/itext7/
https://itextpdf.com/en/products/itext-7
This is how you could get access to the stamps, depending on the stamping technique used:
using (PdfReader r = new PdfReader("input.pdf"))
using (PdfDocument doc = new PdfDocument(r))
{
var annotations = doc.GetPage(1).GetAnnotations();
}
on other occasions this could do the trick:
using (PdfReader r = new PdfReader("input.pdf"))
using (PdfDocument doc = new PdfDocument(r))
{
var res = doc.GetFirstPage().GetResources();
}
unless you provide a demo-pdf, that all the help i can give you
回答2:
@mkl @FastJack Thanks for your time! By this answer ImageRenderListener
It solved my problem. The following are my codes:
PdfReader reader = new PdfReader(this.txtPdf.Text);
PdfDictionary pageDict = reader.GetPageN(reader.NumberOfPages);
PdfArray annotArray = pageDict.GetAsArray(PdfName.ANNOTS);
//Console.WriteLine("Annotation count:{0}", annotArray.Size);
for (int i = 0; i < annotArray.Size; i++)
{
PdfObject annot = annotArray.GetDirectObject(i);
//Console.WriteLine(annot.ToString());
bool btmp = annot.IsDictionary();
if (btmp)
{
PdfDictionary pdfDic = ((PdfDictionary)annot);
PdfName stamp = pdfDic.GetAsName(PdfName.SUBTYPE);
if (stamp.Equals(PdfName.STAMP))
{
// rects are laid out [llx, lly, urx, ury]
float x, y, width, height;
PdfArray rect = pdfDic.GetAsArray(PdfName.RECT);
x = rect.GetAsNumber(0).FloatValue;
y = rect.GetAsNumber(1).FloatValue;
width = rect.GetAsNumber(2).FloatValue - x;
height = rect.GetAsNumber(3).FloatValue - y;
PdfDictionary appearancesDic = pdfDic.GetAsDict(PdfName.AP);
PdfStream normalAppearance = appearancesDic.GetAsStream(PdfName.N);
PdfDictionary resourcesDic = ormalAppearance.GetAsDict(PdfName.RESOURCES);
ImageRenderListener listener = new ImageRenderListener();
PdfContentStreamProcessor processor = new PdfContentStreamProcessor(listener);
processor.ProcessContent(
ContentByteUtils.GetContentBytesFromContentObject(normalAppearance), resourcesDic);
System.Drawing.Image drawingImage = listener.Images.First();
//Image image = Image.GetInstance(drawingImage, drawingImage.RawFormat);
}
}
}
来源:https://stackoverflow.com/questions/57321944/how-can-i-export-the-stamp-annotation-from-image-to-an-image-file