【问题标题】:How to make stamped image uneditable using iTextSharp?如何使用 iTextSharp 使标记的图像不可编辑?
【发布时间】:2017-05-26 13:07:11
【问题描述】:

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

我尝试制作如下所示的注释,但图像(资源中的“ClassificationBlock.png”)可以在输出 PDF 上调整大小并删除。这是“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();

【问题讨论】:

  • 正如您对@Jan 回答的问题所表明的那样,您还应该分享您正在使用的测试 PDF。

标签: pdf itext watermark pdfstamper pdf-annotations


【解决方案1】:

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

第二:我在 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();

【讨论】:

  • 不幸的是,在使用此代码表单运行后,它仍然没有显示在 PDF 上。
  • 你把它放在哪里了?
  • 您能分享一下您目前拥有的示例 PDF 和完整的 stamp-image-to-pdf 代码吗?
  • 很遗憾这是我公司的IP,所以我不能分享。
  • 然后创建一个没有公司内容的示例?我知道对于我上面测试过的所有 PDF,它会将图像放在 PDF 之上...
【解决方案2】:

已解决!使用如上所述的“图章图像方法”,我只需要更改图章本身的属性(将 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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-28
    • 1970-01-01
    • 1970-01-01
    • 2022-06-10
    • 2015-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多