Steps for Converting large batches of files with Event Handling

  1. Import the trial Developer License:

    License.Import(new StreamReader(@”C:\Users\Joe\license.xml”));

  2. Set up the folders the processed files will be moved to:

    //set your source and output folders
    static String sourceFolder = @”E:\process\source”;
    static String outputFolder = @”E:\process\output”;
    static String errorFolder = @”E:\process\error”;

    //Check there is a source folder if there isn’t throw and exception
    if (!Directory.Exists(sourceFolder))
    {
    throw new InvalidOperationException();
    }

    //if there is a source folder but no output folder – create the output folder.
    if (!Directory.Exists(outputFolder))
    {
    Directory.CreateDirectory(outputFolder);
    }

    //Lastly create the error folder.
    if (!Directory.Exists(errorFolder))
    {
    Directory.CreateDirectory(errorFolder);
    }

  3. Use the JobProcessor Method to start a process:

    //Simple synchronous (no events) version
    using (JobProcessor processor = new JobProcessor())
    {

  4. For each file in Source Folder – You must set the OCR Language any other settings are optional:

    processor.JobCompletedEvent += new EventHandler(processor_JobCompletedEvent); //TAB to create method

    foreach (String pdfPath in Directory.GetFiles(sourceFolder, “*.pdf”, SearchOption.TopDirectoryOnly))
    {
    PdfToWordJobEnvelope jobEnvelope = new PdfToWordJobEnvelope();
    // OCR Language must be set
    jobEnvelope.OcrLanguage = “en”;

    //Some Optional Settings / forexample just convert the first page
    PageRange pageRange = new PageRange(new int[] { 1 });
    jobEnvelope.PageRange = pageRange;

    //Set the output format
    jobEnvelope.OutputType = SolidFramework.Converters.Plumbing.WordDocumentType.DocX;

  5. Set the Source path of the files and Submit the Job:

    //Set the Source Path
    jobEnvelope.SourcePath = pdfPath;

    //Submit the Job
    processor.SubmitJob(jobEnvelope);
    }

  6. Set a time period to give the process a chance to complete:

    //Gives the process time to complete
    Thread.Sleep(1000);

  7. In your Method Stub create a new PDFtoWord JobEnvelope:

    static void processor_JobCompletedEvent(object sender, JobCompletedEventArgs e)
    {
    PdfToWordJobEnvelope jobEnvelope = e.JobEnvelope as PdfToWordJobEnvelope;
    jobEnvelope = e.JobEnvelope as PdfToWordJobEnvelope;

  8. If there are any errors report them to the console window:

    //Trap if their are any errors
    if (jobEnvelope.Status != SolidFramework.Services.Plumbing.JobStatus.Success)
    {
    // report errors to the console window
    Console.WriteLine(Path.GetFileName(jobEnvelope.SourcePath) + ” failed because ” + jobEnvelope.Message);
    String eFolder = Path.Combine(errorFolder, Path.GetFileName(jobEnvelope.SourcePath));
    String ePath = jobEnvelope.SourcePath;
    File.Move(ePath, eFolder);
    }

  9. Files are saved to a temporary location and need to be moved with the correct file extension:

    else
    {
    //files are saved to a temporary location and need to be copied. This code builds the file name
    String wordFolder = Path.Combine(outputFolder, Path.GetFileNameWithoutExtension(jobEnvelope.SourcePath));

    //this add the file extention that must match what was chosen above.
    String wordFile = Path.Combine(wordFolder + “.docx”);

    //for each file in the jobEnvelope copy it to the file path and format above
    foreach (String wordPath in jobEnvelope.OutputPaths)
    {
    String wordDestination = Path.GetFileName(wordPath);
    File.Copy(wordPath, wordFile, true);
    }
    }