C# iTextSharp 7:带有透明度的图章

C# iTextSharp 7:带有透明度的图章,c#,pdf,annotations,itext,itext7,C#,Pdf,Annotations,Itext,Itext7,谢天谢地,在一些帮助下,我使用以下代码获得了邮票: PdfReader reader = new PdfReader(source); PdfWriter writer = new PdfWriter(dest); PdfDocument pdfDoc = new PdfDocument(reader, writer); Rectangle crop = pdfDoc.GetPage(1).GetCropBox(); Debug.

谢天谢地,在一些帮助下,我使用以下代码获得了邮票:

PdfReader reader = new PdfReader(source);
        PdfWriter writer = new PdfWriter(dest);
        PdfDocument pdfDoc = new PdfDocument(reader, writer);

        Rectangle crop = pdfDoc.GetPage(1).GetCropBox();
        Debug.WriteLine("CropBox Rectangle Dim "+crop);


        float w = 0;
        float h = 0;

        ImageData img = ImageDataFactory.Create(imgsrc);

        float iWidth = img.GetWidth();
        float iHeight = img.GetHeight();
        if (crop.GetWidth() > crop.GetHeight())
        {
            w = crop.GetWidth();
            h = crop.GetHeight();
        }
        else
        {
            w = crop.GetHeight();
            h = crop.GetWidth();
        }


        Debug.WriteLine("Width = "+w+" and Height = "+h);


        Rectangle location = new Rectangle(crop.GetLeft(),crop.GetBottom(),iWidth/4,iHeight/4);


        PdfStampAnnotation stamp = new PdfStampAnnotation(location).SetStampName(new PdfName("Logo"));

        PdfFormXObject xObj = new PdfFormXObject(new Rectangle(iWidth, iHeight));
        PdfCanvas canvas = new PdfCanvas(xObj, pdfDoc);
        canvas.AddImage(img, 0, 0,iWidth, false);
        stamp.SetNormalAppearance(xObj.GetPdfObject());



        stamp.SetFlags(PdfAnnotation.PRINT);

        pdfDoc.GetFirstPage().AddAnnotation(stamp);
        pdfDoc.Close();
如果我没有弄错的话,使用iText进行冲压的过程涉及到使用外部图像文件——我就是这么做的。但是,与我的Adobe stamps不同,这些图像保留了白色背景,因此不透明。在尝试将其转换为透明PNG时,我得到了一个奇怪的结果。我的邮票以黑色背景出现。我最初的假设是,画布对象具有某种默认为黑色的填充颜色?如何将黑色填充设置为透明


我尝试使用“图形状态”(PdfGState-State),将其不透明度设置为0,并将其应用于画布。然而,这反过来又使我的邮票图像不可见。你是如何处理这一问题的?

好吧,在盯着屏幕看了几个小时后,我终于找到了我的邮票尺寸不正确的原因。显然,包含要合并的PDF文件的FormXObject不会存储其存储的页面的相应宽度和高度。当页面明显是横向的时,它不仅提供了更高的高度,而且也不能反映真实的页面大小。因此,使用FormXObject.getWidth()FormXObject.getHeight()会导致不正确的尺寸,挤压邮票图像。相反,使用与文件关联的PdfDocument合并并相应地使用页面().GetCropBox().GetWidth()高度

当然,不用说,这种误解是基于我对FormXObject将真正做什么的假设

以下是所有感兴趣的人的最终代码,请注意,其中包括一些未使用的变量和主要用于调试的无关行:

 PdfReader reader = new PdfReader(src);
        PdfWriter writer = new PdfWriter(dest);

        // Document to be edited and documented to be merged in
        PdfDocument newDoc = new PdfDocument(reader, writer);
        PdfDocument srcDoc = new PdfDocument(new PdfReader(stampsrc));

        // CropBox And Dimensions
        Rectangle crop = newDoc.GetFirstPage().GetCropBox();
        float width = crop.GetWidth();
        float height = crop.GetHeight();


        // Create FormXObject and Canvas
        PdfFormXObject page = srcDoc.GetPage(1).CopyAsFormXObject(newDoc);
        //Extract Page Dimensions
        float xWidth = srcDoc.GetFirstPage().GetCropBox().GetWidth();
        float xHeight = srcDoc.GetFirstPage().GetCropBox().GetHeight();

        Rectangle location = new Rectangle(crop.GetLeft(), crop.GetBottom(), xWidth , xHeight );



        PdfStampAnnotation stamp = new PdfStampAnnotation(location).SetStampName(new PdfName("Logo"));
        PdfCanvas canvas = new PdfCanvas(newDoc.GetFirstPage().NewContentStreamBefore(), newDoc.GetFirstPage().GetResources(), newDoc);

       // canvas.AddXObject(page,location.GetLeft(),location.GetBottom(),page.GetWidth());
        stamp.SetNormalAppearance(page.GetPdfObject());
        stamp.SetFlags(PdfAnnotation.PRINT);
        newDoc.GetFirstPage().AddAnnotation(stamp);

        srcDoc.Close();
        newDoc.Close();

戳记只是在现有内容之上或之下添加内容的术语,它不一定涉及外部图像,也可以是文本。也就是说,看看构建块教程本章中的透明背景图像示例:。我刚刚浏览了代码(因此是注释,而不是答案),但它应该为您提供一种水印方法,这听起来像是您正在尝试做的事情。@Samuel Huylebroeck看到了水印示例,而不完全是我遇到的。然而,我找到了另一种在我的商标上盖章的方式。现在我将叠加包含邮票的PDF。它工作得很好,但是一旦您将一个戳记与FormXObject(即包含该戳记的页面)关联起来,大小就会混乱——甚至更糟,该戳记不会出现。让我知道是否值得发布这段新代码。“显然,包含要合并的PDF文件的FormXObject不存储它正在存储的页面的相应宽度和高度。”-form XObject具有页面媒体框的尺寸,而不是其CropBox。