/// <summary> /// 核心代码是从网上找的,但是那些代码是运行于winform环境下的剪切板,要在WPF下,需要改进,已经改进完毕 /// </summary> /// <param name="pageIndex"></param> /// <param name="action"></param> private void UsingImage(int pageIndex, Action <Bitmap> action) { var page = (CAcroPDPage)_document.AcquirePage(pageIndex); try { var point = (CAcroPoint)page.GetSize(); //WPF下,需要转换逻辑像素到物理像素 //double resolution = SystemScreen.DPI.ScalingY; //设置图片的分辨率,数字越大越清晰,默认值为1 double resolution = 1.5; int imgWidth = (int)((double)point.x * resolution); int imgHeight = (int)((double)point.y * resolution); _rect.Left = 0; _rect.right = (short)imgWidth; _rect.Top = 0; _rect.bottom = (short)imgHeight; using (MemoryStream ms = GetStream(page, resolution)) { using (var image = new Bitmap(ms)) { action(image); } } } catch (Exception ex) { throw ex; } finally { if (page != null) { Marshal.ReleaseComObject(page); } } }
private Bitmap pdfPageToBitmap(int pageNumber) { var pdfPage = (CAcroPDPage)_pdfDoc.AcquirePage(pageNumber); if (pdfPage == null) { throw new InvalidOperationException(BadFileErrorMessage); } var pdfPoint = (CAcroPoint)pdfPage.GetSize(); _pdfRect.Left = 0; _pdfRect.right = pdfPoint.x; _pdfRect.Top = 0; _pdfRect.bottom = pdfPoint.y; pdfPage.CopyToClipboard(_pdfRect, 0, 0, 100); Bitmap pdfBitmap = null; var thread = new Thread(() => { var data = Clipboard.GetDataObject(); if (data != null && data.GetDataPresent(DataFormats.Bitmap)) { pdfBitmap = (Bitmap)data.GetData(DataFormats.Bitmap); } }); thread.SetApartmentState(ApartmentState.STA); thread.Start(); thread.Join(); Marshal.ReleaseComObject(pdfPage); return(pdfBitmap); }
//http://www.myexception.cn/c-sharp/1506415.html //http://www.aspose.com/docs/display/pdfnet/Generate+Thumbnail+Images+from+PDF+Documents public static void ConvertPDF2Image(string pdfInputPath, string imageOutputPath, string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, double resolution) { CAcroPDDoc pdfDoc = null; CAcroPDPage pdfPage = null; CAcroRect pdfRect = null; CAcroPoint pdfPoint = null; // Create the document (Can only create the AcroExch.PDDoc object using late-binding) // Note using VisualBasic helper functions, have to add reference to DLL pdfDoc = (CAcroPDDoc)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.PDDoc", ""); // validate parameter if (!pdfDoc.Open(pdfInputPath)) { throw new FileNotFoundException(); } if (!Directory.Exists(imageOutputPath)) { Directory.CreateDirectory(imageOutputPath); } if (startPageNum <= 0) { startPageNum = 1; } if (endPageNum > pdfDoc.GetNumPages() || endPageNum <= 0) { endPageNum = pdfDoc.GetNumPages(); } if (startPageNum > endPageNum) { int tempPageNum = startPageNum; startPageNum = endPageNum; endPageNum = startPageNum; } if (imageFormat == null) { imageFormat = ImageFormat.Jpeg; } if (resolution <= 0) { resolution = 1; } // start to convert each page for (int i = startPageNum; i <= endPageNum; i++) { pdfPage = (CAcroPDPage)pdfDoc.AcquirePage(i - 1); pdfPoint = (CAcroPoint)pdfPage.GetSize(); pdfRect = (CAcroRect)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.Rect", ""); int imgWidth = (int)((double)pdfPoint.x * resolution); int imgHeight = (int)((double)pdfPoint.y * resolution); pdfRect.Left = 0; pdfRect.right = (short)imgWidth; pdfRect.Top = 0; pdfRect.bottom = (short)imgHeight; // Render to clipboard, scaled by 100 percent (ie. original size) // Even though we want a smaller image, better for us to scale in .NET // than Acrobat as it would greek out small text pdfPage.CopyToClipboard(pdfRect, 0, 0, (short)(100 * resolution)); IDataObject clipboardData = Clipboard.GetDataObject(); if (clipboardData != null && clipboardData.GetDataPresent(DataFormats.Bitmap)) { string _dir = Path.Combine(imageOutputPath, imageName); string _path = _dir + "_" + i.ToString() + ".jpg"; if (File.Exists(_path)) { File.Delete(_path); } Bitmap pdfBitmap = (Bitmap)clipboardData.GetData(DataFormats.Bitmap); pdfBitmap.Save(_path, imageFormat); pdfBitmap.Dispose(); } } pdfDoc.Close(); Marshal.ReleaseComObject(pdfPage); Marshal.ReleaseComObject(pdfRect); Marshal.ReleaseComObject(pdfDoc); Marshal.ReleaseComObject(pdfPoint); }
public String getPageRangeBetweenStrings(String szPdfPathConst, String HeaderStr, String FooterStr, Boolean includeFirstPageInRange, Boolean includeLastPageInRange) { CAcroApp avApp; CAcroAVDoc avDoc; CAcroAVPageView avPage; avApp = new AcroAppClass(); avDoc = new AcroAVDocClass(); avDoc.Open(szPdfPathConst, ""); CAcroPDDoc pdDoc = (CAcroPDDoc)avDoc.GetPDDoc(); List <int> PageListHeader = new List <int>(); List <int> PageListFooter = new List <int>(); //AcroPDDoc pdDoc = getPDDoc(szPdfPathConst); int TotalNumberOfPages = pdDoc.GetNumPages(); AcroPDPage page; //set AVPage View object avPage = (CAcroAVPageView)avDoc.GetAVPageView(); avApp.Show(); for (int i = 0; i < TotalNumberOfPages; i++) { page = (AcroPDPage)pdDoc.AcquirePage(i); Boolean TextCheck = avDoc.FindText(HeaderStr, 1, 1, 0); if (TextCheck == true) { int PageNum = avPage.GetPageNum(); PageListHeader.Add(PageNum); } } List <int> PagesWithHeaderWords = DeDuplicateArray(PageListHeader); for (int i = 0; i < TotalNumberOfPages; i++) { page = (AcroPDPage)pdDoc.AcquirePage(i); Boolean TextCheck = avDoc.FindText(FooterStr, 1, 1, 0); if (TextCheck == true) { int PageNum = avPage.GetPageNum(); PageListFooter.Add(PageNum); } } List <int> PagesWithFooterWords = DeDuplicateArray(PageListFooter); int MinimumFooterRange = 0; int MinimumHeaderRange = 0; if (PagesWithFooterWords.Count == 0 || PagesWithHeaderWords.Count == 0) { return("No Range Found"); } MinimumFooterRange = PagesWithFooterWords.Min(); MinimumHeaderRange = PagesWithFooterWords.Min(); int HeaderFinalPageNumber = MinimumHeaderRange + 1; int FooterFinalPageNumber = MinimumFooterRange + 1; if (!includeFirstPageInRange) { HeaderFinalPageNumber++; } if (!includeLastPageInRange) { FooterFinalPageNumber--; } return(HeaderFinalPageNumber + "-" + FooterFinalPageNumber); }