gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 01:27:29 36 4
gpt4 key购买 nike

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

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com