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!