是否可以使用 itextsharp 在 asp.net C# 中将可填充的 pdf 文件与常规 pdf 文件合并。任何帮助将不胜感激。
我花了很长时间才弄清楚这一点,但这是一个有效的示例:
public static void MergeFiles(string destinationFile, string[] sourceFiles)
{
try
{
int f = 0;
String outFile = destinationFile;
Document document = null;
PdfCopy writer = null;
while (f < sourceFiles.Length) {
// Create a reader for a certain document
PdfReader reader = new PdfReader(sourceFiles[f]);
// Retrieve the total number of pages
int n = reader.NumberOfPages;
//Trace.WriteLine("There are " + n + " pages in " + sourceFiles[f]);
if (f == 0)
{
// Step 1: Creation of a document-object
document = new Document(reader.GetPageSizeWithRotation(1));
// Step 2: Create a writer that listens to the document
writer = new PdfCopy(document, new FileStream(outFile, FileMode.Create));
// Step 3: Open the document
document.Open();
}
// Step 4: Add content
PdfImportedPage page;
for (int i = 0; i < n; )
{
++i;
page = writer.GetImportedPage(reader, i);
writer.AddPage(page);
}
PRAcroForm form = reader.AcroForm;
if (form != null)
writer.CopyAcroForm(reader);
f++;
}
// Step 5: Close the document
document.Close();
}
catch(Exception)
{
//handle exception
}
}
希望这有帮助!
来源: http://www.ujihara.jp/iTextdotNET/examples/concat_pdf.java
是的。
PdfCopyFields 听起来很正确。 这样,即使您的“常规 pdf 文件”具有任何类型的注释(链接、flash 等),它们仍然可以正常工作。
或者,您可以使用 PdfStamper 打开表单,然后从“常规”PDF 添加 PdfImportedPages。 这将保留关于原始 PDF 的所有内容(元数据、结构、文档级脚本等),并从“常规 pdf”中吸收页面(没有任何不是页面内容的内容)。
遗憾的是,这里没有完美的答案,但至少有两个可行的选项可供选择。
您可以使用 PdfCopyFields 合并任何复杂的可填写 pdf 表单。它确实支持所有样式、表单字段和 pdf 功能,只需尝试此代码..
PdfReader readerfile1 = new PdfReader("File1");
PdfReader readerfile2 = new PdfReader("File2");
PdfCopyFields copyfile =
new PdfCopyFields(new FileStream("OutputFilewithFullPath", FileMode.Create));
copyfile.AddDocument(readerfile1);
copyfile.AddDocument(readerfile2);
copyfile.Close();
这将生成一个您指定的新文件,并且将是一个完整的可填写表格。
我意识到这是一个相当古老的线程,但是我遇到了类似的问题,这里有一个更干净的解决方案
public string GetMergedPdfPath(List<KeyValuePair<AccountHolder, string>> filePaths, string outputFilename)
{
string directory = ApplicationDeployment.IsNetworkDeployed ? ApplicationDeployment.CurrentDeployment.DataDirectory : NoticeConstants.Paths.TestDirectory;
string outputFilepath = directory + "\\" + outputFilename;
Document outputDocument = null;
PdfCopy outputCopier = null;
try
{
using (outputDocument = new Document())
{
using (FileStream fs = new FileStream(outputFilepath, FileMode.Create))
{
using (outputCopier = new PdfCopy(outputDocument, fs))
{
outputDocument.Open();
for (int i = 0; i < filePaths.Count; i++)
{
if (string.IsNullOrEmpty(filePaths[i].Value))
{
continue;
}
using (PdfReader reader = new PdfReader(filePaths[i].Value))
{
NoticeUtil.GetPdfPages(reader, ref outputCopier);
}
}
outputDocument.Close();
}
}
}
return outputFilepath;
}
catch (Exception ex)
{
Log.Error(ex.Message, ex);
return string.Empty;
}
}
private static List<PdfImportedPage> GetPdfPages(PdfReader reader, ref PdfCopy outputCopier)
{
List<PdfImportedPage> pages = new List<PdfImportedPage>();
try
{
for (int p = 1; p <= reader.NumberOfPages; p++)
{
outputCopier.AddPage(outputCopier.GetImportedPage(reader, p));
}
}
catch (Exception ex)
{
Log.Error(ex.Message, ex);
}
return pages;
}
SautinSoft.Pdf 库 提供以下解决方案。该库中的拆分和合并方法是完全免费,甚至用于商业用途。
using SautinSoft.Pdf;
using SautinSoft.Pdf.Facades;
var inpFiles = new List<string>() {"1.pdf", "2.pdf", "3.pdf"};
string outFile = @"Merged.pdf";
var merger = new PdfMerger();
foreach (var inpFile in inpFiles)
merger.Append(inpFile);
merger.Save(outFile);
这里是更全面的示例:https://sautinsoft.com/products/pdf/help/net/developer-guide/merge-pdf-files.php.
免责声明:我是 SautinSoft.Pdf 库的共同作者。