public void ExtractPages(string sourcePdfPath, string outputPdfPath, int startPage, int endPage)
{
PdfReader reader = null;
Document sourceDocument = null;
PdfCopy pdfCopyProvider = null;
PdfImportedPage importedPage = null;
try
{
// Intialize a new PdfReader instance with the contents of the source Pdf file:
reader = new PdfReader(sourcePdfPath);
// For simplicity, I am assuming all the pages share the same size
// and rotation as the first page:
sourceDocument = new Document(reader.GetPageSizeWithRotation(startPage));
// Initialize an instance of the PdfCopyClass with the source
// document and an output file stream:
pdfCopyProvider = new PdfCopy(sourceDocument,
new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create));
sourceDocument.Open();
// Walk the specified range and add the page copies to the output file:
for (int i = startPage; i <= endPage; i++)
{
importedPage = pdfCopyProvider.GetImportedPage(reader, i);
pdfCopyProvider.AddPage(importedPage);
}
sourceDocument.Close();
reader.Close();
}
catch (Exception ex)
{
throw ex;
}
}
我正在使用asp.net應用程序。我正在使用以下方法將現有PDF中的一系列頁面提取到新文件。但我得到「訪問路徑被拒絕」錯誤消息。但我得到「訪問路徑被拒絕」錯誤消息。從現有PDF提取一系列頁面到一個新文件
編輯:
此行引發錯誤:
pdfCopyProvider = new PdfCopy(sourceDocument,
new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create));
*訪問路徑被拒絕* - 這種消息很可能與本地文件系統權限有關,因此與iTextSharp無關。哪個代碼行觸發該消息?請確保您的進程有足夠的權限來訪問代碼所需的'sourcePdfPath'和'outputPdfPath'。 – mkl