The CombineFolder method allows you to combine (append) the files in the given folder and optionally any subfolders as well, into a single output file, or a serialized sequence of single page output files.

The conversion settings are passed as a profile, or through a custom list of settings. When converting a folder of files, all files are converted with the same conversion settings. To combine files with different setting per file, see Combining Select Pages Of Each File.

Filtering Files in the Folder

You can use the Filter and the ExcludeFilter arguments to specify which files in the folder you want to convert. The Filter is always applied to the directory contents first, then the ExcudeFilter is applied to that list of files to remove the unwanted files.

Hidden and system files are ignored, and the search pattern filters files based on a regular expression match of the long name of a file. The Filter defaults to all files in the folder (*.*) if String.Empty or null are passed. ExcludeFilter is ignored when String.Empty or null is passed.

Multiple filters can be combined using the pipe (|) character, such as *.doc|*.pdf to process only Word and PDF files. The table below lists some examples of filtering directory contents.

Filter

Exclude Filter

Action

*.pdf

String.Empty

Process only PDF documents.

*.*

*.tif|*.jpg

Process all documents except TIFF and JPEG images.

*.doc|*.docx|*.txt

Draft_*

Process all Word and Text documents except those starting with Draft_.

Sorting the Files for Pickup

Starting with Document Conversion Service 3.0.029, this method now includes the ability to order the files by name, date created or date modified when picking up files from the Input folder.

Configuring the Sort Mode and Order

Sort order defaults to name and ascending when picking up files. Files in the root of the input folder are picked up and sorted first. If sub folders are enabled, they are searched in alphabetical order. Any files in each sub folder are then sorted and returned. Uses the PNFileSortMode enumeration.

There are four sorting modes that can be used:

oNone - No ordering is used. Files are returned in the order they were given to us from the underlying file system.

oName - This is the default if the setting is not found or the value is incorrect. Files are sorted based on the full path name of the source file in the input folder.

oDateCreated - Files are sorted based on their creation date. For watch folders where files are dropped, a file can be moved or copied into the folder. If the files are moved into the Input folder they will retain their original created date. Copying a file into the Input folder wil set the created date to the time of the copy.

oDateModified - Files are sorted based on when they were last modified on the computer.

The order of the files is either Ascending or Descending. Uses the PNFileSortOrder enumeration.

oAscending - sorted the files from low to high: 0-9, A-Z.

oDescending - sorts the files from high to low: Z-A, 9-0.

Combining a Folder of Files

The code sample below will convert all files except TIFF, JPEG and BMP images from the folder C:\Test\Input\. Any subfolders are also be searched for files to convert.

The combined file will be created using the conversion settings from the profile PDF 200dpi OptimizedColor. You can change this to use any profile you require.

A sort order of DateCreated is set, meaning files created first will be submitted for processing first. This will determine the order of the files and pages in the combined file at the end.

 

PNCombineItem resultsItem = null;

String inputDir = @"C:\Test\Input";
String outputDir = @"C:\Test\CombinedOutput";
String outputName = "CombinedInput";
 

// Directory must exist

if ( !Directory.Exists(outputDir) )

{

    Directory.CreateDirectory(outputDir);

}

 
resultsItem = 
    PNConverter.CombineFolder(inputDir, // folder of files

                              true, // include subfolders

                              "*.*", // filter

                              "*.tif|*.jpg|*.bmp", // exclude filter
                              outputDir, // output folder
                              baseName,  // name of combined file
                              false, // overwrite
                              false, // create results log 
                              "PDF 200dpi OptimizedColor", // profile
                              String.Empty, // File-ext
                              String.Empty, // MIME
                              null, // user settings
                              String.Empty, // not using remote conversion (DCOM)
                              String.Empty, // use default working folder
                              String.Empty  // Log path

                              PNFileSortMode.DateCreated, // sort by created date

                              PNFileSortOrder.Asccending); // A-Z, 0-9
                              );

 

 

The created file, in this case a multipaged PDF document, will be placed in the specified output folder C:\Test\CombinedOutput and named CombinedInput.pdf. Files matching the ExcludeFilter (*.tif, *.jpg) were not converted.

 

Reading the Results

When combining a folder of files, a PNCombineItem object is returned. This object contains information about the original combine request, the files found to be converted, list of the output files created and a collection of PNConversionResult objects that lists the results of the conversion for each input file. The results of the conversion can be a list of created files or a collection of error messages detailing why the files were not combined.

This code sample traverses the returns results from the above combine and lists the input files used and the files created.

 

if (resultsItem != null)
{

    int idx = 0;

 
    Console.WriteLine("*******************************");
    Console.WriteLine("* Combined ITEM               *");
    Console.WriteLine("*******************************");
    Console.WriteLine("BaseName: " + resultsItem.OutputBaseName);
    Console.WriteLine("Directory: " + resultsItem.OutputDirectory);
    Console.WriteLine("Input Files:");
    
    foreach (String inputFile in resultsItem.InputFiles)
    {
        Console.WriteLine("    " + inputFile);
    }
    
    Console.WriteLine("Combined Output:");
    if (resultsItem.CombinedOutputFileList.Count == 0)
    {
        Console.WriteLine("    None");
    }
    foreach (String combinedFile in resultsItem.CombinedOutputFileList)
    {
        Console.WriteLine("    " + combinedFile);
    }
 
    if ( resultsItem.HasErrors() == true)
    {
        foreach (PNConversionResultError errorItem in resultsItem.Errors)
        {
            Console.WriteLine("    Error: " + errorItem.Value);
        }
    }

    else

    {

        foreach (PNConversionItem item in results)

        {

            idx++;

            Console.WriteLine("*******************************");

            Console.WriteLine(String.Format("* Item {0}                      *", idx));

            Console.WriteLine("*******************************");

                    

            if (item != null)

            {

                Console.WriteLine("Item: " + item.SourceFilePath);

                Console.WriteLine("OutputDir: " + item.OutputDirectory

                Console.WriteLine("BaseName: " + item.OutputBaseName);

 

                if (item.HasErrors() == false)

                {

                    foreach (PNConversionResultOutputFile outputfile in

                             item.ConversionResult.OutputFiles)

                    {

                        Console.WriteLine("    Converted to: " + outputfile.OutputFilePath);

                    }

                }

                else

                {

                    foreach (PNConversionResultError errorItem in

                             item.ConversionResult.Errors)

                    {

                        Console.WriteLine("    Error: " + errorItem.Value);

                    }

                }

            }

        }

    }

}

 

The console output from the above code is shown below.