我正在使用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();
// ...如何解决这个问题?
发布于 2014-05-17 11:25:17
下面是我要做的事情:我会将页面高度设置为非常大的值,然后呈现文档。一旦呈现完毕,您就可以访问所有的位置和维度信息(这可能需要添加一个新方法,如这个线程中所描述的:http://forum.pdfsharp.net/viewtopic.php?p=1904#p1904)。
可能我会在末尾添加一个空的虚拟段落,并使用这个虚拟段落的Y位置来确定所需的高度。
使用新的页面大小重新呈现文档,或者用PDFsharp打开MediaBox文件,并将MediaBox设置为新需要的大小。
发布于 2016-03-15 10:50:53
我也遇到了一个类似的问题,我不得不为一张印表机创建文件(税务发票,转移报告等)。具有动态页面高度。
3)/PDFsharp-MigraDocFoundation-1_50-beta3b.zip (在页面底部) /pdfsharp/PDFsharp 1.50 (beta 下载源代码(在撰写本文时),构建它并将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);这可能不会完全适用于您的场景,但是您将能够获得添加的最后一个表的坐标和大小,并从那里计算出您的页面高度。
感谢ThomasH提供的信息和链接,它帮助我解决了这个问题。
https://stackoverflow.com/questions/23708750
复制相似问题