在 vb.net 中使用 itextsharp 将文本写入 pdf

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18648888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:08:21  来源:igfitidea点击:

write text to pdf with itextsharp in vb.net

vb.netpdfitextsharpvb.net-2010

提问by cMinor

I have a sub in vb.net like:

我在 vb.net 中有一个子项,例如:

Public Shared f_cb As BaseFont = BaseFont.CreateFont(Directory.GetCurrentDirectory() + "\plantillas\fonts\trebucbd.ttf", BaseFont.CP1252, BaseFont.NOT_EMBEDDED)
    Public Shared f_cn As BaseFont = BaseFont.CreateFont(Directory.GetCurrentDirectory() + "\plantillas\fonts\trebuc.ttf", BaseFont.CP1252, BaseFont.NOT_EMBEDDED)


Public Shared Sub GenPDF()
Try
   Using fs As System.IO.FileStream = New FileStream(Directory.GetCurrentDirectory() + "\te\TEST.pdf", FileMode.Create)
                Dim document As New Document(PageSize.A4, 25, 25, 30, 1)
                Dim writer As PdfWriter = PdfWriter.GetInstance(document, fs)
                document.AddTitle("test")
                document.Open()
                Dim cb As PdfContentByte = writer.DirectContent
                Dim png As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(Current.Server.MapPath("Images/logo.jpg"))
                png.ScaleAbsolute(75, 52)
                png.SetAbsolutePosition(40, 770)
                cb.AddImage(png)
                cb.BeginText()

                Dim left_margin As Integer = 40
                Dim top_margin As Integer = 750
                write(cb, "Streets S.R.L.", left_margin, top_margin, f_cb, 10)
                write(cb, "Av. 900, C.A.B.A. (1001), Peru", left_margin, top_margin - 12, f_cn, 8)
                write(cb, "Tel: 4300-2147 - Fax: 4300-2148", left_margin, top_margin - 24, f_cn, 8)
                write(cb, "C.U.I.T. 20-30602227-0", left_margin, top_margin - 36, f_cn, 10)
                write(cb, "I.V.A.", left_margin, top_margin - 48, f_cn, 10)
                cb.EndText()
                document.Close()
                writer.Close()
                fs.Close()
            End Using
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

Public Shared Sub write(ByVal cb As PdfContentByte, ByVal Text As String, ByVal X As Integer, ByVal Y As Integer, ByVal font As BaseFont, ByVal Size As Integer)
        cb.SetFontAndSize(font, Size)
        cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, Text, X, Y, 0)
    End Sub

So it works ok, now I would like to write to an already created pdf, but I can not find analog for vb.net I was trying pdfStamper but had no succes, How does the code to append data to pdf using itextsharp in vb.net look like?

所以它工作正常,现在我想写一个已经创建的pdf,但我找不到vb.net的模拟我正在尝试pdfStamper但没有成功,如何在vb中使用itextsharp将数据附加到pdf的代码。网长什么样?

回答by Karl Anderson

Here is an example of writing text to an existing PDF file and then saving it with a new name:

以下是将文本写入现有 PDF 文件然后使用新名称保存的示例:

Dim oldFile As String = "SomePath/Existing.pdf"
Dim newFile As String = "SomePath/New.pdf"

' Create reader
Dim reader As New PdfReader(oldFile)
Dim size As Rectangle = reader.GetPageSizeWithRotation(1)
Dim document As New Document(size)

' Create the writer
Dim fs As New FileStream(newFile, FileMode.Create, FileAccess.Write)
Dim writer As PdfWriter = PdfWriter.GetInstance(document, fs)
document.Open()
Dim cb As PdfContentByte = writer.DirectContent

' Set the font, color and size properties for writing text to the PDF
Dim bf As BaseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED)
cb.SetColorFill(BaseColor.DARK_GRAY)
cb.SetFontAndSize(bf, 8)

' Write text in the PDF
cb.BeginText()
Dim text As String = "Some text here"

' Set the alignment and coordinates here
cb.ShowTextAligned(1, text, 520, 640, 0)
cb.EndText()

' Put the text on a new page in the PDF 
Dim page As PdfImportedPage = writer.GetImportedPage(reader, 1)
cb.AddTemplate(page, 0, 0)

' Close the objects
document.Close()
fs.Close()
writer.Close()
reader.Close()

回答by Arides

While this question's original answer was very helpful, it doesn't work anymore due to natural evolution of the PdfSharp library. With the PdfWriterclass now being friend(=inaccessible), it seems the authors want to promote using of the high-level functions of documentinstead.

虽然这个问题的原始答案非常有帮助,但由于 PdfSharp 库的自然演变,它不再起作用。随着PdfWriter类现在成为朋友(=无法访问),作者似乎想要促进使用文档的高级功能。

For those who used the above approach and it didn't work - if you just need to edit the pdf, and it's enough to do it graphically (no search, etc.), this is how (vb.net):

对于那些使用上述方法但不起作用的人 - 如果您只需要编辑 pdf,并且以图形方式进行操作就足够了(无需搜索等),这就是方法(vb.net):

Public Sub AppendText(SourcePath As String, TargetPath As String,
                           Text As String, someFont As Font,
                           left As Single, top As Single)
  Dim document As PdfDocument = IO.PdfReader.Open(SourcePath)
  Dim Page As PdfPage = document.Pages.Item(0)
  Dim GFX As XGraphics = XGraphics.FromPdfPage(Page)
  Dim someXFont As New XFont(someFont.FontFamily.Name, someFont.Size)
  GFX.DrawString(Text, someXFont, XBrushes.Red, New PointF(left, top))
  document.Save(TargetPath)
End Sub

I'm still running into issues regarding the positioning, but the code is otherwise running (12/2019)

我仍然遇到有关定位的问题,但代码正在运行 (12/2019)