-1

我的目标是在3D PDF 上添加一个像水印一样的图像(最终用户无法选择、编辑、调整大小或删除图像)。

我尝试制作如下所示的注释,但可以在输出 PDF 上调整和删除图像(资源中的“ClassificationBlock.png”)。这是“PdfAnnotation”矩形的固有行为,还是我可以定义一个属性来保持图像基本上是只读的?

using (PdfStamper stamp = new PdfStamper(reader, fs))

. . .

                Rectangle stampRect2 = null;

                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 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.GetOverContent(1).CreateAppearance(stampImage2.Width, stampImage2.Height);
                app2.AddImage(stampImage2);
                pdfStamp2.SetAppearance(PdfName.N, app2);
                pdfStamp2.SetPage();
                stamp.AddAnnotation(pdfStamp2, 1);
                stampRect2 = location2;

                stamp.FormFlattening = true;

                stamp.Close();
                reader.Close();
                fs.Close();

我也尝试过通过 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();
4

2 回答 2

0

解决了!使用如上所述的“图章图像方法”,我只需要更改图章本身的属性(将 FLAGS 更改为 LOCKED 和 READ-ONLY)。这会导致 3D 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;
于 2017-05-31T12:42:09.243 回答
0

首先:您很可能无法阻止选择图像。

第二:我用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();
于 2017-05-26T15:38:04.800 回答