使用GdPicture.NET提高将多个PDF转换为多个tif图像的过程的速度

时间:2017-06-02 07:17:03

标签: c# multithreading parallel-processing

我正在使用GdPicture.NET将多个PDF转换为多个tif图像。 (using this sample code in a Windows Forms Application

我需要提高此过程的速度以适应数千个PDF文件。

下面是我用来实现线程的示例方法。然而,这混合了pdf页面。

    public void ThreadRun(string pdFilFullName, string batchDir){
                GdPictureStatus status = new GdPictureStatus();
                GdPictureImaging oGdPictureImaging = new GdPictureImaging();
                GdPicturePDF oGdPicturePDF = new GdPicturePDF();
                status = oGdPicturePDF.LoadFromFile(pdFilFullName, false);

                for (int i = 1; i <= oGdPicturePDF.GetPageCount(); i++)
                {
                    //select page
                    oGdPicturePDF.SelectPage(i);
                    //render selected page to GdPictureImage identifier
                    int rasterizedPageID = oGdPicturePDF.RenderPageToGdPictureImageEx(200.0f, true);

                    if (i == 1 || i < 10)
                    {
                        padding = "00";
                    }
                    else if (i == 10 || i < 100)
                    {
                        padding = "0";
                    }
                    else
                    {
                        padding = string.Empty;
                    }
                    //Set Image file name
                    filePath = batchDir + "\\" + padding + i + ".tif";

                    // Converting to black and White
                    oGdPictureImaging.FxBlackNWhite(rasterizedPageID, BitonalReduction.Stucki);

                    // Converting to Single pixel
                    oGdPictureImaging.ConvertTo1BppAT(rasterizedPageID);

                    // Saving each page of the PDF file to single TIFF image
                    status = oGdPictureImaging.SaveAsTIFF(rasterizedPageID, filePath, false, tiffType);
                    oGdPictureImaging.ReleaseGdPictureImage(rasterizedPageID);

                    //check for page errors
                    if (status != GdPictureStatus.OK)
                    {
                        Console.WriteLine("page error: " + pdFilFullName + status.ToString());
                   }
                   Application.DoEvents();
                }


            }
    protected void pdftotiff(string filepath){ 
    List<string> result = Directory.EnumerateFiles(filepath, "*.pdf", System.IO.SearchOption.TopDirectoryOnly).Union(Directory.EnumerateFiles(filepath, "*.tif", System.IO.SearchOption.TopDirectoryOnly)).ToList();

foreach(string file in result){
    GdPicturePDF oGdPicturePDF = new GdPicturePDF();
                                GdPictureImaging oGdPictureImaging = new GdPictureImaging();

if ((_pdFileInfo.Name.Split('.')[1] != "tif") && (oGdPicturePDF.LoadFromFile(_pdFileInfo.FullName, false) == GdPictureStatus.OK))
                               {
 batchDir = folderPath + "\\Batches\\" + _pdFileInfo.Name.Split('.')[0] + "." + batchDate.Substring(6, 2) + batchDate.Substring(4, 2);

 batchname = _pdFileInfo.Name.Split('.')[0] + "." + batchDate.Substring(6, 2) + batchDate.Substring(4, 2);
 if (!Directory.Exists(batchDir)){
 Directory.CreateDirectory(batchDir);
 }
  Thread t = new Thread(() => ThreadRun(_pdFileInfo.FullName, batchDir));
  t.Start();
  }
  }

您能提供建议/样品。

1 个答案:

答案 0 :(得分:0)

使用Parallel.ForEach来处理如下的pdf文件。

    List<string> result = Directory.EnumerateFiles(filepath, "*.pdf", System.IO.SearchOption.TopDirectoryOnly).Union(Directory.EnumerateFiles(filepath, "*.tif", System.IO.SearchOption.TopDirectoryOnly)).ToList();
        Parallel.ForEach(result, new ParallelOptions { MaxDegreeOfParallelism=3}, file =>
       {
            try
             {
                GdPictureStatus status = new GdPictureStatus();
                GdPictureImaging oGdPictureImaging = new GdPictureImaging();
                GdPicturePDF oGdPicturePDF = new GdPicturePDF();
                status = oGdPicturePDF.LoadFromFile(file, false);
         if (status == GdPictureStatus.OK)
       {
         string batchDate = filepath.Substring(filepath.LastIndexOf("\\") + 1);
         string padding = String.Empty;
         string filePath = string.Empty;

         FileInfo _pdFileInfo = new FileInfo(file);
         string batchDir = filepath + "\\Batches\\" + _pdFileInfo.Name.Split('.')[0] + "." + batchDate.Substring(6, 2) + batchDate.Substring(4, 2);
        string batchname = _pdFileInfo.Name.Split('.')[0] + "." + batchDate.Substring(6, 2) + batchDate.Substring(4, 2);
         if (!Directory.Exists(batchDir))
        {
          Directory.CreateDirectory(batchDir);
         }
         for (int i = 1; i <= oGdPicturePDF.GetPageCount(); i++)
         {
         //select page
          oGdPicturePDF.SelectPage(i);
         //render selected page to GdPictureImage identifier
        int rasterizedPageID = oGdPicturePDF.RenderPageToGdPictureImageEx(200.0f, true);

                                        if (i == 1 || i < 10)
                                        {
                                            padding = "00";
                                        }
                                        else if (i == 10 || i < 100)
                                        {
                                            padding = "0";
                                        }
                                        else
                                        {
                                            padding = string.Empty;
                                        }

            //Set Image file name
          filePath = batchDir + "\\" + padding + i + ".tif";

          // Converting to black and White
oGdPictureImaging.FxBlackNWhite(rasterizedPageID, BitonalReduction.Stucki);
         // Converting to Single pixel
oGdPictureImaging.ConvertTo1BppAT(rasterizedPageID);

        // Saving each page of the PDF file to single TIFF image
         status = oGdPictureImaging.SaveAsTIFF(rasterizedPageID, filePath, false, tiffType);
                                        oGdPictureImaging.ReleaseGdPictureImage(rasterizedPageID);
            }
            }

                                oGdPictureImaging.Dispose();
                                oGdPicturePDF.Dispose();

                            }
                            catch (Exception g)
                            {
                                throw new ApplicationException(g.Message + file);

                                return;
                            }
                        }

                        );
                    }