using System;
using System.Drawing;
using System.Windows.Forms;

namespace Pdf2Png
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void txtPdfPath_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); //桌面
            openFileDialog.Filter = "pdf文件|*.pdf";
            openFileDialog.RestoreDirectory = true;
            openFileDialog.FilterIndex = 0;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                this.txtPdfPath.Text = openFileDialog.FileName;
                this.txtPngPath.Text = openFileDialog.FileName.Replace(".pdf",".png");
            }
        }

        private void txtPngPath_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            //设置文件类型 
            sfd.Filter = "png文件（*.png）|*.png";
            //设置默认文件类型显示顺序 
            sfd.FilterIndex = 1;
            //保存对话框是否记忆上次打开的目录 
            sfd.RestoreDirectory = true;
            //点了保存按钮进入 
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                this.txtPngPath.Text = sfd.FileName;
            }
        }

        private void btnExec_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty(this.txtPdfPath.Text))
            {
                MessageBox.Show("没有指定 pdf 文件");
                return;
            }
            if (string.IsNullOrEmpty(this.txtPngPath.Text))
            {
                MessageBox.Show("没有指定 png 文件");
                return;
            }
            try
            {
                Bitmap[] arr = ImgHelper.GetBitmapArrFromPdf(this.txtPdfPath.Text);
                ImgHelper.MergerImg(arr, this.txtPngPath.Text);
                MessageBox.Show("转换成功！");
            }
            catch (Exception ex)
            {
                MessageBox.Show("转换失败："+ex.Message);
            }
        }
    }//end of class
}//end of namespace
