0

I am trying to split pdf files into separate individual pdfs. I can't figure out why keep giving me access to the path is denied error. I set my user name as an admin and full control for the folder.

        public void ExtractPage()
    {

        string sourcePdfPath = "C:\\SOURCECODE\\OAPdfSplitter\\OAPdfSplitter\\PDFSource\\Xerox Scan_05022019151254.PDF";
        string outputPdfPath = "C:\\SOURCECODE\\OAPdfSplitter\\OAPdfSplitter\\PDFOutput";
        int pageNumber = 1;

        PdfReader reader = null;
        Document document = null;
        PdfCopy pdfCopyProvider = null;
        PdfImportedPage importedPage = null;

        try
        {
            reader = new PdfReader(sourcePdfPath);
            document = new Document(reader.GetPageSizeWithRotation(pageNumber));

        pdfCopyProvider = new PdfCopy(document,new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create));

            document.Open();

            importedPage = pdfCopyProvider.GetImportedPage(reader, pageNumber);
            pdfCopyProvider.AddPage(importedPage);
            document.Close();
            reader.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error message:", MessageBoxButton.OK, MessageBoxImage.Error);
        }
AliAzra
  • 889
  • 1
  • 9
  • 28
  • 1
    Is the path for outputPdfPath by any chance a directory? –  Mar 21 '19 at 16:20
  • Hi, It is just a folder path name.. I am not sure what you mean Directory? – AliAzra Mar 21 '19 at 16:22
  • 1
    If outputPdfPath is a folder (= directory), then look at your code here: `new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create)` and ponder a bit about what this code is trying to do... –  Mar 21 '19 at 16:23
  • You are right, This part cause problem but FileStream is just Initializes a new instance of the FileStream class for the specified file handle with specific permission – AliAzra Mar 21 '19 at 16:27
  • "_the specified file handle with specific permission_" File handle? Where is a file handle in your code coming from? –  Mar 21 '19 at 16:28
  • Not sure... I just copied this code from https://www.codeproject.com/Articles/559380/SplittingplusandplusMergingplusPdfplusFilesplusinp – AliAzra Mar 21 '19 at 16:29
  • 1
    Copying code from somewhere without understanding it will rarely yield successful results and in [most cases not work](https://en.wikipedia.org/wiki/Cargo_cult_programming). Doesn't it look a bit strange to you that you are trying to open a **File**Stream on a **directory/folder** path? –  Mar 21 '19 at 16:30
  • You are right. but problem is PdfCopy needs fileStream and not sure how this is working. But i fixed it now. Just put file name rather than folder name. string outputPdfPath = "C:\\SOURCECODE\\OAPdfSplitter\\OAPdfSplitter\\PDFSource\\1.PDF"; – AliAzra Mar 21 '19 at 16:33
  • Thank you for help. – AliAzra Mar 21 '19 at 16:33

0 Answers0