iTextSharp 覆盖图像

发布于 2024-10-22 07:58:15 字数 1581 浏览 1 评论 0原文

大家好,我有一个实例,其中我有一个徽标图像作为某些艺术品的一部分。

如果用户上传新徽标,我有一个比默认徽标大的表单字段。

然后,我使用该表单字段来定位新图像。

问题是我需要将该表单字段的背景颜色设置为白色,以便在新图像小于旧徽标的情况下覆盖旧徽标。

我所做的是:

foreach (var imageField in imageReplacements)
            {
                fields.SetFieldProperty(imageField.Key, "bgcolor", iTextSharp.text.Color.WHITE, null);
                fields.RegenerateField(imageField.Key);

                PdfContentByte overContent = stamper.GetOverContent(imageField.Value.PageNumber);
                float[] logoArea = fields.GetFieldPositions(imageField.Key);
                if (logoArea != null)
                {
                    iTextSharp.text.Rectangle logoRect = new iTextSharp.text.Rectangle(logoArea[1], logoArea[2], logoArea[3], logoArea[4]);

                    var logo = iTextSharp.text.Image.GetInstance(imageField.Value.Location);
                    if (logo.Width >= logoRect.Width || logo.Height >= logoRect.Height)
                    {
                        logo.ScaleToFit(logoRect.Width, logoRect.Height);
                    }
                    logo.Alignment = iTextSharp.text.Image.ALIGN_LEFT;
                    logo.SetAbsolutePosition(logoRect.Left, logoArea[2] + (logoRect.Height - logo.ScaledHeight) / 2);
                    // left:  logoArea[3] - logo.ScaledWidth + (logoRect.Width - logo.ScaledWidth) / 2

                    overContent.AddImage(logo);
                }
            }

这样做的问题是字段的背景颜色设置为白色,然后图像不会出现。我删除了 SetFieldProperty 和 RegenerateField 命令,图像替换工作正常。

有没有办法在图层上设置堆叠顺序?

Hi guys I have an instance where I have a logo image as part of some artwork..

If a user uploads a new logo I have a form field which is larger than the default logo.

I then use that form field to position the new image.

The problem is I need to set the background colour of that form field to white so that it covers the old logo in the event that the new image is smaller than the old logo..

what I have done is:

foreach (var imageField in imageReplacements)
            {
                fields.SetFieldProperty(imageField.Key, "bgcolor", iTextSharp.text.Color.WHITE, null);
                fields.RegenerateField(imageField.Key);

                PdfContentByte overContent = stamper.GetOverContent(imageField.Value.PageNumber);
                float[] logoArea = fields.GetFieldPositions(imageField.Key);
                if (logoArea != null)
                {
                    iTextSharp.text.Rectangle logoRect = new iTextSharp.text.Rectangle(logoArea[1], logoArea[2], logoArea[3], logoArea[4]);

                    var logo = iTextSharp.text.Image.GetInstance(imageField.Value.Location);
                    if (logo.Width >= logoRect.Width || logo.Height >= logoRect.Height)
                    {
                        logo.ScaleToFit(logoRect.Width, logoRect.Height);
                    }
                    logo.Alignment = iTextSharp.text.Image.ALIGN_LEFT;
                    logo.SetAbsolutePosition(logoRect.Left, logoArea[2] + (logoRect.Height - logo.ScaledHeight) / 2);
                    // left:  logoArea[3] - logo.ScaledWidth + (logoRect.Width - logo.ScaledWidth) / 2

                    overContent.AddImage(logo);
                }
            }

The problem with this is that the background colour of the field is set to white and the image then doesn't appear.. i remove the SetFieldProperty and RegenerateField commands and the image replacement works fine..

is there a way to set a stacking order on layers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海蓝天 2024-10-29 07:58:16

注释(例如表单字段)始终位于页面内容的顶部。注释 Z 顺序只是给定页面上注释数组的顺序。

页面内容 Z 顺序就是所有内容在内容流中出现的顺序。新的绘图操作符位于正在进行的操作符之上。

如果您想覆盖旧图像,请在其上绘制一个白框,然后在其上方绘制新徽标。。无需担心注释。


实际上,您真正需要做的就是不设置 imageField 的背景颜色。您已经在缩放新徽标以匹配旧徽标的大小。

但是,如果您确实必须绘制该白框,则相当简单:

overContent.setColorFill(iTextSharp.text.Color.WHITE);
overContent.rectangle( logoRect );
overcontent.fill();

Annotations (such as form fields) are always on top of page contents. Annotation Z order is just the order of the annotations array on a given page.

Page content Z order is just the order everything appears in the content stream. New drawing operators go on top of proceeding operators.

If you want to cover your old image, draw a white box over it and then draw the new logo over top that. No need to worry about annotations.


Actually, all you really need to do is not set the background color of the imageField. You're already scaling the new logo to match the size of the old one.

However, if you really must draw that white box, it's fairly simple:

overContent.setColorFill(iTextSharp.text.Color.WHITE);
overContent.rectangle( logoRect );
overcontent.fill();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文