0

I am using this to merge 2 pad files:

public static void MergePages(string outputPdfPath, string[] lstFiles)
{
    lstFiles = new string[2] { @"Downloads\Certificates\119.FDV-3686.pdf", 
                               @"Downloads\Certificates\119.FDV-3686.pdf" };
    outputPdfPath = @"Downloads\Certificates\";

    PdfReader reader = null;
    Document sourceDocument = null;
    PdfCopy pdfCopyProvider = null;
    PdfImportedPage importedPage;
    sourceDocument = new Document(); 
    pdfCopyProvider = new PdfCopy(sourceDocument,
    new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create));
    sourceDocument.Open();
    try
    {
        for (int f = 0; f < lstFiles.Length - 1; f++)
        {
            int pages = 1;
            reader = new PdfReader(lstFiles[f]);
            //Add pages of current file
            for (int i = 1; i <= pages; i++)
            {
                importedPage = pdfCopyProvider.GetImportedPage(reader, i);
                pdfCopyProvider.AddPage(importedPage);
            }
            reader.Close();
        }
        sourceDocument.Close();
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

The 2 files exists in my project directory but it throws error:

Could not find a part of the path 'C:\Program Files (x86)\IIS Express\~\Downloads\Certificates\119.FDV-3686.pdf'.

I don't understand why it goes to C drive since files are in the same project.

enter image description here

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
CodingManiac
  • 123
  • 1
  • 1
  • 9

1 Answers1

2

(1) One problem might be that your design-time pdf files are not copied to application output directory during compilation. Therefore they are not available run-time.

If you want to copy file(s) from your solution folder to application output directory, you can set to file property "Copy to Output directory" to "Copy always" or "Copy if newer". More discussion about topic is found i.e here.

File properties can be set by selecting file under solution explorer.

(2) Another problem is that you do not set root directory of file path. I recommend you to express file path with following style:

var rootLocation = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase.ToString()).LocalPath);
var filePath1  = Path.Combine(rootLocation,@"Downloads\Certificates\filename1.pdf"); 
var filePath2  = Path.Combine(rootLocation,@"Downloads\Certificates\filename2.pdf"); 
..
Risto M
  • 2,919
  • 1
  • 14
  • 27