Migradoc动态页面大小

时间:2014-05-17 07:26:13

标签: c# pdf-generation pdfsharp migradoc

我正在使用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();
// ...

如何解决这个问题?

2 个答案:

答案 0 :(得分:3)

这就是我要做的:我将页面高度设置为一个非常大的值,然后我将渲染文档。渲染完成后,您可以访问所有位置和尺寸信息(这可能需要添加此线程中描述的新方法:http://forum.pdfsharp.net/viewtopic.php?p=1904#p1904)。

可能我会在末尾添加一个空的虚拟段落,并使用此虚拟段落的Y位置来确定所需的高度。

使用新页面大小再次渲染文档或使用PDFsharp打开PDF文件,并将MediaBox设置为新需要的大小。

答案 1 :(得分:0)

我遇到了类似的问题,我必须使用动态页面高度为滑动打印机(税务发票,班次报告等)创建文档。

Download(在页面底部)/ 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的信息和链接,它帮助我解决了这个问题。