using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media.Imaging;
using Acrobat;
using Microsoft.VisualBasic;
using Point = System.Drawing.Point;
namespace WpfApp1
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public delegate void CompleteDelegate(string completeMsg);
/// <summary>
/// 转换异常委托
/// </summary>
/// <param name="errMessage"></param>
public delegate void ConvertExceptionDelegate(Exception errMessage);
/// <summary>
/// 进度提示委托
/// </summary>
/// <param name="progressValue"></param>
/// <param name="progressMessage"></param>
public delegate void ProcessChangedDelegate(int progressValue, string progressMessage);
public MainWindow()
{
InitializeComponent();
const string filename = @"E:\文档\人月神话.pdf";
var tr = new Thread(() =>
{
//间接法,清晰度低,但是能有进度
// Pdf2Png(filename, (a, b) => { Dispatcher.Invoke(() => { Title = b; }); }, a => { Dispatcher.Invoke(() => { MessageBox.Show(a.Message); }); }, (a) => { Dispatcher.Invoke(() => { MessageBox.Show(a); }); });
//直接法,但是没有进度
cvt(filename, "", (a, b) => { Dispatcher.Invoke(() => { Title = b; }); },
a => { Dispatcher.Invoke(() => { MessageBox.Show(a.Message); }); },
a => { Dispatcher.Invoke(() => { MessageBox.Show(a); }); });
});
tr.IsBackground = true;
tr.SetApartmentState(ApartmentState.STA);
tr.Start();
}
private static void cvt(string szPdfPathConst, string imageOutputPath, ProcessChangedDelegate processChange,
ConvertExceptionDelegate convertException, CompleteDelegate completeAction)
{
CAcroApp mApp = new AcroApp();
mApp.Hide();
var g_AVDoc = new AcroAVDoc();
//open the PDF
if (g_AVDoc.Open(szPdfPathConst, ""))
{
var currentIndex = 0;
var formDataExists = true;
object newDoc = null;
var dt = DateTime.Now;
if (g_AVDoc.IsValid())
{
var pdDoc = (CAcroPDDoc) g_AVDoc.GetPDDoc();
var iNum = pdDoc.GetNumPages();
object jsObj = pdDoc.GetJSObject();
if (formDataExists)
{
var T = jsObj.GetType();
object[] extractPagesParam = {currentIndex, iNum - 1};
newDoc = T.InvokeMember(
"extractPages",
BindingFlags.InvokeMethod |
BindingFlags.Public |
BindingFlags.Instance,
null, jsObj, extractPagesParam);
var newFileName = AppDomain.CurrentDomain.BaseDirectory + "data\\" + currentIndex + ".png";
Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "data");
object[] saveAsParam = {newFileName, "com.adobe.acrobat.png"};
T.InvokeMember(
"saveAs",
BindingFlags.InvokeMethod |
BindingFlags.Public |
BindingFlags.Instance,
null, newDoc, saveAsParam);
formDataExists = currentIndex != iNum - 1;
object[] closeDocParam = {true};
T.InvokeMember(
"closeDoc",
BindingFlags.InvokeMethod |
BindingFlags.Public |
BindingFlags.Instance,
null, newDoc, closeDocParam);
currentIndex++;
}
}
completeAction?.Invoke((DateTime.Now - dt).TotalSeconds.ToString());
}
mApp.CloseAllDocs();
mApp.Exit();
}
private static void Pdf2Png(string pdfInputPath, ProcessChangedDelegate processChange,
ConvertExceptionDelegate convertException, CompleteDelegate completeAction)
{
Thread tr = null;
tr = new Thread(() =>
{
try
{
CAcroPDDoc pdfDoc = null;
CAcroPDPage pdfPage = null;
CAcroRect pdfRect = null;
CAcroPoint pdfPoint = null;
if (processChange != null) processChange(0, "加载pdf文件中...");
pdfDoc = (CAcroPDDoc) Interaction.CreateObject("AcroExch.PDDoc", "");
if (!pdfDoc.Open(pdfInputPath))
{
if (convertException != null)
convertException(new Exception("未找到文件或文件不是pdf格式:" + pdfInputPath));
if (processChange != null) processChange(0, "未找到文件或文件不是pdf格式:" + pdfInputPath);
return;
}
for (var i = 1; i <= pdfDoc.GetNumPages(); i++)
{
pdfPage = (CAcroPDPage) pdfDoc.AcquirePage(i - 1);
pdfPoint = (CAcroPoint) pdfPage.GetSize();
pdfRect = (CAcroRect) Interaction.CreateObject("AcroExch.Rect", "");
var imgWidth = pdfPoint.x;
var imgHeight = (int) pdfPoint.y;
pdfRect.Left = 0;
pdfRect.right = imgWidth;
pdfRect.Top = 0;
pdfRect.bottom = (short) imgHeight;
if (processChange != null)
processChange(i * 100 / pdfDoc.GetNumPages(),
"转换进度" + i * 100 / pdfDoc.GetNumPages() + "%");
pdfPage.CopyToClipboard(pdfRect, 0, 0, 100);
var clipboardData = Clipboard.GetDataObject();
if (clipboardData.GetDataPresent(DataFormats.Bitmap))
{
var pdfBitmap = GetBitmap((InteropBitmap) clipboardData.GetData(DataFormats.Bitmap));
var newFileName = AppDomain.CurrentDomain.BaseDirectory + "data\\" + i + ".png";
if (!File.Exists(newFileName) && pdfBitmap != null)
{
pdfBitmap.Save(newFileName, ImageFormat.Png);
pdfBitmap.Dispose();
}
}
}
if (processChange != null) processChange(100, "转换完成");
Marshal.ReleaseComObject(pdfPage);
Marshal.ReleaseComObject(pdfRect);
Marshal.ReleaseComObject(pdfDoc);
Marshal.ReleaseComObject(pdfPoint);
if (completeAction != null) completeAction("完成:" + pdfInputPath + DateTime.Now);
}
catch (Exception ex)
{
if (convertException != null) convertException(ex);
if (processChange != null) processChange(0, "转换发生异常。源文件" + pdfInputPath + "(" + ex.Message + ")");
}
});
tr.SetApartmentState(ApartmentState.STA);
tr.Start();
}
private static Bitmap GetBitmap(BitmapSource source)
{
var bmp = new Bitmap(
source.PixelWidth,
source.PixelHeight,
PixelFormat.Format32bppRgb);
var data = bmp.LockBits(
new Rectangle(Point.Empty, bmp.Size),
ImageLockMode.WriteOnly,
PixelFormat.Format32bppRgb);
source.CopyPixels(
Int32Rect.Empty,
data.Scan0,
data.Height * data.Stride,
data.Stride);
bmp.UnlockBits(data);
return bmp;
}
}
}