単純な直線
単純な直線のみのPDFファイルを出力するサンプルです。
左下が原点となるため、用紙の左下の方に直線が描画されます。
//以下の参照設定を追加してください。
//using PdfGeneratorNetFree;
//using PdfGeneratorNetFree.PdfContentItem;
//using PdfGeneratorNetFree.PgnEnum;
//using PdfGeneratorNetFree.PgnStyle;
var doc = new PdfDocument();
doc.AddPage("page1");
var content = new AbstractPdfContentItem[] { new Line(0, 100, 200, 300) };
doc.AddContent("page1", content);
File.WriteAllBytes("SimpleLineSample.pdf", doc.GetBinary());
実行結果:SimpleLineSample.pdf
オレンジ色の破線
オレンジ色の破線のPDFファイルを出力するサンプルです。
//プロジェクトのプロパティで「System.Drawing」の参照を追加し、
//ソースコードのファイルの先頭に「using System.Drawing;」を追加する。
var dashStyle = new StrokeStyle(5, Color.DarkOrange, DashType.DashDot);
var doc = new PdfDocument();
doc.AddPage("page1");
var content = new AbstractPdfContentItem[1];
content[0] = new Line(0, 100, 200, 300, dashStyle);
doc.AddContent("page1", content);
File.WriteAllBytes("DashLineSample.pdf", doc.GetBinary());
実行結果:DashLineSample.pdf
PDFファイルを開くときにパスワードを要求(PDFファイルの暗号化)
Adobe Reader等でPDFファイルを開くときにパスワードを要求するサンプルです。
(サンプルのパスワードは「user」としています。)
var doc = new PdfDocument();
doc.AddPage("page1");
var content = new AbstractPdfContentItem[] { new Line(0, 100, 200, 300) };
doc.AddContent("page1", content);
doc.SetOpenPassword("user"); //ファイルを開くために必要なパスワードを設定
File.WriteAllBytes("EncryptionSample_password-user.pdf", doc.GetBinary());
実行結果:EncryptionSample_password-user.pdf
PDFファイルのプロパティ情報を設定
PDFファイルのプロパティ情報を設定するサンプルです。
var doc = new PdfDocument();
doc.AddPage("page1");
var content = new AbstractPdfContentItem[] { new Line(0, 100, 200, 300) };
doc.AddContent("page1", content);
doc.SetDocInfo("PDFファイルのタイトル", "PDFファイルのサブタイトル", "PDFファイルの作成者");
File.WriteAllBytes("PdfPropertySample.pdf", doc.GetBinary());
プロパティ情報
実行結果:PdfPropertySample.pdf
特殊な用紙サイズを指定
任意の大きさの用紙(横300pt×縦400pt)に出力するサンプルです。
Adobe Readerが拡大
var doc = new PdfDocument();
doc.AddPage("page1", PaperSize.Custom(300, 400));
doc.AddPage("page2", PaperSize.A4Landscape);
var content = new AbstractPdfContentItem[] { new Line(0, 100, 200, 300) };
doc.AddContent("page1", content);
doc.AddContent("page2", content);
File.WriteAllBytes("CustomSizePageSample.pdf", doc.GetBinary());
実行結果:CustomSizePageSample.pdf
文字列(外字・異体字を含む)
文字列をPDFファイルを出力するサンプルです。
外字、異体字も出力しています。(サンプルにはサロゲートペアの文字を出力していませんが、通常の文字と同様に出力可能です。)外字を出力しない場合は、PdfFontEudcの処理は不要です。
var doc = new PdfDocument();
//IPAexフォント、外字フォントのファイルのある場所を指定する。
//(サンプルを動作させる場合は最初の外字0xE000が登録済であること。)
var windir = Environment.ExpandEnvironmentVariables("%windir%");
var eudc = windir + @"\Fonts\EUDC.TTE";
doc.AddFont("gothic", "ipaexg.ttf");
doc.AddFont("eudc", eudc);
var textStyle = new TextStyle("gothic", fontSize: 30, eudcFontName: "eudc");
//文字コードではなく普通の文字と同様の文字を入力して問題ないが、
//ブラウザ等の環境によって文字化けするため異体字・外字は文字コードで入力している。
var content = new AbstractPdfContentItem[2];
content[0] = new Text(10, 450, 840, 30, "IVS異体字や外字「\uE000」OK", textStyle);
var str = "「葛」が問題の「葛\uDB40\uDD00城市」「葛\uDB40\uDD01飾区」も正しく表示";
content[1] = new Text(10, 400, 840, 30, str, textStyle);
doc.AddPage("page1", PaperSize.A4Landscape);
doc.AddContent("page1", content);
File.WriteAllBytes("TextSample.pdf", doc.GetBinary());
実行結果:TextSample.pdf
テンプレートの使用例
一覧表の枠線やヘッダー、フッターなど複数のページで同じものを繰り返し出力することがあります。
これらのデータはページ毎に変動するデータと区別したcontentとして登録(下例の場合、template)し、複数のページで使いまわすことができます。これにより、PDFファイルの作成時間、ファイルサイズにメリットがあります。
var doc = new PdfDocument();
//フォントファイルのある場所を指定する。
var windir = Environment.ExpandEnvironmentVariables("%windir%");
doc.AddFont("mincho", windir + @"\Fonts\msmincho.ttc", 0);
var textStyle = new TextStyle("mincho", hAlign: HorizontalAlign.Center);
var template = new AbstractPdfContentItem[]
{
new Rect(30, 800, 150, 20),
new Rect(30, 780, 150, 20),
new Rect(30, 760, 150, 20),
};
var content1 = new AbstractPdfContentItem[]
{
new Text(30, 800, 150, 20, "枠線などの定型部分を", textStyle),
new Text(30, 780, 150, 20, "使いまわすと", textStyle),
new Text(30, 760, 150, 20, "ファイルサイズを小さくし", textStyle),
};
var content2 = new AbstractPdfContentItem[]
{
new Text(30, 800, 150, 20, "作成時間を短くする", textStyle),
new Text(30, 780, 150, 20, "効果あり!!", textStyle),
};
doc.AddPage("page1");
doc.AddPage("page2");
doc.AddContent("page1", template, contentName: "table");
doc.AddContent("page1", content1);
doc.AddContent("page2", "table");
doc.AddContent("page2", content2);
File.WriteAllBytes("TemplateSample.pdf", doc.GetBinary());
実行結果:TemplateSample.pdf