0
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 提取到新文件。但我收到“拒绝访问路径”错误消息。但我收到“拒绝访问路径”错误消息。

编辑:

此行抛出错误:

pdfCopyProvider = new PdfCopy(sourceDocument, 

            new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create));
4

1 回答 1

0

尝试这个,

PdfReader R = new PdfReader(srcPdfFilePath);
using (PdfStamper stamper = new PdfStamper(R, new FileStream(dPdfFile, FileMode.Create)))
{
// if you want to do any changes in new pdf do here.
stamper.Close();
}
R.Close();

笔记:

源 PDf 路径和名称与新的 pdf 路径和名称不应相同。因为文件在打开时不能被覆盖。

于 2013-10-21T06:38:48.517 回答