【问题标题】:iTextSharp Image Bring To FrontiTextSharp 图像显示在前面
【发布时间】:2014-10-31 10:47:06
【问题描述】:

为我们的网络应用程序构建一个文档生成系统,并根据需要对文档进行品牌化。该文档是在 powerpoint 中设计的,并通过 NitroPdf 打印。第一页本质上是一个大图像,图像中有一个白色区域。 我试图将品牌标志放在分配的空白处。定位没问题,但是我的品牌形象出现在 PDF 文档整页图片的后面。

用谷歌搜索后,我似乎找不到“z-index”类型的函数...会认为我不会是唯一有问题的人吗?

添加图片的部分代码如下:

        image.ScaleToFit(width, height);
        image.SetDpi(300, 300);

        // Position the logo.
        image.SetAbsolutePosition(fromLeft, fromBottom);

        // Add the image.
        document.Add(image);

【问题讨论】:

    标签: image pdf itextsharp


    【解决方案1】:

    很奇怪,您需要以下行来将图像添加到现有 PDF:

    document.Add(image);
    

    就好像你用的是PdfWriter而不是PdfStamper,这会很奇怪。

    也许您忽略了documentation,或者您在开始编写代码之前没有搜索 StackOverflow:How can I insert an image with iTextSharp in an existing PDF?

    using System.IO;
    using iTextSharp.text;
    using iTextSharp.text.pdf;
    
    class Program
    {
        static void Main(string[] args)
        {
            using (Stream inputPdfStream = new FileStream("input.pdf", FileMode.Open, FileAccess.Read, FileShare.Read))
            using (Stream inputImageStream = new FileStream("some_image.jpg", FileMode.Open, FileAccess.Read, FileShare.Read))
            using (Stream outputPdfStream = new FileStream("result.pdf", FileMode.Create, FileAccess.Write, FileShare.None))
            {
                var reader = new PdfReader(inputPdfStream);
                var stamper = new PdfStamper(reader, outputPdfStream);
                var pdfContentByte = stamper.GetOverContent(1);
    
                Image image = Image.GetInstance(inputImageStream);
                image.SetAbsolutePosition(100, 100);
                pdfContentByte.AddImage(image);
                stamper.Close();
            }
        }
    }
    

    您可能已经找到使用GetUnderContent() 的示例。这会在现有内容添加内容。如果希望内容覆盖现有内容,则需要GetOverContent(),如代码示例所示。

    【讨论】:

    • 您好,谢谢调查。我们使用的包装器是几年前编写的,直到现在都运行良好。也许当时使用的示例引用了 Document。不确定,会调查。感谢您的建议。
    • 好的,经过进一步调查,我们使用的是 PdfReader 和 PdfWriter,但由于某种原因使用 Document 来添加图像。一定是疏忽——尽管一直在工作。我们基本上是合并各种文档,然后添加所需的图像。快速更改 PdfContentByte.AddImage 和图像显示正确分层。
    • 使用 PdfWriter 和 Document 会抛弃所有交互功能。请正确使用iText!
    • 网上有大量使用PdfWriter的例子。是不是都用错了?
    • 不幸的是,它们中的大多数都不正确,这对于编写官方文档的人(即我)和遵循错误建议的人来说都是令人沮丧的。使用PdfCopyPdfSmartCopy 合并文档,not 使用PdfWriter
    【解决方案2】:

    也许有点晚了,但我也遇到了同样的问题,我已经用 Paragraphs 的解决方法解决了(以下是 Visual Basic 中的代码):

    Public Class PDF
    
      Public Doc As Document
      Public Writer As PdfWriter
      Public Cb As PdfContentByte
    
      Public Sub setFrontImage(ByVal _appendImg As String, align As Integer, x As Integer, y As Integer, ByVal w As Integer, h As Integer, _leading As Integer)
    
        Dim ct As New ColumnText(Cb)
        Dim ph As Phrase
        Dim ch As Chunk
    
        Dim p As Paragraph = new Paragraph()
        Dim image As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(_appendImg)
        image.ScaleAbsolute(w, h)
        p.Add(new Chunk(image,x,y))
    
        ct.SetSimpleColumn(p,x, y, w, h, _leading, align)
        ct.Go()
    
      End Sub
    End Class
    

    我看到您使用绝对位置将您的徽标放在您的图像上,我也是,如果您不需要将其放在受限空间内,请考虑修改 Chunk 的使用宽度和高度。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-26
      • 2019-01-09
      • 1970-01-01
      • 2023-03-26
      • 2011-05-24
      • 1970-01-01
      • 2017-03-13
      • 1970-01-01
      相关资源
      最近更新 更多