PDFToImageTest.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. ComPDFKit.Import.CSize pageSize = document.GetPageSize(0);
  57. ComPDFKit.Import.CRect pageRect = new ComPDFKit.Import.CRect(0, 0, (int)(pageSize.width / 72.0 * 96), (int)(pageSize.height / 72.0 * 96));
  58. int size = (int)(pageRect.width() * pageRect.height() * (96 / 72.0) * (96 / 72.0) * 4);
  59. byte[] bmpData = new byte[size];
  60. // TODO: Need to repair CRect.Height
  61. pdfPage.RenderPageBitmapWithMatrix((float)(96 / 72.0), pageRect, 0xFFFFFFFF, bmpData, 0, true);
  62. WriteableBitmap writeableBitmap = new WriteableBitmap((int)pageRect.width(), (int)pageRect.height(), 96, 96, PixelFormats.Bgra32, null);
  63. writeableBitmap.WritePixels(new Int32Rect(0, 0, (int)pageRect.width(), (int)pageRect.height()), bmpData, writeableBitmap.BackBufferStride, 0);
  64. var path = outputPath + @"\PDFToImageTest" + i + ".png";
  65. SaveWriteableBitmapAsPng(writeableBitmap, path);
  66. Console.WriteLine("Png image saved in {0}", path);
  67. }
  68. return false;
  69. }
  70. }
  71. }