【问题标题】:Filling out a PDF form with values from textbox.text and printing the PDF after the PDF is filled使用 textbox.text 中的值填写 PDF 表单并在填写 PDF 后打印 PDF
【发布时间】:2019-02-23 12:56:09
【问题描述】:

我已经在网上搜索过,但没有找到解决方案。

我有一个包含客户数据集的 VB.net 表单,我想获取 textbox.text 中的联系信息并填写 PDF 表单,然后打印。

Itextsharp 被提及创建pdf的每个地方,而不是填写并打印它,我找到了一个有前途的代码,我对vb.net一点也不熟悉,这是我的第一个程序。

这是我发现我认为可以工作的代码......

Imports System
Imports System.IO
Imports System.Xml
Imports iTextSharp
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.text.xml
Imports System.Security

Private Sub xmltopdf()
    Dim pdfTemp As String = "C:\ExampleTemplate.pdf" ' ---> It's the original pdf form you want to fill
    Dim newFile As String = "C:\NewFile.Pdf" ' ---> It will generate new pdf that you have filled from your program

    ' ------ READING -------

    Dim pdfReader As New PdfReader(pdfTemp)

    ' ------ WRITING -------

    ' If you don’t specify version and append flag (last 2 params) in below line then you may receive “Extended Features” error when you open generated PDF
    Dim pdfStamper As New PdfStamper(pdfReader, New FileStream(newFile, FileMode.Create), "\6c", True)

    Dim pdfFormFields As AcroFields = pdfStamper.AcroFields

    ' ------ SET YOUR FORM FIELDS ------

    pdfFormFields.SetField("Company", "Parth Dave & Co.")
    pdfFormFields.SetField("SalesOrder", "1234456")
    pdfFormFields.SetField("InstallAddress", "Lorimer Street")
    pdfFormFields.SetField("Suburb", "Port Melbourne")
    pdfFormFields.SetField("Post_Code", "3207")
    pdfFormFields.SetField("ClientContact", "Parth")
    pdfFormFields.SetField("ClientPhone", "0402020202")

    pdfStamper.FormFlattening = False

    ' close the pdf
    pdfStamper.Close()
    ' pdfReader.close() ---> DON"T EVER CLOSE READER IF YOU'RE GENERATING LOTS OF PDF FILES IN LOOP
End Sub

我在导入时遇到错误并且不知道导入是什么..

我已经添加了 itextsharp.dll 应该没问题..

如果有人可以帮助我或向我发送正确的方向,将不胜感激。

【问题讨论】:

  • 您是否添加了对 itextsharp.dll 的引用? Project|Add Reference|Browse找到你的dll并选择它
  • 实际上,我只是将您的代码添加到一个项目中,并使用 NuGet 包管理器安装 iTextSharp,除了“\6c”上的 Option Strict 错误之外,一切都按预期进行。没有显示任何错误。
  • 试图删除它并再次添加它......仍然无法正常工作
  • 等等,你有没有在课堂之外导入,对吧?它们应该在任何其他代码之前。
  • 不,我没有

标签: vb.net visual-studio pdf itext


【解决方案1】:

将 Imports 放在类之外。

【讨论】:

    猜你喜欢
    • 2017-06-12
    • 1970-01-01
    • 2017-08-21
    • 1970-01-01
    • 2011-10-15
    • 2013-01-08
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多