如题
已经找到解决方法,一直没时间传上来和大家分享一下,
使用的是itextsharp
/// <summary>
/// 合并PDF文件
/// </summary>
/// <param name="lstFiles">要合并的文件列表</param>
/// <returns></returns>
private bool MergePDF(List<string> lstFiles)
{
bool bReturn = false;
try
{
int f = 1;
PdfReader reader = new PdfReader(lstFiles[f]);
int n = reader.NumberOfPages;
Document document = new Document(reader.GetPageSizeWithRotation(1));
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(lstFiles[0], FileMode.Create));
document.Open();
PdfContentByte cb = writer.DirectContent;
PdfImportedPage page;
int rotation;
while (f < lstFiles.Count)
{
int i = 0;
while (i < n)
{
i++;
document.SetPageSize(reader.GetPageSizeWithRotation(i));
document.NewPage();
page = writer.GetImportedPage(reader, i);
rotation = reader.GetPageRotation(i);
if (rotation == 90 || rotation == 270)
{
cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
}
else
{
cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
}
}
f++;
if (f < lstFiles.Count)
{
reader = new PdfReader(lstFiles[f]);
n = reader.NumberOfPages;
}
}
document.Close();
bReturn = true;
}
catch
{
bReturn = false;
}
return bReturn;
}
}