1

我正在使用 Migradoc 在我的应用程序中生成 PDF 文档。

现在我需要根据内容高度生成一个固定宽度和动态计算高度的单页文档。

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(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 提供的信息和链接,它帮助我解决了这个问题。

于 2016-03-15T10:50:53.330 回答