//
// このコードは、DioDocs for PDF のサンプルの一部として提供されています。
// Copyright (c) GrapeCity inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using System.Collections.Generic;
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Pdf.Layers;
using GrapeCity.Documents.Pdf.Annotations;
using GrapeCity.Documents.Pdf.Graphics;
using GrapeCity.Documents.Text;
using GrapeCity.Documents.Drawing;
namespace GcPdfWeb.Samples
{
// このサンプルでは、住宅の電気設計図の一部分を示した PDF のセットから、
// マルチレイヤーの PDF ドキュメントを作成しています。
// それぞれの PDF は別のレイヤーとして追加されます。
// 結果として出力された PDF では、住宅の電気配線の一部(例:空調の設定だけ、
// コンセントだけなど)を選択的に閲覧できるオプションコンテンツ(レイヤー)を
// 利用することができます。
public class HousePlanLayers
{
public void CreatePDF(Stream stream)
{
// ドキュメントの内容がわかるファイル名部分の一覧
List<(string,string)> fnames = new List<(string, string)>()
{
( "full_electrical_plan.pdf", "完全な電気設計図" ),
( "all_outlets.pdf", "すべてのコンセント" ),
( "data_plan_and_detectors.pdf", "データプランと検知器" ),
( "HVAC_with_wiring.pdf", "配線を含む空調設定" ),
( "lighting_plan.pdf", "照明プラン" ),
( "lighting_plan_with_wiring.pdf", "配線を含む照明プラン" ),
( "security_system_plan.pdf", "セキュリティシステム" )
};
// 共通の PDF ファイル名
var fbase = "how_to_read_electrical_plans_";
// PDF が配置されたディレクトリ
var dir = Path.Combine("Resources", "PDFs");
GcPdfDocument doc = null;
Page page = null;
GcPdfGraphics g = null;
var disposables = new List<IDisposable>();
// すべての PDF を1つのドキュメントにまとめます。
// 最初の PDF をベースとして使用し、他の PDF を
// オプションのコンテンツ(レイヤー)として追加していきます。
for (int i = 0; i < fnames.Count; ++i)
{
var fname = fnames[i].Item1;
var iname = fnames[i].Item2;
var idoc = new GcPdfDocument();
var ifs = File.OpenRead(Path.Combine(dir, fbase + fname));
idoc.Load(ifs);
disposables.Add(ifs);
if (i == 0)
{
doc = idoc;
page = idoc.Pages.Last;
g = page.Graphics;
}
else
{
doc.OptionalContent.AddLayer(iname);
doc.OptionalContent.SetLayerDefaultState(iname, false);
g.BeginLayer(iname);
g.DrawPdfPage(idoc.Pages[0], page.Bounds);
g.EndLayer();
}
}
// PDF を保存します。
doc.Save(stream);
// ファイルストリームを廃棄します。
disposables.ForEach(d_ => d_.Dispose());
}
}
}