1

アプリケーションで PDF ドキュメントを生成するために Migradoc を使用しています。

ここで、固定幅とコンテンツの高さに基づいて動的に計算された高さを持つ単一ページのドキュメントを生成する必要があります。

var document = new Document();
var section = doc.AddSection();
section.PageSetup.PageWidth = "100mm";
// section.PageSetup.PageHeight = ???
var p1 = section.AddParagraph();
// ...
var p2 = section.AddParagraph();
// ...

この問題を解決するには?

4

2 に答える 2

3

ページの高さを非常に大きな値に設定してから、ドキュメントをレンダリングします。レンダリングが完了すると、すべての位置と寸法の情報にアクセスできます (このスレッドで説明されているように、新しいメソッドを追加する必要がある場合があります: http://forum.pdfsharp.net/viewtopic.php?p=1904#p1904 )。

おそらく、最後に空のダミー段落を追加し、このダミー段落の Y 位置を使用して必要な高さを決定します。

新しいページ サイズでドキュメントを再度レンダリングするか、PDFsharp で PDF ファイルを開き、MediaBox を新しい必要なサイズに設定します。

于 2014-05-17T11:25:17.543 に答える
0

動的なページの高さを持つスリップ プリンター (税の請求書、シフト レポートなど) のドキュメントを作成しなければならないという同様の問題がありました。

/pdfsharp/PDFsharp 1.50 (ベータ 3)/PDFsharp-MigraDocFoundation-1_50-beta3b.zip ソース コード (この記事の執筆時点) をダウンロード(ページの下部) し、ビルドしてプロジェクトに dll を追加します。

PageHeight の最初に途方もなく大きな高さを設定します

Document document = new Document();

Section section = document.AddSection();
section.PageSetup.PageWidth = Unit.FromMillimeter(100);
section.PageSetup.PageHeight = Unit.FromMillimeter(10000);

//add tables etc.
//note: all my tables are added using document.LastSection.Add(table)

DocumentRenderer renderer = new DocumentRenderer(document);
renderer.PrepareDocument();

RenderInfo[] info = renderer.GetRenderInfoFromPage(1);
int index = info.Length - 1;

double stop = info[index].LayoutInfo.ContentArea.Y.Millimeter + info[index].LayoutInfo.ContentArea.Height.Millimeter; //add more if you have bottom page margin, borders on the last table etc.
section.PageSetup.PageHeight = Unit.FromMillimeter(stop);

これはシナリオで 100% 正確に機能しない場合がありますが、最後に追加されたテーブルの座標とサイズを取得し、そこからページの高さを計算できます。

情報とリンクを提供してくれた ThomasH に感謝し、これを理解するのに役立ちました。

于 2016-03-15T10:50:53.330 に答える