使用PDF itextSharp可以在创建pdf文档时将图像放在文本顶部 [英] Using PDF itextSharp it is possible to put an image on top of text while creating the pdf document

查看:188
本文介绍了使用PDF itextSharp可以在创建pdf文档时将图像放在文本顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了几种方法来做到这一点,但仍然无法得到它。
看来iTextSharp需要2遍情况,以便图像显示在文本的顶部。
所以我试图使用内存流来做这件事,但我一直都会遇到错误。

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

参考:

搜索引擎参考:
whiteout通过使用与背景颜色相同的图像在pdf文档上显示文本pdf
图像与文本whiteout顶部重叠
在文本whiteout顶部的itextsharp图像
itextsharp文本顶部的图片
itextpdf图片在顶部

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

谢谢,
路易斯安那的Doug Lubey

thanks, Doug Lubey of Louisiana

推荐答案

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

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.

一个注释, 从不 致电 GetBuffer() MemoryStream 上, 始终 使用 ToArray()。前一种方法包括未初始化的字节,它会为您提供可能损坏的PDF。

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

这篇关于使用PDF itextSharp可以在创建pdf文档时将图像放在文本顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