2016-11-15 3 views
1

私は、PDFsharpを使用して、モデル内の各アイテムの複数ページのドキュメントを作成しています。PDFsharpを使用したデータの配置

私は最終的にPDFが見えるように必要なものの大まかなモックアップ与えられている:、私は私が生成しなければならないPDFの二種類があり、長い話を短くカットするには

enter image description here

をテンプレートをフォローする必要があるのは複数ページのPDFなので、下のコードスニペットの変数_numberOfPagesです。

私は実際には、複数のページのPDF版の各項目ごとに情報を生成しています。リクエストに含まれるアイテムの数に応じて、新しいページを追加して、現在のページのアイテム配列のインデックスを取得し、そのような行を追加します。

PdfDocumentクラス

public PdfDocument BuildPdfDocument() 
    { 
     if (_numberOfPages > 1) 
     { 
      PdfDocument pdfDocument = new PdfDocument(); 
      for (int i = 0; i < _numberOfPages; i++) 
      { 
       PdfPage page = pdfDocument.AddPage(); 
       page.Orientation = PageOrientation.Landscape; 
       XGraphics gfx = XGraphics.FromPdfPage(page); 
       XFont font = new XFont("Arial", 30, XFontStyle.Regular); 
       XTextFormatter tf = new XTextFormatter(gfx); 
       XRect rect = new XRect(0, (page.Height/4), page.Width, page.Height); 
       gfx.DrawRectangle(XBrushes.White, rect); 
       tf.Alignment = XParagraphAlignment.Left; 
       tf.DrawString(_request.GetText(i), font, XBrushes.Black, rect); 
      } 

       return pdfDocument; 
     } 

     else 
     { 
      // stuff relating to the other pdfDocument that is OK 
     } 
    } 

はFileRequestクラスはGetTextは

public string GetText(int pageNo) 
{ 
    int[] fileRequestItems = this.FileRequestItems.Select(x => x.Id).ToArray(); 
    var currentFileRequestItem = fileRequestItems[pageNo]; 
    var items = FileRequestItems; 

    FileRequestItem item = items.FirstOrDefault(s => s.Id == currentFileRequestItem); 

    StringBuilder sb = new StringBuilder(); 

    if (item.ObjectContextGetType() == typeof(FileRequestDocument)) // its a document so provide text for the PDF like this 
    { 
     var fileRequestDocument = item as FileRequestDocument; 

     sb.AppendLine("Date Requested: " + this.DateOfRequest.ToString("D")); 
     if (fileRequestDocument != null) sb.AppendLine("Box No: " + fileRequestDocument.Record.Box.BoxNumber); 
     sb.AppendLine("Location: " + this.Location.LocationName); 
     if (fileRequestDocument != null) sb.AppendLine("Description: " + fileRequestDocument.Record.Description); 
     sb.AppendLine("Requested By: " + this.Name); 
     sb.AppendLine("Tel: " + this.TelephoneNumber); 
     sb.AppendLine("Department: " + this.Department); 
    } 

    if (item.ObjectContextGetType() == typeof(FileRequestBox)) // its a box so provide text for the PDF like this 
    { 
     var fileRequestBox = item as FileRequestBox; 

     sb.AppendLine("Date Requested: " + this.DateOfRequest.ToString("D")); 
     if (fileRequestBox != null) sb.AppendLine("Box No: " + fileRequestBox.Box.BoxNumber); 
     sb.AppendLine("Location: " + this.Location.LocationName); 
     if (fileRequestBox != null) sb.AppendLine("Description: " + $"Entire Contents of Box Number {fileRequestBox.Box.BoxNumber}"); 
     sb.AppendLine("Requested By: " + this.Name); 
     sb.AppendLine("Tel: " + this.TelephoneNumber); 
     sb.AppendLine("Department: " + this.Department); 
    } 

    return sb.ToString(); 
} 

このすべての結果は以下のPDFになります5つの項目を含むファイル要求であると呼ばれるメソッドが含まれています5ページ以上、名前/日付などが繰り返されているものの、FileRequestItemまたはページに固有の説明。あなたが見ることができるような情報がすべてありますが

enter image description here

は、位置決めが必要なテンプレートと一致していません。

ページごとに指定したテンプレートなどを作成し、それに応じてデータを配置するにはどうすればよいですか?

答えて

1

ニースは長いマルチライン文字列をXTextFormatterに渡そうとします。あなたがポジションについてのコントロールを必要とする場合、間違ったアプローチ。

ポジショニングを完全に制御するには、個々の項目にDrawStringと電話してください。

私はMigraDocを使用します:MigraDocを使用すると、テーブルを作成して個々のテーブルセルに情報を入れることができます。

MigraDocはPDFシャープを使用してPDFファイルを作成しますが、より高いAPIレベルを提供します。
http://www.pdfsharp.net/wiki/Invoice-sample.ashx

関連する問題