|
|
using iText.Kernel.Pdf; |
|
|
using iText.Kernel.Pdf.Canvas.Parser; |
|
|
using iText.Kernel.Pdf.Canvas.Parser.Listener; |
|
|
using iText.Kernel.Pdf.Xobject; |
|
|
using OpenCvSharp; |
|
|
using System.Drawing; |
|
|
using System.Drawing.Imaging; |
|
|
using System.IO; |
|
|
using System.Text; |
|
|
using System.Threading.Tasks; |
|
|
using System.Windows; |
|
|
using System.Windows.Controls; |
|
|
using System.Windows.Data; |
|
|
using System.Windows.Documents; |
|
|
using System.Windows.Forms; |
|
|
using System.Windows.Input; |
|
|
using System.Windows.Media; |
|
|
using System.Windows.Media.Imaging; |
|
|
using System.Windows.Navigation; |
|
|
using System.Windows.Shapes; |
|
|
using Image = System.Drawing.Image; |
|
|
using Path = System.IO.Path; |
|
|
using Window = System.Windows.Window; |
|
|
|
|
|
|
|
|
|
|
|
namespace WpfEditor |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
public partial class ExportPDFImage : Window |
|
|
{ |
|
|
private string pdfFilePath = ""; |
|
|
private string outputFolderPath = ""; |
|
|
|
|
|
public ExportPDFImage() |
|
|
{ |
|
|
InitializeComponent(); |
|
|
} |
|
|
|
|
|
private void btnSelectPdf_Click(object sender, RoutedEventArgs e) |
|
|
{ |
|
|
OpenFileDialog openFileDialog = new OpenFileDialog() |
|
|
{ |
|
|
Filter = "PDF文件|*.pdf", |
|
|
Title = "选择PDF文件" |
|
|
}; |
|
|
|
|
|
if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) |
|
|
{ |
|
|
pdfFilePath = openFileDialog.FileName; |
|
|
txtPdfPath.Text = pdfFilePath; |
|
|
lblStatus.Content = "状态:已选择PDF文件"; |
|
|
} |
|
|
} |
|
|
|
|
|
private void btnSelectOutputFolder_Click(object sender, RoutedEventArgs e) |
|
|
{ |
|
|
FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog() |
|
|
{ |
|
|
Description = "选择图片输出文件夹" |
|
|
}; |
|
|
|
|
|
if (folderBrowserDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) |
|
|
{ |
|
|
outputFolderPath = folderBrowserDialog.SelectedPath; |
|
|
txtOutputPath.Text = outputFolderPath; |
|
|
lblStatus.Content = "状态:已选择输出文件夹"; |
|
|
} |
|
|
} |
|
|
|
|
|
private async void btnExtractImages_Click(object sender, RoutedEventArgs e) |
|
|
{ |
|
|
if (string.IsNullOrEmpty(pdfFilePath)) |
|
|
{ |
|
|
System.Windows.MessageBox.Show("请先选择PDF文件!", "提示", MessageBoxButton.OK, MessageBoxImage.Warning); |
|
|
return; |
|
|
} |
|
|
|
|
|
if (string.IsNullOrEmpty(outputFolderPath)) |
|
|
{ |
|
|
System.Windows.MessageBox.Show("请先选择输出文件夹!", "提示", MessageBoxButton.OK, MessageBoxImage.Warning); |
|
|
return; |
|
|
} |
|
|
|
|
|
btnExtractImages.IsEnabled = false; |
|
|
listBoxImages.Items.Clear(); |
|
|
progressBar.Value = 0; |
|
|
lblStatus.Content = "状态:正在提取图片..."; |
|
|
|
|
|
try |
|
|
{ |
|
|
await Task.Run(() => ExtractImagesFromPdf()); |
|
|
lblStatus.Content = "状态:图片提取完成!"; |
|
|
System.Windows.MessageBox.Show("图片提取完成!", "成功", MessageBoxButton.OK, MessageBoxImage.Information); |
|
|
} |
|
|
catch (Exception ex) |
|
|
{ |
|
|
lblStatus.Content = "状态:提取失败"; |
|
|
System.Windows.MessageBox.Show($"提取图片时发生错误:{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error); |
|
|
} |
|
|
finally |
|
|
{ |
|
|
btnExtractImages.IsEnabled = true; |
|
|
progressBar.Value = 0; |
|
|
} |
|
|
} |
|
|
|
|
|
private void ExtractImagesFromPdf() |
|
|
{ |
|
|
using (PdfReader reader = new PdfReader(pdfFilePath)) |
|
|
using (PdfDocument pdfDoc = new PdfDocument(reader)) |
|
|
{ |
|
|
int totalPages = pdfDoc.GetNumberOfPages(); |
|
|
int imageCount = 0; |
|
|
|
|
|
for (int pageNumber = 1; pageNumber <= totalPages; pageNumber++) |
|
|
{ |
|
|
Dispatcher.Invoke(() => |
|
|
{ |
|
|
progressBar.Value = (int)((double)pageNumber / totalPages * 100); |
|
|
lblStatus.Content = $"状态:正在处理第 {pageNumber}/{totalPages} 页..."; |
|
|
}); |
|
|
|
|
|
PdfPage page = pdfDoc.GetPage(pageNumber); |
|
|
PdfDictionary pageDict = page.GetPdfObject(); |
|
|
PdfDictionary resources = pageDict.GetAsDictionary(PdfName.Resources); |
|
|
|
|
|
if (resources != null) |
|
|
{ |
|
|
PdfDictionary xObjects = resources.GetAsDictionary(PdfName.XObject); |
|
|
if (xObjects != null) |
|
|
{ |
|
|
foreach (PdfName name in xObjects.KeySet()) |
|
|
{ |
|
|
PdfObject obj = xObjects.Get(name); |
|
|
if (obj.IsStream()) |
|
|
{ |
|
|
PdfStream stream = (PdfStream)obj; |
|
|
PdfObject subtype = stream.Get(PdfName.Subtype); |
|
|
|
|
|
if (subtype != null && subtype.Equals(PdfName.Image)) |
|
|
{ |
|
|
try |
|
|
{ |
|
|
imageCount++; |
|
|
string imageName = $"{pageNumber:D4}"; |
|
|
string imagePath = Path.Combine(outputFolderPath, imageName); |
|
|
|
|
|
ExtractImage(stream, imagePath); |
|
|
|
|
|
Dispatcher.Invoke(() => |
|
|
{ |
|
|
listBoxImages.Items.Add($"已提取:{imageName}"); |
|
|
}); |
|
|
} |
|
|
catch (Exception ex) |
|
|
{ |
|
|
Dispatcher.Invoke(() => |
|
|
{ |
|
|
listBoxImages.Items.Add($"提取失败:第{pageNumber}页图片 - {ex.Message}"); |
|
|
}); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
Dispatcher.Invoke(() => |
|
|
{ |
|
|
lblStatus.Content = $"状态:完成!共提取 {imageCount} 张图片"; |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
private void ExtractImage(PdfStream stream, string imagePath) |
|
|
{ |
|
|
byte[] bts = stream.GetBytes(); |
|
|
Mat img = Cv2.ImDecode(bts, ImreadModes.Color); |
|
|
Cv2.ImWrite(imagePath + ".jpg", img); |
|
|
return; |
|
|
|
|
|
PdfObject filter = stream.Get(PdfName.Filter); |
|
|
|
|
|
if (filter != null) |
|
|
{ |
|
|
if (filter.Equals(PdfName.DCTDecode)) |
|
|
{ |
|
|
File.WriteAllBytes(imagePath + ".jpg", stream.GetBytes()); |
|
|
} |
|
|
else if (filter.Equals(PdfName.JPXDecode)) |
|
|
{ |
|
|
File.WriteAllBytes(imagePath + ".jp2", stream.GetBytes()); |
|
|
} |
|
|
else |
|
|
{ |
|
|
|
|
|
} |
|
|
} |
|
|
else |
|
|
{ |
|
|
File.WriteAllBytes(imagePath + ".raw", stream.GetBytes()); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |