none
Merging PDF Files using C# - file created but the supposed merged file cannot be opened RRS feed

  • Question

  • So, currently I have a program that runs through folders to get all of the files that need to be merged. After that, it passes those to a method that uses SautinSoft's PdfVision to merge them. The merge returns a 0 (meaning merged successfully), but when I double click the output file, it displays the message at the bottom, below.

    Here is my code so far. First, the method that reads in the PDF Files - 

    public void ProcessFiles(string path)
            {
                Console.WriteLine(" - - - - - - - - - - - - - - - -");
                try
                {
                    List<string> pathsForFiles = new List<string>();
                    String storeNum = "";
                    //Every subdirectory in the directory
                    foreach (string d in Directory.GetDirectories(path))
                    {
                        storeNum = (Path.GetFileName(d));
    
                        //Every item in the folder
                        foreach (string filePaths in Directory.GetFiles(d))
                        {
                            pathsForFiles.Add(filePaths);
                            Console.WriteLine(filePaths);
                        }
    
                        //WriteToXML(storeNum, pathsForFiles);
                        pathsForFiles.Clear();
                        ProcessFiles(d);
                    }
                    string dirForOutput = "C:\\Users\\User\\Desktop";
                    MergeFiles(pathsForFiles, storeNum, dirForOutput);
                    Console.ReadKey();
    
                }
                catch (System.Exception excpt)
                {
                    Console.WriteLine(excpt.Message);
                }
            } // end ProcessFiles()

    And here is the method for Merging PDF files -

    public void MergeFiles(List<string> f, string storeNum, string outputDirectory)
            {
                SautinSoft.PdfVision v = new SautinSoft.PdfVision();
    
                string[] files = new string[f.Count];
                int i = 0;
                foreach (string s in f)
                {
                    files[i] = s;
                    i++;
                }
                string outName = outputDirectory + "\\" + storeNum + " Original As-Builts.pdf";
                int ret = v.MergePDFFileArrayToPDFFile(files, @outName);
                //0 - merged successfully
                //1 - error, can't merge PDF documents
                //2 - error, can't create output file, probably it used by another application
                //3 - merging failed
                //4 - merged successfully, but some files were not merged
                Console.WriteLine("Merge Status - " + ret);
            }

    The message I get when I try to open up the output file


    • Edited by JxkeZ Wednesday, August 15, 2018 5:15 PM
    Wednesday, August 15, 2018 2:29 PM

Answers

  • Hi JxkeZ,

    Thank you for posting here.

    >>The merge returns a 0 (meaning merged successfully), but when I double click the output file, it displays the message at the bottom, below.

    Please put the PDF files under the folder of the subdirectory of the root directory.

    Here is the complete code:

            static void Main(string[] args)
            {
                Program program = new Program();
                program.ProcessFiles(@"C:\Users\Administrator\Desktop\New folder");
            }
            public void ProcessFiles(string path)
            {
                Console.WriteLine(" - - - - - - - - - - - - - - - -");
                try
                {
                    List<string> pathsForFiles = new List<string>();
                    String storeNum = "";
                    //Every subdirectory in the directory
                    foreach (string d in Directory.GetDirectories(path))
                    {
                        storeNum = (Path.GetFileName(d));
                        Console.WriteLine(d+storeNum);
                        
                        //Every item in the folder
                        foreach (string filePaths in Directory.GetFiles(d))
                        {
                            pathsForFiles.Add(filePaths);
                            Console.WriteLine(filePaths);
                        }
                    }
                    string dirForOutput = @"C:\Users\Administrator\Desktop";
                    MergeFiles(pathsForFiles, storeNum, dirForOutput);
                    Console.ReadKey();
                }
                catch (Exception excpt)
                {
                    Console.WriteLine(excpt.Message);
                }
            } // end ProcessFiles()
            public void MergeFiles(List<string> f, string storeNum, string outputDirectory)
            {
                PdfVision v = new PdfVision();
                string[] files = new string[f.Count];
                int i = 0;
                foreach (string s in f)
                {
                    files[i] = s;
                    i++;
                }
                string outName = outputDirectory + "\\" + storeNum + " Original As-Builts.pdf";
                int ret = v.MergePDFFileArrayToPDFFile(files, @outName);
                //0 - merged successfully
                //1 - error, can't merge PDF documents
                //2 - error, can't create output file, probably it used by another application
                //3 - merging failed
                //4 - merged successfully, but some files were not merged
                Console.WriteLine("Merge Status - " + ret);
            }

    Regards,

    JianGuo


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by JxkeZ Thursday, August 16, 2018 1:36 PM
    Thursday, August 16, 2018 6:47 AM

All replies

  • Hi,

    You can try using Spire.Pdf.dll (https://www.nuget.org/packages/Spire.pdf/), and refer to the below code to see if it works.

    String[] files = new String[] { "Sample1.pdf", "Sample2.pdf", "Sample3.pdf" };
    
    string outputFile = "result.pdf";
    
    PdfDocumentBase doc = PdfDocument.MergeFiles(files);
    
    doc.Save(outputFile, FileFormat.PDF);

    Thursday, August 16, 2018 6:24 AM
  • Hi JxkeZ,

    Thank you for posting here.

    >>The merge returns a 0 (meaning merged successfully), but when I double click the output file, it displays the message at the bottom, below.

    Please put the PDF files under the folder of the subdirectory of the root directory.

    Here is the complete code:

            static void Main(string[] args)
            {
                Program program = new Program();
                program.ProcessFiles(@"C:\Users\Administrator\Desktop\New folder");
            }
            public void ProcessFiles(string path)
            {
                Console.WriteLine(" - - - - - - - - - - - - - - - -");
                try
                {
                    List<string> pathsForFiles = new List<string>();
                    String storeNum = "";
                    //Every subdirectory in the directory
                    foreach (string d in Directory.GetDirectories(path))
                    {
                        storeNum = (Path.GetFileName(d));
                        Console.WriteLine(d+storeNum);
                        
                        //Every item in the folder
                        foreach (string filePaths in Directory.GetFiles(d))
                        {
                            pathsForFiles.Add(filePaths);
                            Console.WriteLine(filePaths);
                        }
                    }
                    string dirForOutput = @"C:\Users\Administrator\Desktop";
                    MergeFiles(pathsForFiles, storeNum, dirForOutput);
                    Console.ReadKey();
                }
                catch (Exception excpt)
                {
                    Console.WriteLine(excpt.Message);
                }
            } // end ProcessFiles()
            public void MergeFiles(List<string> f, string storeNum, string outputDirectory)
            {
                PdfVision v = new PdfVision();
                string[] files = new string[f.Count];
                int i = 0;
                foreach (string s in f)
                {
                    files[i] = s;
                    i++;
                }
                string outName = outputDirectory + "\\" + storeNum + " Original As-Builts.pdf";
                int ret = v.MergePDFFileArrayToPDFFile(files, @outName);
                //0 - merged successfully
                //1 - error, can't merge PDF documents
                //2 - error, can't create output file, probably it used by another application
                //3 - merging failed
                //4 - merged successfully, but some files were not merged
                Console.WriteLine("Merge Status - " + ret);
            }

    Regards,

    JianGuo


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by JxkeZ Thursday, August 16, 2018 1:36 PM
    Thursday, August 16, 2018 6:47 AM
  • The problem is, you are not closing SautinSoft.PdfVision object and still the handle exists. In normal case it will be cleared when next time the GC calls.

    Instead of waiting for it, you can explicitly close it once merge is done as follows.

    public void MergeFiles(List<string> f, string storeNum, string outputDirectory)
            {
                using(SautinSoft.PdfVision v = new SautinSoft.PdfVision())
                {
                string[] files = new string[f.Count];
                int i = 0;
                foreach (string s in f)
                {
                    files[i] = s;
                    i++;
                }
                string outName = outputDirectory + "\\" + storeNum + " Original As-Builts.pdf";
                int ret = v.MergePDFFileArrayToPDFFile(files, @outName);
                //0 - merged successfully
                //1 - error, can't merge PDF documents
                //2 - error, can't create output file, probably it used by another application
                //3 - merging failed
                //4 - merged successfully, but some files were not merged
                Console.WriteLine("Merge Status - " + ret);
               }
            }
    


    Sai Kumar K (MCP)
    Blog: Sai's Stuff.
    WebSite: SantoshTechnologies.

    Thursday, August 16, 2018 9:24 AM