首页 文章

使用iTextSharp将现有PDF文件附加到创建的PDF页面

提问于
浏览
2

我目前正在创建(我认为应该是)一个非常简单的方法来附加两个PDF文件 .

首先,我的方法创建一个页面,其中包含所有客户端的详细信息作为数字签名 . 我有这个工作正常并保存到单页PDF文件 .

但是,我现在想要将他们签署的条款和条件附加到PDF的底部 . 我编写的解决方案使用VB.NET,但如果您愿意,可以在C#中提供答案,因为我对两者都很熟悉 . 我只是认真地无法理解iTextSharps流程 .

这是我目前的代码:

Public Sub CreateDocument() Handles btnCreate.ServerClick

    Dim path As [String] = Server.MapPath("PDFs")
    Dim document As New Document()
    Dim writer As PdfWriter = PdfWriter.GetInstance(document, New FileStream(path + "/" + _AccountNo + "-RegAgreement.pdf", FileMode.Create))

    'Removed chunk of code here, just defining content chunks and paragraphs
     for the dynamically created page, left in the construction
     part which you can see below'


    'Construct digitally signed agreement page'
    document.Open()
    document.NewPage()
    document.Add(pHeader)

    table.SpacingBefore = 30.0F
    table.SpacingAfter = 60.0F
    document.Add(table)

    pAgreement.SpacingAfter = 20.0F
    document.Add(pAgreement)

    document.Add(pSignedBy)
    document.Add(imgSig)
    document.Add(pFooter1)
    document.Add(pFooter2)
    document.Add(pFooter3)

    writer.Close()
    document.Close()

现在这是我添加到上面的子末尾以添加PDF的位 . 据我所知,您需要使用PdfCopy将PDF中的信息传输到新的Document对象(在本例中为doc) . 但是,我找不到将这些添加到动态创建的PDF中的方法 .

是否有办法在复印机中打开它然后从第2页开始复印?

Dim terms As New PdfReader(path + "/termsconditions.pdf")
    Dim doc As New Document()
    Dim copier As New PdfCopy(doc, New FileStream(path + "/" + _accountNo + "-RegAgreement2.pdf", FileMode.Create))

    'Append Ts & Cs'
    For i As Integer = 1 To terms.NumberOfPages
        Dim importedPage As PdfImportedPage = copier.GetImportedPage(terms, i)
        copier.AddPage(importedPage)
    Next

    terms.Close()
    terms.Close()

End Sub

到目前为止我看到的每个解决方案都使用了不同的方法,如页面标记或内存流,但它们都没有工作或给我我需要的结果 .

任何帮助是极大的赞赏!

UPDATE

好吧,在@mkl的建议之后,我现在用一个阅读器带回动态生成的文档,但是它返回一个空值,表明.PDF是空白的,但事实并非如此 . (我的目录中的文件已填写所有内容)

Dim copier As New PdfCopy(document, New FileStream(path + "/" + _distNo + "-RegAgreement2.pdf", FileMode.OpenOrCreate))
    Dim reader As New PdfReader(path + "/" + _distNo + "-RegAgreement.pdf")

    'retrieve dynamic document
    Dim dynamicPage As PdfImportedPage = copier.GetImportedPage(reader, 1)
    copier.AddPage(dynamicPage)

    'Append Ts & Cs
    For i As Integer = 1 To terms.NumberOfPages
        Dim importedPage As PdfImportedPage = copier.GetImportedPage(terms, i)
        copier.AddPage(importedPage)
    Next

这是因为它是在同一个子程序中完成的吗?

1 回答

  • 0

    解决了,非常感谢@mkl在这一个 .

    我遇到的问题是因为我没有为复印机创建一个Document对象来写入 .

    Dim doc As New Document()
        Dim copier As New PdfCopy(doc, New FileStream(path + "/" + _distNo + "-RegAgreement.pdf", FileMode.Create))
        'Open PDF created earlier in subroutine'
        Dim reader As New PdfReader(path + "/" + _distNo + "-Signed.pdf")
    
        doc.Open()
        'Copy first (And only) page of dynamic PDF'
        Dim dynamicPage As PdfImportedPage = copier.GetImportedPage(reader, 1)
        copier.AddPage(dynamicPage)
    
        'Append Ts & Cs'
        For i As Integer = 1 To terms.NumberOfPages
            Dim importedPage As PdfImportedPage = copier.GetImportedPage(terms, i)
            copier.AddPage(importedPage)
        Next
    
        doc.Close()
        terms.Close()
        reader.Close()
        copier.Close()
    
        'For temporary purposes, delete local file'
        'This will be done in output stream in end release'
        File.Delete(path + "/" + _distNo + "-Signed.pdf")
    

    谢谢你的帮助 . 想想我现在已经掌握了这个iTextSharp的基本程序了!

相关问题