動的なページの高さを持つスリップ プリンター (税の請求書、シフト レポートなど) のドキュメントを作成しなければならないという同様の問題がありました。
/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 に感謝し、これを理解するのに役立ちました。