VB PDF程序,用户可以上传文件并保存

时间:2015-12-10 19:30:35

标签: c# vb.net pdf

我有一个将数据写入PDF文件的程序。我想添加程序的能力,询问用户他们想要上传什么文件,保存时会询问用户他们想要命名的内容以及他们想要保存的位置。我也希望这个程序保存两个文件。原始数据和包含数据的数据。到目前为止这是我的程序

'''公共类Form1

''' <summary>
''' Application main form Load event handler
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    ' Load all field names from template PDF
    'ListFieldNames()

    ' Fill the target PDF form with canned values
    FillForm2()

End Sub



''' <summary>
''' List all of the form fields into a textbox.  The
''' form fields identified can be used to map each of the
''' fields in a PDF.
''' </summary>
Private Sub ListFieldNames()

    Dim pdfTemplate As String = "c:\Temp\PDF\RD108ApplicationForTitle.pdf"
    Dim newFile As String = "c:\Temp\PDF\RD108ApplicationForTitle_fw4.pdf"

    ' title the form
    Me.Text += " - " + PdfTemplate

    ' create a new PDF reader based on the PDF template document
    'Dim pdfReader As PdfReader = New PdfReader(pdfTemplate)
    Dim pdfReader As New PdfReader(pdfTemplate)
    Dim pdfStamper As New PdfStamper(pdfReader, New FileStream( _
                newFile, FileMode.Create))

    Dim pdfFormFields As AcroFields = pdfStamper.AcroFields

    ' create and populate a string builder with each of the 
    ' field names available in the subject PDF
    Dim sb As New StringBuilder()

    Dim intFieldNum As Integer = 1
    Dim de As New DictionaryEntry
    For Each de In pdfReader.AcroFields.Fields
        sb.Append(String.Format("{0} - {1}{2}", intFieldNum, de.Key.ToString() + Environment.NewLine))
        pdfFormFields.SetField(de.Key.ToString(), intFieldNum)
        intFieldNum = intFieldNum + 1
    Next

    pdfStamper.FormFlattening = True
    pdfStamper.Close()

    ' Write the string builder's content to the form's textbox
    textBox1.Text = sb.ToString()
    textBox1.SelectionStart = 0

End Sub


