Click here to Skip to main content
14,690,522 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I'm trying to save image files which are converted from PDF to PNG. I want my application to save the converted image if the PDF was a single page document using the "SaveFileDialog", and if the PDF file was a multi-page document, I then want my application to save them into a folder using the "FolderBrowserDialog".

My problem is that if the PDF file was a multi-page document, my code would first save the first image (after conversion) using the "SaveFileDialog" before attempting to save the rest of the images using "FolderBrowserDialog".

Here is what I've tried.

What I have tried:

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_OriginalFileName.Text;


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

            imageToConvert.Dispose();
        }
        else if (_saveFile.ShowDialog() == DialogResult.Cancel)
        {
            return;
        }
    }
    else
    {
        if (i > 0)
        {
            // Save converted Images if PDF is multi-page
            Image imageToConvert2 = images[i];

            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.ShowDialog();
            fbd.Description = "Select the folder you want save your files into.";

            string pathString = Path.Combine(fbd.SelectedPath, subFolder);
            Directory.CreateDirectory(pathString);

            if (fbd.ShowDialog() == DialogResult.Cancel)
            {
                return;
            }
            
                string saveFileNamesPNG = string.Format(Lbl_OriginalFileName.Text + "_" + i.ToString() + ".png", ImageFormat.Png);
                imageToConvert.Save(Path.Combine(pathString, saveFileNamesPNG));
           
            imageToConvert.Dispose();
        }
    }
}


I would really appreciate any help.
Posted
Updated 8-Oct-20 9:15am
Comments
Richard MacCutchan 8-Oct-20 10:49am
   
Why do you need two different dialogs? Just use SaveFileDialog and let the user decide where to save it and what to name it. You should check the number of images before starting the for loop; the first image will always have an index value of zero.
Member 14766911 8-Oct-20 10:53am
   
I need two dialogs because I want to user to save a single page PDF that has been converted into an image using the "SaveFileDialog" and if the PDF is multi-page, I want the user to save those images into a folder using the "FolderBrowserDialog".
Richard MacCutchan 8-Oct-20 11:03am
   
You can still do that with a single dialog.
BillWoodruff 8-Oct-20 12:44pm
   
Where is the test that determines if there is one image, or multiple ?
Member 14766911 8-Oct-20 13:15pm
   
I'm using the If-statements after the for-loop to check for that

So move the test outside the loop. If there is one page, use the SaveFileDialog. It there are more than one, use a FolderBrowserDialog and then your loop without the check it does at the moment.
   
You are doing the same thing every time 'i == 0 ... 'i is the for-loop index: don't you really need to check for when the images.Length is #1 which is the case you show the filedialog ? And, if images.Length > #1, you save multiple images in the same folder ?

Right now you show the folderbrowserdialog images.Length - 1 times: that doesn't seem right.
   
So after moving the test outside the loop, everything works perfectly okay now!!

Thank you so much everyone!
   

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