使用 PDF itextSharp 可以在创建 pdf 文档时将图像放在文本之上

发布于 2024-12-22 13:56:22 字数 3165 浏览 3 评论 0原文

我尝试了多种方法来做到这一点,但仍然无法实现。 看来 iTextSharp 需要 2 遍情况,以便图像出现在文本顶部。 所以我尝试使用内存流来执行此操作,但我不断收到错误。

    Public Function createDoc(ByRef reqResponse As HttpResponse) As Boolean

        Dim m As System.IO.MemoryStream = New System.IO.MemoryStream()
        Dim document As Document = New Document()
        Dim writer As PdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(document, m)
        document.Open()
        document.Add(New Paragraph(DateTime.Now.ToString()))
        document.Add(New Paragraph(DateTime.Now.ToString()))
        document.Add(New Paragraph(DateTime.Now.ToString()))
        document.Add(New Paragraph(DateTime.Now.ToString()))
        document.Add(New Paragraph(DateTime.Now.ToString()))
        document.Add(New Paragraph(DateTime.Now.ToString()))
        document.Add(New Paragraph(DateTime.Now.ToString()))
        document.Close()
        writer.Flush()
        writer.Flush()
        'yes; I get the pdf if this is the last statement
        'reqResponse.OutputStream.Write(m.GetBuffer(), 0, m.GetBuffer().Length)

        'this statment does not work it says the stream is closed
        'm.Position = 0
        Dim Reader As PdfReader = New PdfReader(m)
        'Dim rm As MemoryStream = New MemoryStream(m.GetBuffer(), 0, m.GetBuffer().Length)
        Dim PdfStamper As PdfStamper = New PdfStamper(Reader, reqResponse.OutputStream)
        Dim cb As iTextSharp.text.pdf.PdfContentByte = Nothing
        cb = PdfStamper.GetOverContent(1)
        Dim locMyImage As System.Drawing.Image = System.Drawing.Image.FromStream(zproProduceWhiteImageToCovertBarCodeNumbers())
        Dim BImage As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(locMyImage, iTextSharp.text.BaseColor.CYAN)
        Dim overContent As PdfContentByte = PdfStamper.GetOverContent(1)
        BImage.SetAbsolutePosition(5, 5)
        overContent.AddImage(BImage)
        PdfStamper.FormFlattening = True
        PdfStamper.Close()

        'rm.Flush()
        'rm.Close()
        'Dim data As Byte() = rm.ToArray()

        'reqResponse.Clear()
        'Dim finalMs As MemoryStream = New MemoryStream(data)
        'reqResponse.ContentType = "application/pdf"
        'reqResponse.AddHeader("content-disposition", "attachment;filename=labtest.pdf")
        'reqResponse.Buffer = True
        'finalMs.WriteTo(reqResponse.OutputStream)
        'reqResponse.End()


        'Dim data As Byte() = rm.ToArray()
        'reqResponse.OutputStream.Write(data, 0, data.Length)

        ''Response.OutputStream.Write(m.GetBuffer(), 0, m.GetBuffer().Length);
        ''Response.OutputStream.Flush();
        ''Response.OutputStream.Close();
        ''Response.End();


        HttpContext.Current.ApplicationInstance.CompleteRequest()
        Return True
    End Function

参考: 将文本放在顶部图像的?

搜索引擎参考: 使用与背景 pdf 颜色相同的图像来涂白 pdf 文档上的文本 图像与 itextpdf 重叠 文本白色上方的 itextsharp 图像 itextsharp 将图片放在文本上方 itextpdf 图片在上面

谢谢, 路易斯安那州的 Doug Lubey

我的最终产品示例

I attempted several ways to do this, but still cannot get it.
It appears iTextSharp requires a 2 pass situation so that an image appears on top of the text.
So I am attempting to do this using memory streams, but I keep getting errors.

    Public Function createDoc(ByRef reqResponse As HttpResponse) As Boolean

        Dim m As System.IO.MemoryStream = New System.IO.MemoryStream()
        Dim document As Document = New Document()
        Dim writer As PdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(document, m)
        document.Open()
        document.Add(New Paragraph(DateTime.Now.ToString()))
        document.Add(New Paragraph(DateTime.Now.ToString()))
        document.Add(New Paragraph(DateTime.Now.ToString()))
        document.Add(New Paragraph(DateTime.Now.ToString()))
        document.Add(New Paragraph(DateTime.Now.ToString()))
        document.Add(New Paragraph(DateTime.Now.ToString()))
        document.Add(New Paragraph(DateTime.Now.ToString()))
        document.Close()
        writer.Flush()
        writer.Flush()
        'yes; I get the pdf if this is the last statement
        'reqResponse.OutputStream.Write(m.GetBuffer(), 0, m.GetBuffer().Length)

        'this statment does not work it says the stream is closed
        'm.Position = 0
        Dim Reader As PdfReader = New PdfReader(m)
        'Dim rm As MemoryStream = New MemoryStream(m.GetBuffer(), 0, m.GetBuffer().Length)
        Dim PdfStamper As PdfStamper = New PdfStamper(Reader, reqResponse.OutputStream)
        Dim cb As iTextSharp.text.pdf.PdfContentByte = Nothing
        cb = PdfStamper.GetOverContent(1)
        Dim locMyImage As System.Drawing.Image = System.Drawing.Image.FromStream(zproProduceWhiteImageToCovertBarCodeNumbers())
        Dim BImage As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(locMyImage, iTextSharp.text.BaseColor.CYAN)
        Dim overContent As PdfContentByte = PdfStamper.GetOverContent(1)
        BImage.SetAbsolutePosition(5, 5)
        overContent.AddImage(BImage)
        PdfStamper.FormFlattening = True
        PdfStamper.Close()

        'rm.Flush()
        'rm.Close()
        'Dim data As Byte() = rm.ToArray()

        'reqResponse.Clear()
        'Dim finalMs As MemoryStream = New MemoryStream(data)
        'reqResponse.ContentType = "application/pdf"
        'reqResponse.AddHeader("content-disposition", "attachment;filename=labtest.pdf")
        'reqResponse.Buffer = True
        'finalMs.WriteTo(reqResponse.OutputStream)
        'reqResponse.End()


        'Dim data As Byte() = rm.ToArray()
        'reqResponse.OutputStream.Write(data, 0, data.Length)

        ''Response.OutputStream.Write(m.GetBuffer(), 0, m.GetBuffer().Length);
        ''Response.OutputStream.Flush();
        ''Response.OutputStream.Close();
        ''Response.End();


        HttpContext.Current.ApplicationInstance.CompleteRequest()
        Return True
    End Function

