拆分合并PDF 图片互转——c#编程实现

该软件实现pdf的拆分合并、与jpg图片互转功能。效果如下:

软件界面如下:

附部分代码如下:

        #region PDF转JPG功能
        private void btnSelectPdfFiles_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.Filter = "PDF文件 (*.pdf)|*.pdf|所有文件 (*.*)|*.*";
                openFileDialog.Multiselect = true;
                openFileDialog.Title = "选择PDF文件";

                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    selectedPdfFiles.AddRange(openFileDialog.FileNames);
                    UpdatePdfFileList();
                }
            }
        }

        private void UpdatePdfFileList()
        {
            lstPdfFiles.Items.Clear();
            foreach (string file in selectedPdfFiles)
            {
                lstPdfFiles.Items.Add(Path.GetFileName(file));
            }
            UpdatePdfStatus($"已选择 {selectedPdfFiles.Count} 个文件");
        }

        private void UpdatePdfStatus(string message)
        {
            lblPdfStatus.Text = message;
        }

        private void btnClearPdf_Click(object sender, EventArgs e)
        {
            selectedPdfFiles.Clear();
            UpdatePdfFileList();
        }

        private void btnConvertPdfToJpg_Click(object sender, EventArgs e)
        {
            if (selectedPdfFiles.Count == 0)
            {
                MessageBox.Show("请先选择PDF文件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            using (FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog())
            {
                folderBrowserDialog.Description = "选择保存JPG文件的文件夹";

                if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        UpdatePdfStatus("正在转换...");
                        Cursor = Cursors.WaitCursor;

                        int dpi = (int)nudDpi.Value;
                        ConvertPdfToJpg(selectedPdfFiles, folderBrowserDialog.SelectedPath, dpi);

                        UpdatePdfStatus("转换完成!");
                        MessageBox.Show($"JPG文件已保存至:{folderBrowserDialog.SelectedPath}", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    catch (Exception ex)
                    {
                        UpdatePdfStatus("转换失败!");
                        MessageBox.Show($"转换过程中发生错误:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    finally
                    {
                        Cursor = Cursors.Default;
                    }
                }
            }
        }

        private void ConvertPdfToJpg(List<string> pdfFiles, string outputFolder, int dpi)
        {
            foreach (string pdfFile in pdfFiles)
            {
                try
                {
                    string fileNameWithoutExt = Path.GetFileNameWithoutExtension(pdfFile);
                    string fileOutputFolder = Path.Combine(outputFolder, fileNameWithoutExt);
                    Directory.CreateDirectory(fileOutputFolder);

                    using (PdfiumViewerPdf document = PdfiumViewerPdf.Load(pdfFile))
                    {
                        int pageCount = document.PageCount;

                        for (int i = 0; i < pageCount; i++)
                        {
                            string outputPath = Path.Combine(fileOutputFolder, $"{fileNameWithoutExt}_第{i + 1}页.jpg");

                            using (Bitmap image = (Bitmap)document.Render(i, dpi, dpi, false))
                            {
                                image.Save(outputPath, System.Drawing.Imaging.ImageFormat.Jpeg);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"处理文件 {Path.GetFileName(pdfFile)} 时出错:{ex.Message}", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            }
        }
        #endregion

        #region 合并PDF功能
        private void btnSelectPdfToMergeFiles_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.Filter = "PDF文件 (*.pdf)|*.pdf|所有文件 (*.*)|*.*";
                openFileDialog.Multiselect = true;
                openFileDialog.Title = "选择要合并的PDF文件";

                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    selectedPdfToMergeFiles.AddRange(openFileDialog.FileNames);
                    UpdatePdfToMergeFileList();
                }
            }
        }

软件获取方式:↓↓↓

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

山水CAD插件定制

你的鼓励是我创作最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值