我在Windows窗体应用程序中使用了版本5.5.8.0的iTextSharp。这是我用来合并两个pdf文件的代码。我的任务是在合并文件中添加页码。
private void MergePDFFiles()
{
string fileSuffix = DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf";
string[] lstFiles = new string[2];
lstFiles[0] = @"/File_1.pdf";
lstFiles[1] = @"/File_2.pdf";
PdfReader reader = null;
Document sourceDocument = null;
PdfCopy pdfCopyProvider = null;
PdfImportedPage importedPage;
string outputPdfPath = @"/MergedPageNo_" + fileSuffix;
sourceDocument = new Document();
pdfCopyProvider = new PdfCopy(sourceDocument, new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create));
sourceDocument.Open();
try
{
for (int f = 0; f < lstFiles.Length - 0; f++)
{
int pages = get_pageCount(lstFiles[f]);
reader = new PdfReader(lstFiles[f]);
for (int i = 1; i <= pages; i++)
{
importedPage = pdfCopyProvider.GetImportedPage(reader, i);
pdfCopyProvider.AddPage(importedPage);
}
reader.Close();
}
sourceDocument.Close();
}
catch (Exception ex)
{
throw ex;
}
}`