reference:
Put text on top of an image?

seach engine reference:
whiteout text on a pdf document by using a image which is the same color as the background pdf
image overlap with itextpdf
itextsharp image on top of the text whiteout
itextsharp place picture on top of text
itextpdf image on top

thanks,
Doug Lubey of Louisiana

Example of my final product

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

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

发布评论

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

评论(1

策马西风 2024-12-29 13:56:23

你可以很容易地做到这一点。 Document 对象是一个辅助对象,它抽象了 PDF 模型的许多内部结构,并且在大多数情况下假设您想要流动内容并且文本将位于图像之上。如果您想解决这个问题,您可以直接与 PdfWriter 对象对话。它有两个属性:DirectContentDirectContentUnder,这两个属性都有名为 AddImage() 的方法,可用于设置图像上的绝对位置。 DirectContent 位于现有内容上方,DirectContentUnder 位于现有内容下方。请参阅示例代码:

您似乎是在网络上执行此操作,因此您需要将其调整为您正在使用的任何流,但这应该非常容易。

需要注意的是,决不MemoryStream 上调用 GetBuffer()总是 >使用ToArray()。前一种方法包含未初始化的字节,可能会导致 PDF 损坏。

    ''//File that we are creating
    Dim OutputFile As String = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf")
    ''//Image to place
    Dim SampleImage As String = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "SampleImage.jpg")

    ''//Standard PDF creation setup
    Using FS As New FileStream(OutputFile, FileMode.Create, FileAccess.Write, FileShare.None)
        Using Doc As New Document(PageSize.LETTER)
            Using writer = PdfWriter.GetInstance(Doc, FS)

                ''//Open the document for writing
                Doc.Open()
                ''//Add a simple paragraph
                Doc.Add(New Paragraph("Hello world"))

                ''//Create an image object
                Dim Img = iTextSharp.text.Image.GetInstance(SampleImage)
                ''//Give it an absolute position in the top left corner of the document (remembering that 0,0 is bottom left, not top left)
                Img.SetAbsolutePosition(0, Doc.PageSize.Height - Img.Height)
                ''//Add it directly to the raw pdfwriter instead of the document helper. DirectContent is above and DirectContentUnder is below
                writer.DirectContent.AddImage(Img)

                ''//Close the document
                Doc.Close()
            End Using
        End Using
    End Using

You can do this pretty easily. The Document object is a helper object that abstracts away many of the internals of the PDF model and for the most part assumes that you want to flow content and that text would go above images. If you want to get around this you can talk directly the PdfWriter object instead. It has two properties, DirectContent and DirectContentUnder that both have methods named AddImage() that you can use to set an absolute position on an image. DirectContent is above existing content and DirectContentUnder is below it. See the code for an example:

You appear to be doing this on the web so you'll need to adapt this to whatever stream you are using but that should be pretty easy.

One note, NEVER call GetBuffer() on a MemoryStream, ALWAYS use ToArray(). The former method includes uninitialized bytes that will give you potentially corrupt PDFs.

    ''//File that we are creating
    Dim OutputFile As String = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf")
    ''//Image to place
    Dim SampleImage As String = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "SampleImage.jpg")

    ''//Standard PDF creation setup
    Using FS As New FileStream(OutputFile, FileMode.Create, FileAccess.Write, FileShare.None)
        Using Doc As New Document(PageSize.LETTER)
            Using writer = PdfWriter.GetInstance(Doc, FS)

                ''//Open the document for writing
                Doc.Open()
                ''//Add a simple paragraph
                Doc.Add(New Paragraph("Hello world"))

                ''//Create an image object
                Dim Img = iTextSharp.text.Image.GetInstance(SampleImage)
                ''//Give it an absolute position in the top left corner of the document (remembering that 0,0 is bottom left, not top left)
                Img.SetAbsolutePosition(0, Doc.PageSize.Height - Img.Height)
                ''//Add it directly to the raw pdfwriter instead of the document helper. DirectContent is above and DirectContentUnder is below
                writer.DirectContent.AddImage(Img)

                ''//Close the document
                Doc.Close()
            End Using
        End Using
    End Using
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文