得到合并的pdf,但损坏的文件,我不能打开使用以下代码。“targetPDF”是最终合并的pdf文件,“fileNames”拥有所有的唯一pdfs。请帮帮忙。提前谢谢。
Using (FileStream stream = new FileStream(targetPDF, FileMode.Create, FileAccess.Write))
{
Document document = new Document();
PdfCopy pdf = new PdfCopy(document, stream);
if (pdf == null)
{
return;
}
document.Open();
foreach (string file in fileNames)
{
PdfReader reader = new PdfReader(file);
reader.ConsolidateNamedDestinations();
for (int i = 1; i <= reader.NumberOfPages; i++)
{
PdfImportedPage page = pdf.GetImportedPage(reader, i);
pdf.AddPage(page);
//pdf.AddDocument(new PdfReader(file));
// pdf.AddPage(pdf.GetImportedPage(reader, 1));
}
reader.Close();
}
}发布于 2017-05-15 19:41:13
更改这一行
Document document = new Document();
PdfCopy pdf = new PdfCopy(document, stream);致:
using(Document document = new Document())
{
using(PdfCopy pdf = new PdfCopy(document, stream))
{
//do staff here...
}
}这样,工作完成后,所有的流都会关闭,文件也不会被锁定。
https://stackoverflow.com/questions/43987342
复制相似问题