110,549
社区成员
发帖
与我相关
我的任务
分享
Acrobat.CAcroPDPage pdfPage = null;
Acrobat.CAcroPoint pdfPoint = null;
Acrobat.CAcroRect pdfRect = null;
Acrobat.CAcroPDDoc doc = (Acrobat.CAcroPDDoc)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.PDDoc", "");
Bitmap bitmap = null;
for (curPDFIdx = 0; curPDFIdx < PD.PlateList.Count; ++curPDFIdx)
{
//取出当前页
pdfPage = (Acrobat.CAcroPDPage)doc.AcquirePage(curPDFIdx - 1);
//得到当前页的大小
pdfPoint = (Acrobat.CAcroPoint)pdfPage.GetSize();
//生成一个页的裁剪区矩形对象
pdfRect = (Acrobat.CAcroRect)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.Rect", "");
int pdfWidth = pdfPoint.x * pdfDPI / 72;//默认PDF的点是72DPI得到的。
int pdfHeight = pdfPoint.y * pdfDPI / 72;
//设置裁剪矩形的大小为当前页的大小
pdfRect.Left = 0;
pdfRect.right = (short)(pdfWidth);
pdfRect.Top = 0;
pdfRect.bottom = (short)(pdfHeight);
//将当前页的裁剪区的内容编成图片后复制到剪贴板中//耗时30多毫秒
pdfPage.CopyToClipboard(pdfRect, 0, 0, (short)(100 * pdfDPI / 72)); //100指100%
try
{
System.Windows.IDataObject clipboardData = System.Windows.Clipboard.GetDataObject();
//检查剪贴板中的对象是否是图片,如果是图片则将其保存为指定格式的图片文件
if (clipboardData.GetDataPresent(System.Windows.DataFormats.Bitmap))
{//InteropBitmap转换成Drawing.bitmap
int stride = ((pdfWidth * 32 + 31) & ~31) >> 3;
byte[] byte_arr = new byte[(int)(pdfHeight * stride)];
InteropBitmap interopBitmap = (InteropBitmap)clipboardData.GetData(System.Windows.DataFormats.Bitmap);
interopBitmap.CopyPixels(byte_arr, stride, 0);
System.Drawing.Imaging.BitmapData bmpData;
//The Width and Height should be static don't bother depending on the InteropBitmap for them
bitmap = new System.Drawing.Bitmap(pdfWidth, pdfHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
bmpData = bitmap.LockBits(new System.Drawing.Rectangle(new System.Drawing.Point(), bitmap.Size), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
System.Runtime.InteropServices.Marshal.Copy(byte_arr, 0, bmpData.Scan0, byte_arr.Length);
bitmap.UnlockBits(bmpData);
bitmap.Save(@"D:\" + curPDFIdx.ToString() + ".bmp");//10毫秒
bitmap.Dispose();
}
}
catch (Exception ex)
{
ShowError(ex.Message, "");
return;
}
Marshal.ReleaseComObject(pdfPage);
Marshal.ReleaseComObject(pdfRect);
Marshal.ReleaseComObject(pdfPoint);
}
Marshal.ReleaseComObject(doc);