Private Sub FillForm()

    Dim pdfTemplate As String = "c:\Temp\PDF\fw4.pdf"
    Dim newFile As String = "c:\Temp\PDF\Final_fw4.pdf"

    Dim pdfReader As New PdfReader(pdfTemplate)
    Dim pdfStamper As New PdfStamper(pdfReader, New FileStream( _
                newFile, FileMode.Create))

    Dim pdfFormFields As AcroFields = pdfStamper.AcroFields

    ' set form pdfFormFields

    ' The first worksheet and W-4 form
    pdfFormFields.SetField("f1_01(0)", "1")
    pdfFormFields.SetField("f1_02(0)", "1")
    pdfFormFields.SetField("f1_03(0)", "1")
    pdfFormFields.SetField("f1_04(0)", "8")
    pdfFormFields.SetField("f1_05(0)", "0")
    pdfFormFields.SetField("f1_06(0)", "1")
    pdfFormFields.SetField("f1_07(0)", "16")
    pdfFormFields.SetField("f1_08(0)", "28")
    pdfFormFields.SetField("f1_09(0)", "Franklin A.")
    pdfFormFields.SetField("f1_10(0)", "Benefield")
    pdfFormFields.SetField("f1_11(0)", "532")
    pdfFormFields.SetField("f1_12(0)", "12")
    pdfFormFields.SetField("f1_13(0)", "1234")

    ' The form's checkboxes
    pdfFormFields.SetField("c1_01(0)", "0")
    pdfFormFields.SetField("c1_02(0)", "Yes")
    pdfFormFields.SetField("c1_03(0)", "0")
    pdfFormFields.SetField("c1_04(0)", "Yes")

    ' The rest of the form pdfFormFields
    pdfFormFields.SetField("f1_14(0)", "100 North Cujo Street")
    pdfFormFields.SetField("f1_15(0)", "Nome, AK  67201")
    pdfFormFields.SetField("f1_16(0)", "9")
    pdfFormFields.SetField("f1_17(0)", "10")
    pdfFormFields.SetField("f1_18(0)", "11")
    pdfFormFields.SetField("f1_19(0)", "Walmart, Nome, AK")
    pdfFormFields.SetField("f1_20(0)", "WAL666")
    pdfFormFields.SetField("f1_21(0)", "AB")
    pdfFormFields.SetField("f1_22(0)", "4321")

    ' Second Worksheets pdfFormFields
    ' In order to map the fields, I just pass them a sequential
    ' number to mark them once I know which field is which, I 
    ' can pass the appropriate value
    pdfFormFields.SetField("f2_01(0)", "1")
    pdfFormFields.SetField("f2_02(0)", "2")
    pdfFormFields.SetField("f2_03(0)", "3")
    pdfFormFields.SetField("f2_04(0)", "4")
    pdfFormFields.SetField("f2_05(0)", "5")
    pdfFormFields.SetField("f2_06(0)", "6")
    pdfFormFields.SetField("f2_07(0)", "7")
    pdfFormFields.SetField("f2_08(0)", "8")
    pdfFormFields.SetField("f2_09(0)", "9")
    pdfFormFields.SetField("f2_10(0)", "10")
    pdfFormFields.SetField("f2_11(0)", "11")
    pdfFormFields.SetField("f2_12(0)", "12")
    pdfFormFields.SetField("f2_13(0)", "13")
    pdfFormFields.SetField("f2_14(0)", "14")
    pdfFormFields.SetField("f2_15(0)", "15")
    pdfFormFields.SetField("f2_16(0)", "16")
    pdfFormFields.SetField("f2_17(0)", "17")
    pdfFormFields.SetField("f2_18(0)", "18")
    pdfFormFields.SetField("f2_19(0)", "19")

    ' report by reading values from completed PDF
    Dim sTmp As String = "W-4 Completed for " + pdfFormFields.GetField("f1_09(0)") + " " + _
    pdfFormFields.GetField("f1_10(0)")
    MessageBox.Show(sTmp, "Finished")

    ' flatten the form to remove editting options, set it to false
    ' to leave the form open to subsequent manual edits
    pdfStamper.FormFlattening = True

    ' close the pdf
    pdfStamper.Close()

End Sub

Private Sub FillForm2()

    Dim pdfTemplate As String = "c:\Temp\PDF\CalbeeBOL.pdf"
    Dim newFile As String = "c:\Temp\PDF\CableeBOL_fw4.pdf"

    Dim pdfReader As New PdfReader(pdfTemplate)
    Dim pdfStamper As New PdfStamper(pdfReader, New FileStream( _
                newFile, FileMode.Create))

    Dim pdfFormFields As AcroFields = pdfStamper.AcroFields

    Dim sb As New StringBuilder()

    Dim intFieldNum As Integer = 1
    Dim de As New DictionaryEntry
    For Each de In pdfReader.AcroFields.Fields
        sb.Append(String.Format("{0} - {1}{2}", intFieldNum, de.Key.ToString(), Environment.NewLine))
        pdfFormFields.SetField(de.Key.ToString(), intFieldNum)
        intFieldNum = intFieldNum + 1
    Next

    textBox1.Text = sb.ToString()
    textBox1.SelectionStart = 0

    ' set form pdfFormFields

    ' The first worksheet and W-4 form

    ' report by reading values from completed PDF
    'Dim sTmp As String = "W-4 Completed for " + pdfFormFields.GetField("f1_09(0)") + " " + _
    'pdfFormFields.GetField("f1_10(0)")
    'MessageBox.Show(sTmp, "Finished")

    ' flatten the form to remove editting options, set it to false
    ' to leave the form open to subsequent manual edits
    pdfStamper.FormFlattening = True

    ' close the pdf
    pdfStamper.Close()

End Sub

结束班

0 个答案:

没有答案