RemoveImageLocation.cs
- //
- // このコードは、DioDocs for PDF のサンプルの一部として提供されています。
- // © MESCIUS inc. All rights reserved.
- //
- using System;
- using System.IO;
- using System.Drawing;
- using System.Text;
- using GrapeCity.Documents.Pdf;
- using GrapeCity.Documents.Text;
- using GrapeCity.Documents.Drawing;
- using System.Linq;
-
- namespace DsPdfWeb.Demos
- {
- // このデモでは、特定の場所にある画像を削除する方法を紹介しています。
- // PDFから GcPdfDocument.RemoveImages() メソッドを使用して画像を削除します。
- public class RemoveImageLocation
- {
- public int CreatePDF(Stream stream)
- {
- using var fs = File.OpenRead(Path.Combine("Resources", "PDFs", "ImageTransparency.pdf"));
- var doc = new GcPdfDocument();
- doc.Load(fs);
- // PDF内の画像リストを取得
- var imageInfos = doc.GetImages();
- // 複数の同じ画像が配置されている場合に一番左の画像を削除します。
- foreach (var ii in imageInfos)
- {
- if (ii.Locations.Count > 1)
- {
- int iLeft = -1;
- for (int i = 0; i < ii.Locations.Count; i++)
- {
- if (iLeft == -1 || ii.Locations[i].PageBounds.ToRect().Left < ii.Locations[iLeft].PageBounds.ToRect().Left)
- iLeft = i;
- }
- // 削除したい画像のコレクションを作成します。
- for (int i = ii.Locations.Count - 1; i >= 0; i--)
- if (i != iLeft)
- ii.Locations.RemoveAt(i);
- // コレクションに含まれている画像を削除します。
- doc.RemoveImages([ii]);
- }
- }
- // PDF ドキュメントを保存します。
- doc.Save(stream);
- return doc.Pages.Count;
- }
- }
- }
-