GetImageProperties.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.Graphics.Images;
  11. using GrapeCity.Documents.Pdf.Spec;
  12. using GrapeCity.Documents.Text;
  13. using GrapeCity.Documents.Drawing;
  14.  
  15. namespace DsPdfWeb.Demos
  16. {
  17. // This example shows how to fetch low level information about images living in a PDF file,
  18. // such as the ID of the image object in the PDF, the filter used to store the image,
  19. // the decoder parameters, the content of the PdfImage dictionary, etc.
  20. // The original PDF is appended to the result for reference.
  21. public class GetImageProperties
  22. {
  23. public int CreatePDF(Stream stream)
  24. {
  25. using var fs = File.OpenRead(Path.Combine("Resources", "PDFs", "Wetlands.pdf"));
  26. var docSrc = new GcPdfDocument();
  27. docSrc.Load(fs);
  28.  
  29. // Get the images in the loaded PDF and print their info into the resulting PDF:
  30. var imageInfos = docSrc.GetImages();
  31.  
  32. // The PDF to hold the results:
  33. var doc = new GcPdfDocument();
  34. var page = doc.NewPage();
  35. // Set up a TextLayout to format the results:
  36. var tl = page.Graphics.CreateTextLayout();
  37. tl.DefaultFormat.Font = StandardFonts.Courier;
  38. tl.DefaultFormat.FontSize = 14;
  39. tl.MaxWidth = doc.PageSize.Width;
  40. tl.MaxHeight = doc.PageSize.Height;
  41. tl.MarginAll = tl.Resolution;
  42. var captionFmt = new TextFormat(tl.DefaultFormat) { Font = StandardFonts.CourierBold, FontSize = tl.DefaultFormat.FontSize + 2 };
  43.  
  44. int i = 0;
  45. foreach (var imageInfo in imageInfos)
  46. {
  47. tl.AppendLine($"Image {++i}", captionFmt);
  48.  
  49. // PdfImageBase class represents an image in the loaded PDF file:
  50. PdfImageBase img = imageInfo.Image;
  51.  
  52. tl.AppendLine($"PdfImage object ID: {img.ObjID}");
  53. // PdfImageBase is derived from PdfDictWrapper type, it has methods
  54. // that allow you to access properties/data of the underlying PDF stream object:
  55. using (PdfStreamInfo psi = img.GetPdfStreamInfo())
  56. {
  57. tl.AppendLine($" Image stream length: {psi.Stream.Length}");
  58. tl.AppendLine($" ImageFilterName: {psi.ImageFilterName}");
  59. tl.AppendLine($"ImageFilterDecodeParams: {psi.ImageFilterDecodeParams}");
  60. // Dump the content of the ImageFilterDecodeParams if it exists:
  61. if (psi.ImageFilterDecodeParams != null)
  62. {
  63. foreach (var kvp in psi.ImageFilterDecodeParams.Dict)
  64. {
  65. tl.AppendLine($"{kvp.Key}: {kvp.Value}");
  66. }
  67. // An example of how to get the value of BlackIs1:
  68. var blackIs1 = psi.ImageFilterDecodeParams.GetBool(PdfName.Std.BlackIs1, null);
  69. tl.AppendLine($"BlackIs1: {blackIs1}");
  70. }
  71. }
  72. // Dump the properties of PdfImage dictionary:
  73. tl.AppendLine();
  74. tl.AppendLine("Properties of PdfImage dictionary:");
  75. foreach (KeyValuePair<PdfName, IPdfObject> kvp in img.PdfDict.Dict)
  76. {
  77. tl.AppendLine($"{kvp.Key}: {kvp.Value}");
  78. }
  79. //
  80. var cs = img.Get<IPdfObject>(PdfName.Std.ColorSpace);
  81. tl.AppendLine($"ColorSpace: {cs.GetType().Name} {cs}");
  82. var bpc = img.Get<IPdfObject>(PdfName.Std.BitsPerComponent);
  83. tl.AppendLine($"BitsPerComponent: {bpc?.GetType().Name} {bpc}");
  84. tl.AppendLine();
  85. }
  86.  
  87. // Render the results:
  88. tl.PerformLayout(true);
  89. while (true)
  90. {
  91. var splitResult = tl.Split(null, out TextLayout rest);
  92. page.Graphics.DrawTextLayout(tl, PointF.Empty);
  93. if (splitResult != SplitResult.Split)
  94. break;
  95. tl = rest;
  96. tl.MarginTop = tl.Resolution;
  97. page = doc.Pages.Add();
  98. }
  99.  
  100. // Append the source PDF to the result for reference:
  101. doc.MergeWithDocument(docSrc);
  102.  
  103. // Done:
  104. doc.Save(stream);
  105. return doc.Pages.Count;
  106. }
  107. }
  108. }
  109.