Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

What I have tried:

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!
Posted
Updated 1-Jun-20 6:43am
Comments
Richard MacCutchan 1-Jun-20 11:21am    
You are creating a new zip file for each image. You should create the zip file before the loop starts and add every image to it as you convert them.
Maciej Los 1-Jun-20 11:30am    
Sounds like an answer.
Richard MacCutchan 1-Jun-20 11:41am    
Actually it's a guess. :)
Member 14766911 1-Jun-20 12:50pm    
Hi, thanks for your suggestion. I have moved the loop and the saveFileDialog codes and placed them inside - where I'm creating the zip file, but I'm still having the same result.
It still converts all the pages in the PDF into images but I have to constantly click on the Ok button as the next SaveFileDialog box pops up in order to save the next image that has been converted.

Richard MacCutchan 1-Jun-20 12:52pm    
That is because you are trying to save it every time you add an image to the zip; you should not do that until the loop finishes.

1 solution

Quote:
...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.


Do you know what WYWIWYG acronym means?
- What You Write Is What You Get.

Change your code accordingly to call save method only once.

Tip:
I'd suggest to:
1. create temporary folder for ech pdf document;
2. save all images (using naming convention: $"page{i}") into that directory;
3. zip entire directory into single archive file;
4. remove temporary folder.


Good luck!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900