我目前正在创建(我以为会这样)追加两个PDF文件的一个非常简单的方法。用iTextSharp追加创建的PDF页面与现有的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
这是因为它是在相同的子程序中完成的吗?
创建您的动态PDF,就像您在第一个代码块中所做的一样。然后使用'PdfCopy'创建一个新的PDF,在其中添加新创建的动态PDF和包含所有这些术语的PDF。 – mkl
@mkl我试过了,但是我无法对“文档”对象做任何事情,因为它被锁定到PdfWriter中,如果我关闭了,我又得到另一个错误,因为文档仍然被某些东西使用。 – SamBC
@SamBC不,不是'文档'对象。一个'Document'实例只能用于一个任务。因此,创建了您的初始动态PDF后,您的磁盘上就有一个PDF文件。现在,从该文件创建一个“PdfReader”实例,并在第二个代码块中导入来自该阅读器的所有页面,然后从“PdfReader terms”导入页面。 – mkl