/// <summary> /// 将PDF文档转换为图片的方法 /// </summary> /// <param name="pdfInputPath">PDF文件路径</param> /// <param name="imageOutputPath">图片输出路径</param> /// <param name="imageName">生成图片的名字</param> /// <param name="startPageNum">从PDF文档的第几页开始转换</param> /// <param name="endPageNum">从PDF文档的第几页开始停止转换</param> /// <param name="imageFormat">设置所需图片格式</param> /// <param name="definition">设置图片的清晰度,数字越大越清晰</param> public static void ConvertPDF2Image(string pdfInputPath, string imageOutputPath, string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, Definition definition) { FileStream fs = new FileStream(pdfInputPath, FileMode.Open); TallComponents.PDF.Rasterizer.Document doc = new TallComponents.PDF.Rasterizer.Document(fs); if (!Directory.Exists(imageOutputPath)) { Directory.CreateDirectory(imageOutputPath); } // validate pageNum if (startPageNum <= 0) { startPageNum = 1; } if (endPageNum > doc.Pages.Count) { endPageNum = doc.Pages.Count; } if (startPageNum > endPageNum) { int tempPageNum = startPageNum; startPageNum = endPageNum; endPageNum = startPageNum; } // start to convert each page for (int i = startPageNum; i <= endPageNum; i++) { using (FileStream fs1 = File.Create(imageOutputPath + @"~temp" + i + ".tmp")) { TallComponents.PDF.Rasterizer.ConvertToTiffOptions option = new TallComponents.PDF.Rasterizer.ConvertToTiffOptions(); doc.Pages[i].ConvertToTiff(fs1, option); System.Drawing.Image img = System.Drawing.Image.FromStream(fs1); img.Save(imageOutputPath + imageName + i.ToString() + "." + imageFormat.ToString(), imageFormat); } File.Delete(imageOutputPath + @"~temp" + i + ".tmp"); } fs.Dispose(); }
void drawPage(int index) { using (FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read)) { pdf.Rasterizer.Document document = new pdf.Rasterizer.Document(file); pdf.Rasterizer.Page page = document.Pages[index]; pdf.Rasterizer.Configuration.RenderSettings renderSettings = new pdf.Rasterizer.Configuration.RenderSettings(); pdf.Rasterizer.Diagnostics.Summary summary = new pdf.Rasterizer.Diagnostics.Summary(); pdf.Rasterizer.ConvertToWpfOptions convertOptions = new pdf.Rasterizer.ConvertToWpfOptions(); FixedDocument fixedDocument = document.ConvertToWpf(renderSettings, convertOptions, index, index, summary); documentViewer.Document = fixedDocument; FixedPage fixedPage = fixedDocument.Pages[0].Child; // add a canvas and change its coordinate system to that of PDF overlay = new Canvas(); overlay.Width = fixedPage.Width; overlay.Height = fixedPage.Height; TransformGroup group = new TransformGroup(); group.Children.Insert(0, new TranslateTransform(0, fixedPage.Height)); group.Children.Insert(0, new ScaleTransform(fixedPage.Width / page.Width, -fixedPage.Height / page.Height)); switch (page.Orientation) { case pdf.Rasterizer.Orientation.Rotate90: group.Children.Insert(0, new RotateTransform(90)); group.Children.Insert(0, new TranslateTransform(0, -page.MediaBox.Height)); break; case pdf.Rasterizer.Orientation.Rotate180: group.Children.Insert(0, new RotateTransform(180)); group.Children.Insert(0, new TranslateTransform(-page.MediaBox.Width, -page.MediaBox.Height)); break; case pdf.Rasterizer.Orientation.Rotate270: group.Children.Insert(0, new RotateTransform(270)); group.Children.Insert(0, new TranslateTransform(-page.MediaBox.Width, 0)); break; default: break; } overlay.RenderTransform = group; fixedPage.Children.Add(overlay); // now draw on the canvas in PDF coordinate space System.Windows.Shapes.Line line1 = new System.Windows.Shapes.Line(); line1.Stroke = new SolidColorBrush(Colors.Red); line1.StrokeThickness = 3; line1.X1 = line1.Y1 = 0; line1.X2 = page.MediaBox.Width / 2; line1.Y2 = page.MediaBox.Height / 2; overlay.Children.Add(line1); System.Windows.Shapes.Line line2 = new System.Windows.Shapes.Line(); line2.Stroke = new SolidColorBrush(Colors.Green); line2.StrokeThickness = 3; line2.X1 = 0; line2.Y1 = page.MediaBox.Height; line2.X2 = page.MediaBox.Width; line2.Y2 = 0; overlay.Children.Add(line2); System.Windows.Shapes.Ellipse ellipse = new System.Windows.Shapes.Ellipse(); ellipse.Width = ellipse.Height = page.MediaBox.Height; ellipse.Stroke = new SolidColorBrush(Colors.Blue); ellipse.StrokeThickness = 3; overlay.Children.Add(ellipse); } }