我需要将图像添加到pdf文档中,以后可以根据需要删除图像。
我设法使用以下代码将图像添加到文档中:
iText.Kernel.Geom.Rectangle pagesize;
PdfDocument pdfDoc = new PdfDocument(new PdfReader(sourceFile), new PdfWriter(destinationPath));
int n = pdfDoc.GetNumberOfPages();
ImageData img = ImageDataFactory.Create(image_WM_Path);
for (int i = 1; i <= n; i++)
{
pagesize = pdfDoc.GetPage(i).GetPageSizeWithRotation();
float f = pagesize.GetWidth() / (1.5f * img.GetWidth());
float wImage = img.GetWidth() * f;
float hImage = img.GetHeight() * f;
iText.Kernel.Geom.Rectangle rectangle = new iText.Kernel.Geom.Rectangle(pagesize.GetRight() - wImage, pagesize.GetBottom(),wImage, hImage );
PdfStampAnnotation stamp = new PdfStampAnnotation(rectangle);
stamp.SetStampName(new PdfName("MyCustomStamp"));
PdfFormXObject xObj = new PdfFormXObject(new iText.Kernel.Geom.Rectangle(wImage, hImage));
PdfCanvas canvas = new PdfCanvas(xObj, pdfDoc);
canvas.AddImage(img, 0, 0, wImage, false);
stamp.SetNormalAppearance(xObj.GetPdfObject());
pdfDoc.GetPage(i).AddAnnotation(stamp);
}
pdfDoc.Close();
添加图像作为注释是否正确? 我真的用SetStampName方法放置一个id吗?
我无法在页面的PdfDictionary注释数组中检索注释,该数组为null:
PdfArray annotations = pageDict.GetAsArray(PdfName.Annotation);
感谢您的帮助