如何将图章注释从图像导出到图像文件?
How can I export the stamp annotation from image to an image file?
如何将“图像中的图章注释”导出到图像文件?
我有一个包含图章注释的 pdf(实际上它是一个图像),我想将所有这些类型的图像导出到文件并获得 x/y 位置。
我是 pdf 新手。任何想法或代码将不胜感激。
[--- 编辑于 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?
}
}
iText7 正是您想要使用的东西。 ;)
https://www.nuget.org/packages/itext7/
https://itextpdf.com/en/products/itext-7
这是您获得邮票的方式,具体取决于所使用的邮票技术:
using (PdfReader r = new PdfReader("input.pdf"))
using (PdfDocument doc = new PdfDocument(r))
{
var annotations = doc.GetPage(1).GetAnnotations();
}
在其他情况下,这可以解决问题:
using (PdfReader r = new PdfReader("input.pdf"))
using (PdfDocument doc = new PdfDocument(r))
{
var res = doc.GetFirstPage().GetResources();
}
除非你提供一个演示-pdf,否则我可以给你所有的帮助
@mkl @FastJack 感谢您的宝贵时间!
通过这个回答
它解决了我的问题。以下是我的代码:
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);
}
}
}
如何将“图像中的图章注释”导出到图像文件?
我有一个包含图章注释的 pdf(实际上它是一个图像),我想将所有这些类型的图像导出到文件并获得 x/y 位置。
我是 pdf 新手。任何想法或代码将不胜感激。
[--- 编辑于 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? } }
iText7 正是您想要使用的东西。 ;)
https://www.nuget.org/packages/itext7/
https://itextpdf.com/en/products/itext-7
这是您获得邮票的方式,具体取决于所使用的邮票技术:
using (PdfReader r = new PdfReader("input.pdf"))
using (PdfDocument doc = new PdfDocument(r))
{
var annotations = doc.GetPage(1).GetAnnotations();
}
在其他情况下,这可以解决问题:
using (PdfReader r = new PdfReader("input.pdf"))
using (PdfDocument doc = new PdfDocument(r))
{
var res = doc.GetFirstPage().GetResources();
}
除非你提供一个演示-pdf,否则我可以给你所有的帮助
@mkl @FastJack 感谢您的宝贵时间!
通过这个回答
它解决了我的问题。以下是我的代码:
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);
}
}
}