Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
PDF has 3 pages, i want to Extract only 1 - 2 pages.

I am getting this error.

Access to the path 'E:\Output' is denied.

I have using iTextSharp dll.

ExtractPages("E:\\Input\\PO_1_14101056-918476_20150911.pdf", "E:\\Output", 1, 2);

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

1 solution

The error message is pretty self explanatory: you can't create a file called "Output" in the root of your E drive. In fact, it's unlikely that you can create any file in the root of any drive without Admin rights, which your app is unlikely to have. Or, if "Output" is a folder, then you don't have file creation rights for the folder, assuming your PdfCopy class uses the file name from the input.

Try creating an folder in the root of the E drive, set it's security to "any one, any access", and write your file into that, or set the access on the original folder.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900