【问题标题】:Migradoc dynamic page sizeMigradoc 动态页面大小
【发布时间】:2014-05-17 07:26:13
【问题描述】:

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

如何解决这个问题?

【问题讨论】:

    标签: c# pdf-generation pdfsharp migradoc


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多