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

时间:2011-12-22 07:22:34

标签: 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文档上的whiteout文本 图像与itextpdf重叠 在文本白化之上的itextsharp图像 itextsharp将图片置于文本顶部 顶部的itextpdf图像

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

Example of my final product

1 个答案:

答案 0 :(得分:8)

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

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

一个注释, 从不 GetBuffer()上致电MemoryStream 始终 使用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