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

标签 itextsharp response memorystream

我尝试了几种方法来做到这一点,但仍然无法做到。 看来 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

引用: Put text on top of an image?

搜索引擎引用: 使用与背景 pdf 相同颜色的图像来白化 pdf 文档上的文本 图像与itextpdf重叠 itextsharp 图像在文本 whiteout 的顶部 itextsharp 将图片置于文本之上 itextpdf 图片在上面

谢谢, 路易斯安那州的道格·卢贝

Example of my final product

最佳答案

你可以很容易地做到这一点。 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

关于itextsharp - 使用 PDF itextSharp 可以在创建 pdf 文档时将图像放在文本之上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8600599/

相关文章:

mysql - 如何处理客户端向服务器发出的多个请求

c# - 如何使用 MemoryStream 选择路径

c# - 使用 BinaryFormatter 反序列化我的序列化数据时出现异常

javascript - 从 api 检索 Post 请求响应并将其传递给另一个 post 请求以传递给 angular8 应用程序

c# - 在内存中写入文本文件并使用 savefiledialog 保存

c# - iTextSharp 生成的 PDF : How to send the pdf to the client and add a prompt?

asp.net - 直接通过代码打开生成的pdf文件,无需保存到磁盘

c# - itextsharp pdfpcell header

C#/ASP.NET 在 PDF 中创建可呈现的发票

c# - HttpModule 更改响应