0

I want to save files that are converted from a PDF to a PNG in a zip file. So far, I am able to convert a single PDF document into a PNG format, but if the PDF has multiple pages my application only coverts and save them individually (one at a time) in which I have to repeatedly click the Save button before the next page from the PDF is saved, etc.

I'm using Syncfusion for the document conversion and DotNetZip for zipping the files. Here's my code:

 //Loaded PDF file
  PdfLoadedDocument loadedDocument = new PdfLoadedDocument(TxtBox_FileName.Text);

 //ExportAsImage method returns specified page in the PDF document as Bitmap image 
 Bitmap[] images = loadedDocument.ExportAsImage(0, loadedDocument.Pages.Count - 1);

Image imageToConvert = null;
for (int i = 0; i < images.Length; i++)
{
  if(i == 0)
  {
    //Save converted image if PDF is single page
    imageToConvert = images[i];

    SaveFileDialog _saveFile = new SaveFileDialog();
    _saveFile.Title = "Save file";
    _saveFile.Filter = "PNG|*.png";
    _saveFile.FileName = Lbl_FileName.Text;

    if (_saveFile.ShowDialog() == DialogResult.OK)
    {
      imageToConvert.Save(_saveFile.FileName, ImageFormat.Png);

       loadedDocument.Close(true);

       imageToConvert.Dispose();
     }
  }
  else
  {
     //Save converted images if PDF is multi-page
     Image imageToConvert2 = images[i];

     SaveFileDialog _saveFile = new SaveFileDialog();
     _saveFile.Title = "Save file";
     _saveFile.Filter = "PNG|*.png";
     _saveFile.FileName = Lbl_FileName.Text;
     if (_saveFile.ShowDialog() == DialogResult.OK)
     {
        imageToConvert2.Save(_saveFile.FileName, ImageFormat.Png);

         //Create a zip file for all the images 
         string fileName = TxtBox_FileName.Text;
         Thread thread = new Thread(t =>
         {
           using (ZipFile zip = new ZipFile())
           {
              FileInfo fileInfo = new FileInfo(fileName);
              zip.AddFile(fileName);
              DirectoryInfo di = new DirectoryInfo(fileName);
              zip.Save(string.Format("{0}/{1}.zip", di.Parent.FullName, fileInfo.Name));
           }
           })
           { IsBackground = true };
           thread.Start();
          }
          loadedDocument.Close(true);

          imageToConvert2.Dispose();               
     }
 }

Thank you so much for your help!

Opu
  • 43
  • 1
  • 8

1 Answers1

0

On further analysis, we can achieve the requirement of save the PNG images (Generated using ExportAsImage method) to zip file using “CreateFromDirectory method of ZipFile https://learn.microsoft.com/en-us/dotnet/api/system.io.compression.zipfile?view=netcore-3.1 instance which is available in the System.IO.Compression namespace”. Please find the code snippet below to use the CreateFromDirectory method,

string resultPath = @"../../Result.zip";

// Save all images in a zip file.
ZipFile.CreateFromDirectory(@"../../Output", resultPath);

We have modified your code snippet and created the sample for the same and it can be downloaded from below link,

https://www.syncfusion.com/downloads/support/directtrac/general/ze/ExportImageZip1514521637

Regards,
Uthandadraja S (I work for Syncfusion)