下面的代碼工作正常上iTextSharp的5.2.1的iText/iTextSharp的5.5.0與PDF突發錯誤
var file= new FileInfo(args[0]);
string name = file.Name.Substring(0,file.Name.LastIndexOf("."));
// we create a reader for a certain document
var reader = new PdfReader(args[0]);
// we retrieve the total number of pages
int numberOfPages = reader.NumberOfPages;
Console.WriteLine("There are " + n + " pages in the original file.");
Document document;
string filename;
PdfCopy copy;
for (int pageNumber = 1; i <= numberOfPages; i++)
{
filename = pageNumber.ToString();
filename = "_" + filename + ".pdf";
// step 1: creation of a document-object
document = new Document(reader.GetPageSizeWithRotation(pageNumber));
// step 2: we create a writer that listens to the document
copy = new PdfCopy(document, new FileStream(name + filename,FileMode.Create));
// step 3: we open the document
document.Open();
copy.AddPage(copy.GetImportedPage(reader, pageNumber));
// step 5: we close the document
document.Close();
}
但它拋出下面的錯誤上iTextSharp的5.5.0,
頁1被要求但該文件只有0頁。
它似乎是最後一行實際上篡改了讀者實例。有人可以幫我弄清楚嗎?現在我通過爲每個頁面重新創建一個PdfReader實例來解決這個問題,但是對於大型PDF文件來說這很慢。
你能解釋一下工作嗎? – chrispepper1989
@ chrispepper1989,自從我寫出答案以來已經有一段時間了,但從根本原因分析來看,漫步是顯而易見的,不要稱爲「文檔」。關閉();'結束 – imgen