The ConvertFileList method allows you to convert a list of files from various locations in a single call. Each file in the list can optionally have different conversion settings and different output locations. This is different from ConvertFolder where all files are converted with the same settings.

Building the List of Files

The list of files is passed to the ConvertFileList method as a collection of PNConvertFileInfo objects.

The PNConvertFileInfo class requires the path to the source file and two optional arguments - the path to the output folder and a list of additional conversion settings to use when the source file is converted.

If an output folder is specified in the, this folder must exist before calling the conversion method. If this path is left empty the output folder specified in the ConvertFileList call is used. If that folder path is also empty, the file will be created in the same location as the source file.

The settings provided in the PNConvertFileInfo class are used in addition to the conversion settings passed to the ConvertFileList method either as a profile or through the settings list parameter.

A sample list of files to convert is created below. This list will output each file into its own folder. The second file in the list also includes additional settings to use when converting the file.

 

IList<PNConvertFileInfo> fileList = new List<PNConvertFileInfo>();

IList<PNSetting> filesettings = new List<PNSetting>();

 

// This file uses only the conversion settings from the profile

fileList.Add(new PNConvertFileInfo(@"C:\Test\InputFiles\File1.pdf",

                                   @"C:\Test\Output\ConvertedPDF\",

                                   null));

 

// This file also changes the conversion settings to 300 dpi and

// causes the Word converter to print markup.

filesettings.Add( new PNSetting("Devmode settings;Resolution", "300")); // driver setting

filesettings.Add( new PNSetting("Microsoft.Word.Document.PrintOut.Item", // converter setting

                                "DocumentAndMarkup") );

fileList.Add(new PNConvertFileInfo(@"C:\Test\InputFiles\File1.doc",

                                   @"C:\Test\Output\ConvertedDocs\",

                                   filesettings));

 

Converting the List of Files

The code sample below uses the file list created above. It first checks the that all of the output paths exist and creates them if necessary, then calls ConvertFileList to convert all files in the list and place the created files in the output folder specified.

The first file in the list will use only the conversion settings from the profile TIFF 200dpi OptimizedColor.

The second file has additional settings: the first to change the image resolution from 200 dpi to 300 dpi, and the second setting to tell the Word converter.to have any markup (comments, review) visible in the converted file.

 

IList<PNConversionItem> results = new List<PNConversionItem>();

 

// Test output, directories need to be created

foreach (PNConvertFileInfo info in fileList)

{

    if (!String.IsNullOrEmpty(info.OutputPath) && 

        !Directory.Exists(info.OutputPath))

    {

        Directory.CreateDirectory(info.OutputPath);

    }

}

 

results = PNConverter.ConvertFileList(fileList,

                                      String.Empty, // no output folder

                                      String.Empty, // no converted file name

                                      false,  // do not overwrite

                                       false, // remove file ext

                                       false, // create log

                                       "TIFF 200dpi OptimizedColor", // settings

                                       String.Empty, // extensison profile

                                       String.Empty, // MIME profile

                                       null, // User settings

                                       String.Empty, // not using remote conversion (DCOM)

                                       String.Empty, // use default working folder

                                       String.Empty); 

 

 

The created files, in this case TIFF images, will be placed in the specified output folder for each file. The name of the original file, including the extension, is used as the base name for the new file.

 

Reading the Results Collection

When converting a list of files, the results are returns as a collection of PNConversionItem object, one for every file sent to be converted. Each PNConversionItem object contains information about the original conversion request and an internal PNConversionResult object that lists the results of the conversion. The results of the conversion can be a list of created files or a collection of error messages detailing why the file was not converted.

This code sample traverses the returns results from the above conversion and lists the files created.

 

if (results != null)

{

    int idx = 0;

    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("      " + item.OutputDirectory);

            Console.WriteLine("      " + 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.