该软件实现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();
}
}
}
软件获取方式:↓↓↓