RemoveImageLocation.cs
  1. //
  2. // このコードは、DioDocs for PDF のサンプルの一部として提供されています。
  3. // © MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using System.Text;
  9. using GrapeCity.Documents.Pdf;
  10. using GrapeCity.Documents.Text;
  11. using GrapeCity.Documents.Drawing;
  12. using System.Linq;
  13.  
  14. namespace DsPdfWeb.Demos
  15. {
  16. // このデモでは、特定の場所にある画像を削除する方法を紹介しています。
  17. // PDFから GcPdfDocument.RemoveImages() メソッドを使用して画像を削除します。
  18. public class RemoveImageLocation
  19. {
  20. public int CreatePDF(Stream stream)
  21. {
  22. using var fs = File.OpenRead(Path.Combine("Resources", "PDFs", "ImageTransparency.pdf"));
  23. var doc = new GcPdfDocument();
  24. doc.Load(fs);
  25. // PDF内の画像リストを取得
  26. var imageInfos = doc.GetImages();
  27. // 複数の同じ画像が配置されている場合に一番左の画像を削除します。
  28. foreach (var ii in imageInfos)
  29. {
  30. if (ii.Locations.Count > 1)
  31. {
  32. int iLeft = -1;
  33. for (int i = 0; i < ii.Locations.Count; i++)
  34. {
  35. if (iLeft == -1 || ii.Locations[i].PageBounds.ToRect().Left < ii.Locations[iLeft].PageBounds.ToRect().Left)
  36. iLeft = i;
  37. }
  38. // 削除したい画像のコレクションを作成します。
  39. for (int i = ii.Locations.Count - 1; i >= 0; i--)
  40. if (i != iLeft)
  41. ii.Locations.RemoveAt(i);
  42. // コレクションに含まれている画像を削除します。
  43. doc.RemoveImages([ii]);
  44. }
  45. }
  46. // PDF ドキュメントを保存します。
  47. doc.Save(stream);
  48. return doc.Pages.Count;
  49. }
  50. }
  51. }
  52.