Pdf 如何使用iTextSharp使盖章图像不可编辑?
我的目标是在3D PDF上标记一个像水印一样的图像(最终用户无法选择、编辑、调整大小或删除图像) 我试着做一个如下所示的注释,但是图像(“ClassificationBlock.png”在参考资料中)可以在输出PDF上调整大小并删除。这是“PDFanRotation”矩形的固有行为,还是我可以定义一个属性来保持图像本质上是只读的Pdf 如何使用iTextSharp使盖章图像不可编辑?,pdf,itext,watermark,pdfstamper,pdf-annotations,Pdf,Itext,Watermark,Pdfstamper,Pdf Annotations,我的目标是在3D PDF上标记一个像水印一样的图像(最终用户无法选择、编辑、调整大小或删除图像) 我试着做一个如下所示的注释,但是图像(“ClassificationBlock.png”在参考资料中)可以在输出PDF上调整大小并删除。这是“PDFanRotation”矩形的固有行为,还是我可以定义一个属性来保持图像本质上是只读的 using (PdfStamper stamp = new PdfStamper(reader, fs)) 。 . 我也尝试过通过pdfContentBytes模仿
using (PdfStamper stamp = new PdfStamper(reader, fs))
。
.
我也尝试过通过pdfContentBytes模仿其他用户尝试水印文本,但我甚至无法让图像显示在PDF上
stamp.FormFlattening = false;
iTextSharp.text.Rectangle pageRectangle = reader.GetPageSizeWithRotation(1);
PdfContentByte pdfData = stamp.GetOverContent(1);
pdfData.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 10);
PdfGState graphicsState = new PdfGState();
graphicsState.FillOpacity = 0.5F;
pdfData.SetGState(graphicsState);
pdfData.BeginText();
System.Drawing.Image imageBTM2 = System.Drawing.Image.FromHbitmap(Properties.Resources.TEKLAPDF_InstructionBlock.GetHbitmap());
iTextSharp.text.Image stampImage2 = iTextSharp.text.Image.GetInstance(imageBTM2, System.Drawing.Imaging.ImageFormat.Png);
float width = pageRectangle.Width;
float height = pageRectangle.Height;
stampImage2.ScaleToFit(width, height);
stampImage2.SetAbsolutePosition(width / 2 - stampImage2.Width / 2, height / 2 - stampImage2.Height / 2);
stampImage2.SetAbsolutePosition(50, 50);
stampImage2.Rotation = 0;
pdfData.AddImage(stampImage2);
pdfData.EndText();
关于如何最好地实现这一点,有什么想法吗?这让我快发疯了
编辑*****************************
这些是我目前所追求的途径。关于如何“水印”3D PDF有什么想法吗
//Stamp Image Method (works on 2D PDF and 3D PDF BUT results in EDITABLE stamp)
System.Drawing.Image imageBTM2 = System.Drawing.Image.FromHbitmap(Properties.Resources.ClassificationBlock.GetHbitmap());
Image stampImage2 = iTextSharp.text.Image.GetInstance(imageBTM2, System.Drawing.Imaging.ImageFormat.Png);
Rectangle stampRect2 = null;
Rectangle location2 = new Rectangle(0, 0, stampImage2.Width, stampImage2.Height);
PdfAnnotation pdfStamp2 = PdfAnnotation.CreateStamp(stamp.Writer, location2, null, "ImageText");
stampImage2.SetAbsolutePosition(0, 0);
PdfAppearance app2 = stamp.GetUnderContent(1).CreateAppearance(stampImage2.Width, stampImage2.Height);
app2.AddImage(stampImage2);
pdfStamp2.SetAppearance(PdfName.N, app2);
pdfStamp2.SetPage();
stamp.AddAnnotation(pdfStamp2, 1);
stampRect2 = location2;
//Watermark Layering Method (works only on 2D PDF)
var layers = stamp.GetPdfLayers();
var imgLayer = new PdfLayer("StackoverflowImage", stamp.Writer);
PdfContentByte cb = stamp.GetUnderContent(1);
cb.BeginLayer(imgLayer);
stampImage2.ScalePercent(100f);
stampImage2.SetAbsolutePosition(pageWidth/2, pageHeight/2);
cb.AddImage(stampImage2);
cb.EndLayer();
//Jan's Watermark method (works only on 2D PDF)
PdfContentByte over = stamp.GetOverContent(1);
stampImage2.SetAbsolutePosition(pageWidth / 2, pageHeight / 2);
PdfLayer imgLayer = new PdfLayer("StackoverflowImage", stamp.Writer);
imgLayer.OnPanel = false;
over.BeginLayer(imgLayer);
over.AddImage(stampImage2);
over.EndLayer();
stamp.Close();
reader.Close();
首先:您很可能无法阻止选择图像 第二:我在Java中使用itext,所以您可能最终会将方法名的第一个字符大写 对于其余部分或您的问题,您可以尝试将此图像添加到图层:
PdfContentByte over = stamp.getOverContent(1)
Image img = ...//code to get your image;
img.setAbsolutePosition(x, y); //at your postion
PdfLayer imgLayer = new PdfLayer("StackoverflowImage", stamper.getWriter());
imgLayer.setOnPanel(false);
over.beginLayer(imgLayer);
over.addImage(img);
over.endLayer();
解决了!使用如上所述的“Stamp-Image方法”,我只需要更改Stamp本身的属性(将标志更改为锁定和只读)。这会导致戳记位于三维PDF图层上方,并且无法调整大小、编辑或删除。因此,代码现在是:
//Stamp Image Method
System.Drawing.Image imageBTM2 = System.Drawing.Image.FromHbitmap(Properties.Resources.ClassificationBlock.GetHbitmap());
Image stampImage2 = iTextSharp.text.Image.GetInstance(imageBTM2, System.Drawing.Imaging.ImageFormat.Png);
Rectangle stampRect2 = null;
Rectangle location2 = new Rectangle(0, 0, stampImage2.Width, stampImage2.Height);
PdfAnnotation pdfStamp2 = PdfAnnotation.CreateStamp(stamp.Writer, location2, null, "ImageText");
pdfStamp2.Flags = iTextSharp.text.pdf.PdfAnnotation.FLAGS_LOCKED;
pdfStamp2.Flags = iTextSharp.text.pdf.PdfAnnotation.FLAGS_READONLY;
stampImage2.SetAbsolutePosition(0, 0);
PdfAppearance app2 = stamp.GetUnderContent(1).CreateAppearance(stampImage2.Width, stampImage2.Height);
app2.AddImage(stampImage2);
pdfStamp2.SetAppearance(PdfName.N, app2);
pdfStamp2.SetPage();
stamp.AddAnnotation(pdfStamp2, 1);
stampRect2 = location2;
不幸的是,在使用此代码表单运行后,它仍然没有显示在PDF上。您将其放置在何处?您是否可以将示例PDF和完整的戳记图像共享到迄今为止的PDF代码中?不幸的是,这是我公司的IP,因此我无法共享。然后创建一个没有公司内容的示例?我知道,对于我测试过的所有PDF,上面的图片都会放在PDF之上……正如您对@Jan的回答所提出的问题所显示的那样,您也应该分享您正在使用的测试PDF。
//Stamp Image Method
System.Drawing.Image imageBTM2 = System.Drawing.Image.FromHbitmap(Properties.Resources.ClassificationBlock.GetHbitmap());
Image stampImage2 = iTextSharp.text.Image.GetInstance(imageBTM2, System.Drawing.Imaging.ImageFormat.Png);
Rectangle stampRect2 = null;
Rectangle location2 = new Rectangle(0, 0, stampImage2.Width, stampImage2.Height);
PdfAnnotation pdfStamp2 = PdfAnnotation.CreateStamp(stamp.Writer, location2, null, "ImageText");
pdfStamp2.Flags = iTextSharp.text.pdf.PdfAnnotation.FLAGS_LOCKED;
pdfStamp2.Flags = iTextSharp.text.pdf.PdfAnnotation.FLAGS_READONLY;
stampImage2.SetAbsolutePosition(0, 0);
PdfAppearance app2 = stamp.GetUnderContent(1).CreateAppearance(stampImage2.Width, stampImage2.Height);
app2.AddImage(stampImage2);
pdfStamp2.SetAppearance(PdfName.N, app2);
pdfStamp2.SetPage();
stamp.AddAnnotation(pdfStamp2, 1);
stampRect2 = location2;