PdfToTiffGcImaging.cs
  1. //
  2. // This code is part of Document Solutions for PDF demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using System.Collections.Generic;
  9. using GrapeCity.Documents.Pdf;
  10. using GrapeCity.Documents.Pdf.Annotations;
  11. using GrapeCity.Documents.Text;
  12. using GrapeCity.Documents.Imaging;
  13. using GrapeCity.Documents.Drawing;
  14.  
  15. namespace DsPdfWeb.Demos
  16. {
  17. // This example shows how to convert an arbitrary PDF to a multi-frame TIFF
  18. // in which each frame corresponds to a single PDF page, converting full color
  19. // original pages to grayscale images with an arbitrary target resolution
  20. // (in the sample code the resolution is set to 200 dpi).
  21. //
  22. // The resulting TIFF is similar to one produced by the PdfToGrayscaleTiff sample,
  23. // with two differences:
  24. // - Unlike PdfToGrayscaleTiff, this code does need a valid DsImaging license
  25. // in addition to a DsPdf license to work in production, as it directly uses
  26. // GcBitmapGraphics.
  27. // - Because it avoids the additional step of saving each page to PNG,
  28. // this example works faster than PdfToGrayscaleTiff.
  29. //
  30. // In order to seamlessly build this sample into the demo site framework,
  31. // the generated TIFF is then converted back into a PDF. In a real life scenario
  32. // you can just use the code that generates the TIFF directly.
  33. public class PdfToTiffGcImaging
  34. {
  35. public int CreatePDF(Stream stream)
  36. {
  37. // Arbitrary target DPI for the generated TIFF, adjust as needed:
  38. const int targetDPI = 200;
  39. int ptToDpi(float pts)
  40. {
  41. return (int)Math.Round((pts * targetDPI) / 72f);
  42. }
  43.  
  44. var inputDoc = new GcPdfDocument();
  45. using var fs = File.OpenRead(Path.Combine("Resources", "PDFs", "SlidePages.pdf"));
  46. inputDoc.Load(fs);
  47.  
  48. // NOTE: to produce a TIFF disk file, you would create a GcTiffWriter
  49. // with the file path as the parameter. But due to the demo browser requirements,
  50. // we create a GcTiffWriter on a memory stream instead:
  51. // using var tiffWriter = new GcTiffWriter("result.tiff");
  52. using var msTiff = new MemoryStream();
  53. using (var tiffWriter = new GcTiffWriter(msTiff))
  54. {
  55. foreach (var p in inputDoc.Pages)
  56. {
  57. // Create GcBitmap with same size as the PDF page, adjusted for the target DPI:
  58. using var bmp = new GcBitmap(ptToDpi(p.Size.Width), ptToDpi(p.Size.Height), true, ptToDpi(72), ptToDpi(72));
  59. // Draw the PDF page on the bitmap and apply in-place grayscale effect:
  60. using var g = bmp.CreateGraphics(Color.White);
  61. p.Draw(g, new RectangleF(0, 0, bmp.Width, bmp.Height));
  62. bmp.ApplyEffect(GrayscaleEffect.Get());
  63. // Convert full color bitmap to grayscale bitmap and append it as a frame to the TIFF:
  64. using var gbmp = bmp.ToGrayscaleBitmap();
  65. tiffWriter.AppendFrame(gbmp);
  66. }
  67. }
  68. // At this point tiffWriter has the newly created TIFF. If it was created on
  69. // a disk file, that file would contain the TIFF when tiffWriter is disposed.
  70. //
  71. // To use this sample in the demo browser, we have to convert the TIFF
  72. // back to a PDF that is returned to the controller.
  73. msTiff.Position = 0;
  74. var doc = new GcPdfDocument();
  75. using var tiffReader = new GcTiffReader(msTiff);
  76. List<IDisposable> disposables = new List<IDisposable>();
  77. foreach (var tp in tiffReader.Frames)
  78. {
  79. var img = tp.ToImage(ImageBinding.InMemoryData);
  80. var p = doc.Pages.Add(new SizeF(img.Width / img.HorizontalResolution * 72, img.Height / img.VerticalResolution * 72));
  81. p.Graphics.DrawImage(img, p.Bounds, null, ImageAlign.Default);
  82. disposables.Add(img);
  83. }
  84. // Save the PDF:
  85. doc.Save(stream);
  86. // We can only dispose images after saving the PDF:
  87. disposables.ForEach(d_ => d_.Dispose());
  88. return doc.Pages.Count;
  89. }
  90. }
  91. }
  92.