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;
    }
}

I'm using asp.net application. I'm using the following method to Extract a Range of Pages from Existing PDF to a new File. But I am getting "Access to path is denied" error message.But I am getting "Access to path is denied" error message.

Edit:

This line is throwing the error:

pdfCopyProvider = new PdfCopy(sourceDocument, 

            new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create));
Chris Haas
  • 53,986
  • 12
  • 141
  • 274
Billy
  • 825
  • 6
  • 21
  • 36
  • *Access to path is denied* - Such a message most likely is related to local file system permissions and, therefore, not related with iTextSharp as such. Which code line triggers that message? Please make sure your process has sufficient permissions to access `sourcePdfPath` and `outputPdfPath` as required by the code. – mkl Oct 21 '13 at 06:41

1 Answers1

0

Try this,

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();

Note:

The source PDf path & name and new pdf path & name should not be same. Because file can't be overwrite when it is open.

  • Whether or not the OP should use `PdfStamper` instead of `PdfCopy,` depends on the very requirements of his task. It's a good idea, though, that you stress that *the source PDf path & name and new pdf path & name should not be same.* – mkl Oct 21 '13 at 06:47
  • sorry, I'm still getting error message that "Access to the Path is denied" – Billy Oct 21 '13 at 09:23
  • @Billy Have you checked which line throws that error? Have you checked the permissions for the paths involved? – mkl Oct 21 '13 at 10:18
  • @mkl, question updated. How to check for the permissions for the paths involved? – Billy Oct 21 '13 at 11:00
  • Just look this you will get any idea. (http://stackoverflow.com/questions/4877741/access-to-the-path-is-denied), And also check that src PDF file opened by some other application. – Thirusanguraja Venkatesan Oct 21 '13 at 11:09
  • According to your question update, the `outputPdfPath` is likely to be the culprit. Is your application allowed to write there? And is there possibly some existing PDF currently accessed by some other code? – mkl Oct 21 '13 at 11:48
  • Yes, I have set the Permissions by right clicking on the Drive and choosing the Security Tab. – Billy Oct 21 '13 at 12:11
  • You are aware that setting the drive root permissions does not necessarily set the permissions inside each sub-folder? – mkl Oct 21 '13 at 20:08