PdfToGrayscaleTiff.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. // Note that while the code in this example uses some DsImaging features,
  23. // it does not need a DsImaging license to work without issues.
  24. // See PdfToTiffDsImaging for an example that does the same conversion faster,
  25. // but does require a valid DsImaging license.
  26. //
  27. // In order to seamlessly build this sample into the demo site framework,
  28. // the generated TIFF is then converted back into a PDF. In a real life scenario
  29. // you can just use the code that generates the TIFF directly.
  30. public class PdfToGrayscaleTiff
  31. {
  32. public int CreatePDF(Stream stream)
  33. {
  34. // Arbitrary target DPI for the generated TIFF, adjust as needed:
  35. const int targetDPI = 200;
  36.  
  37. var inputDoc = new GcPdfDocument();
  38. using var fs = File.OpenRead(Path.Combine("Resources", "PDFs", "SlidePages.pdf"));
  39. inputDoc.Load(fs);
  40.  
  41. using var ms = new MemoryStream();
  42. using var bmp = new GcBitmap();
  43. // NOTE: to produce a TIFF disk file, you would create a GcTiffWriter
  44. // with the file path as the parameter. But due to the demo browser requirements,
  45. // we create a GcTiffWriter on a memory stream instead:
  46. // using var tiffWriter = new GcTiffWriter("result.tiff");
  47. using var msTiff = new MemoryStream();
  48. using (var tiffWriter = new GcTiffWriter(msTiff))
  49. {
  50. foreach (var p in inputDoc.Pages)
  51. {
  52. // Save each PDF page as PNG with target resolution:
  53. ms.Position = 0;
  54. p.SaveAsPng(ms, new SaveAsImageOptions { Resolution = targetDPI });
  55. ms.Position = 0;
  56. bmp.Load(ms);
  57. // Load the PNG into GcBitmap, apply in-place grayscale effect,
  58. // convert to grayscale bitmap and append it to the TIFF:
  59. bmp.ApplyEffect(GrayscaleEffect.Get());
  60. using var gbmp = bmp.ToGrayscaleBitmap();
  61. tiffWriter.AppendFrame(gbmp);
  62. }
  63. }
  64. // At this point tiffWriter has the newly created TIFF. If it was created on
  65. // a disk file, that file would contain the TIFF when tiffWriter is disposed.
  66. //
  67. // To use this sample in the demo browser, we have to convert the TIFF
  68. // back to a PDF that is returned to the controller.
  69. msTiff.Position = 0;
  70. var doc = new GcPdfDocument();
  71. using var tiffReader = new GcTiffReader(msTiff);
  72. List<IDisposable> disposables = new List<IDisposable>();
  73. foreach (var tp in tiffReader.Frames)
  74. {
  75. var img = tp.ToImage(ImageBinding.InMemoryData);
  76. var p = doc.Pages.Add(new SizeF(img.Width / img.HorizontalResolution * 72, img.Height / img.VerticalResolution * 72));
  77. p.Graphics.DrawImage(img, p.Bounds, null, ImageAlign.Default);
  78. disposables.Add(img);
  79. }
  80. // Save the PDF:
  81. doc.Save(stream);
  82. // We can only dispose images after saving the PDF:
  83. disposables.ForEach(d_ => d_.Dispose());
  84. return doc.Pages.Count;
  85. }
  86. }
  87. }
  88.