我有一个类似的问题,我必须为带有动态页面高度的单据打印机(税务发票、班次报告等)创建文档。
下载(在页面底部)/pdfsharp/PDFsharp 1.50(beta 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 提供的信息和链接,它帮助我解决了这个问题。