PDFToImage.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKit.PDFPage;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. namespace PDFToImageTest
  13. {
  14. internal class PDFToImage
  15. {
  16. private static string parentPath =
  17. Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory())));
  18. private static string outputPath = parentPath + @"\Output\CS";
  19. static void Main(string[] args)
  20. {
  21. Console.WriteLine("Running PDFToImage test sample…" + Environment.NewLine);
  22. SDKLicenseHelper.LicenseVerify();
  23. CPDFDocument document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");
  24. if (!Directory.Exists(outputPath))
  25. {
  26. Directory.CreateDirectory(outputPath);
  27. }
  28. PDFPageToImage(document);
  29. PDFToImage2(document);
  30. Console.WriteLine("--------------------");
  31. Console.WriteLine("Done!");
  32. Console.WriteLine("--------------------");
  33. Console.ReadLine();
  34. }
  35. private static void PDFToImage2(CPDFDocument document)
  36. {
  37. document.PdfToImage("1", outputPath);
  38. }
  39. static private void SaveWriteableBitmapAsPng(WriteableBitmap writeableBitmap, string fileName)
  40. {
  41. using (FileStream stream = new FileStream(fileName, FileMode.Create))
  42. {
  43. PngBitmapEncoder encoder = new PngBitmapEncoder();
  44. encoder.Frames.Add(BitmapFrame.Create(writeableBitmap));
  45. encoder.Save(stream);
  46. }
  47. }
  48. /// <summary>
  49. /// Convert PDF to image
  50. /// </summary>
  51. static private bool PDFPageToImage(CPDFDocument document)
  52. {
  53. for (int i = 0; i < document.PageCount; i++)
  54. {
  55. CPDFPage pdfPage = document.PageAtIndex(i, true);
  56. Size pageSize = document.GetPageSize(0);
  57. Rect pageRect = new Rect(0, 0, (int)(pageSize.Width / 72.0 * 96), (int)(pageSize.Height / 72.0 * 96));
  58. byte[] bmpData = new byte[(int)(pageRect.Width * pageRect.Height * (96 / 72.0) * (96 / 72.0) * 4)];
  59. pdfPage.RenderPageBitmapWithMatrix((float)(96 / 72.0), pageRect, 0xFFFFFFFF, bmpData, 0, true);
  60. WriteableBitmap writeableBitmap = new WriteableBitmap((int)pageRect.Width, (int)pageRect.Height, 96, 96, PixelFormats.Bgra32, null);
  61. writeableBitmap.WritePixels(new Int32Rect(0, 0, (int)pageRect.Width, (int)pageRect.Height), bmpData, writeableBitmap.BackBufferStride, 0);
  62. var path = outputPath + @"\PDFToImageTest" + i + ".png";
  63. SaveWriteableBitmapAsPng(writeableBitmap, path);
  64. Console.WriteLine("Png image saved in {0}", path);
  65. }
  66. return false;
  67. }
  68. }
  69. }