public static void PreViewPage(object sender, PrintPageEventArgs e, PdfiumViewer.PdfDocument pdfDocument, ref int?pages) { PageSettings settings = e.PageSettings; var paperHeight = settings.PaperSize.Height; var paperWidth = settings.PaperSize.Width; int margin = 10; var imgHeight = 0; var imgWidth = 0; //int? pages = 0; using (var image = pdfDocument.Render(pages.Value, ConfigDefine.Pdf2JpgResolution, ConfigDefine.Pdf2JpgResolution, PdfiumViewer.PdfRenderFlags.CorrectFromDpi)) { if ((double)image.Width / (double)image.Height > (double)paperWidth / (double)paperHeight) // 图片等比例放大,宽度超出了,先确定宽度 { imgWidth = (int)(paperWidth - margin * 2); imgHeight = (int)((double)image.Height * (double)imgWidth / (double)image.Width - margin * 2); } else { imgHeight = (int)(paperHeight - margin * 2); imgWidth = (int)((double)image.Width / (double)image.Height * imgHeight - margin * 2); } e.Graphics.DrawImage(image, margin, margin, imgWidth, imgHeight); } if (pages < pdfDocument.PageCount - 1) { e.HasMorePages = true; //HaeMorePages属性为True时,PrintPage的回调函数就会被再次调用,打印一个页面。 pages++; } else { //预览界面点击打印需要把索引重新初始化 pages = 0; } }
public static void PreViewPage(object sender, PrintPageEventArgs e, PdfiumViewer.PdfDocument pdfDocument, string direction, ref int?pages) { PageSettings settings = e.PageSettings; var paperHeight = settings.PaperSize.Height; var paperWidth = settings.PaperSize.Width; int margin = 10; var imgHeight = 0; var imgWidth = 0; using (var image = pdfDocument.Render(pages.Value, ConfigDefine.Pdf2JpgResolution, ConfigDefine.Pdf2JpgResolution, PdfiumViewer.PdfRenderFlags.CorrectFromDpi)) { ImageScaling(image, paperHeight, paperWidth, margin, direction, ref imgHeight, ref imgWidth); if (direction == "2") //2:横向打印(html转pdf的有问题,html转pdf不考虑宽度,转出来标准的pdf页,分页也已经完成,在这部分就处理不了) { e.Graphics.TranslateTransform(0, paperHeight); //旋转原点 e.Graphics.RotateTransform(-90.0F); //旋转角度 } e.Graphics.DrawImage(image, margin, margin, imgWidth, imgHeight); } if (pages < pdfDocument.PageCount - 1) { e.HasMorePages = true; //HaeMorePages属性为True时,PrintPage的回调函数就会被再次调用,打印一个页面。 pages++; } else { //预览界面点击打印需要把索引重新初始化 pages = 0; } }
private BitmapImage GetPdfPageImage(PdfiumViewer.PdfDocument pDoc, int pageNum) { BitmapImage image = new BitmapImage(); Bitmap img; MemoryStream stream = new MemoryStream(); img = (Bitmap)pDoc.Render(pageNum, 96.0f, 96.0f, true); img.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp); image.BeginInit(); image.CacheOption = BitmapCacheOption.OnLoad; image.StreamSource = stream; image.EndInit(); image.Freeze(); return(image); }
public Sprite GetPage(PdfiumViewer.PdfDocument doc, int pageNum) { if (pageNum >= doc.PageCount) { return(null); } var tempPath = Application.persistentDataPath + _targetFolder + "_tempImage_.png"; if (FileBrowserHelpers.FileExists(tempPath)) { FileBrowserHelpers.DeleteFile(tempPath); } var image = doc.Render(pageNum, 72, 72, false); image.Save(tempPath, System.Drawing.Imaging.ImageFormat.Png); var sprite = LoadSprite(tempPath); return(sprite); }
public static void Pdf2Jpg(PdfiumViewer.PdfDocument pdfDocument, string targetPath, int?pages, string folderName) { if (string.IsNullOrEmpty(targetPath)) { throw new ArgumentNullException("targetPath"); } var dir = Path.GetDirectoryName(targetPath); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } if (pdfDocument.PageCount == 1) { pages = 1; } var imageDir = Path.Combine(dir, string.IsNullOrEmpty(folderName) ? Guid.NewGuid().ToString() : folderName); Directory.CreateDirectory(imageDir); if (pages != null) { pages--;//PdfiumViewer.PdfDocument的页数是从0开始的 using (var image = pdfDocument.Render(pages.Value, ConfigDefine.Pdf2JpgResolution, ConfigDefine.Pdf2JpgResolution, PdfiumViewer.PdfRenderFlags.CorrectFromDpi)) { if (ConfigDefine.Pdf2JpgScaleRatio != 0) { Stream imageStream = new MemoryStream(); image.Save(imageStream, ImageFormat.Jpeg); int imgWidth = Convert.ToInt32(image.Width * ConfigDefine.Pdf2JpgScaleRatio); int imgHeight = Convert.ToInt32(image.Height * ConfigDefine.Pdf2JpgScaleRatio); using (var imgScaleStream = ImageScale2Stream(imageStream, imgWidth, imgHeight, "")) { using (var imgScale = System.Drawing.Image.FromStream(imgScaleStream)) { imgScale.Save(Path.Combine(imageDir, folderName + ".jpg")); //imgScale.Save(targetPath); } } } else { image.Save(Path.Combine(imageDir, folderName + ".jpg")); //image.Save(targetPath); } } } else { for (pages = 0; pages < pdfDocument.PageCount; pages++) { using (var image = pdfDocument.Render(pages.Value, ConfigDefine.Pdf2JpgResolution, ConfigDefine.Pdf2JpgResolution, PdfiumViewer.PdfRenderFlags.CorrectFromDpi)) { if (ConfigDefine.Pdf2JpgScaleRatio != 0) { Stream imageStream = new MemoryStream(); image.Save(imageStream, ImageFormat.Jpeg); int imgWidth = Convert.ToInt32(image.Width * ConfigDefine.Pdf2JpgScaleRatio); int imgHeight = Convert.ToInt32(image.Height * ConfigDefine.Pdf2JpgScaleRatio); using (var imgScaleStream = ImageScale2Stream(imageStream, imgWidth, imgHeight, "")) { using (var imgScale = System.Drawing.Image.FromStream(imgScaleStream)) { imgScale.Save(Path.Combine(imageDir, pages + ".jpg")); } } } else { image.Save(Path.Combine(imageDir, pages + ".jpg")); } } } //图片拼接 //MergerImage(imageDir, targetPath); /* * try * { * //删除文件夹 * Directory.Delete(imageDir, true); * } * catch (Exception ex) * { * //Send(string.Format("删除临时目录出错:{0};其他错误信息:{1}", imageDir, ex)); * throw new ArgumentNullException(string.Format("删除临时目录出错:{0};其他错误信息:{1}", imageDir, ex)); * } * */ } }