【问题标题】:MigraDOC and pdfSharp, center dynamic textMigraDOC 和 pdfSharp,居中动态文本
【发布时间】:2016-06-10 08:49:45
【问题描述】:
我一直在寻找创建文档 (PDF) 的解决方案,以便我可以计算输入字段:
PDF 内容应如下所示
(所有pdf内容需要居中)
标题
Field1 : 非动态(需要居中)
Field2 : UserName (动态- 需要附加在
段落) - 因为每个用户的姓名长度不同
所以我的问题是,pdfSharp 或 migraDoc 是否有一种方法或某些东西可以将文本对齐到中心(这意味着它会进行一些计算 - 确定字体系列、字体大小并发挥作用,以便最终标记的文本居中)?
如果是这样,那么方法是什么,因为我搜索了 migraDoc 和 pdfSharp 文档但找不到类似的东西。
如果这种方法不存在,有人尝试过吗?使用它?有什么建议我该如何实现这种行为?也许有一些来源可以查看。
谢谢你
【问题讨论】:
-
您查看this 示例代码了吗?它有很多。这对我帮助很大。
标签:
java
c#
.net
pdfsharp
migradoc
【解决方案1】:
Sample 1 用大量代码示例展示了如何创建 pdf 并使用 Migradoc 提供的几乎所有功能。 Sample 2 详细展示了如何创建表格,这对于您可能对页面内容的布局也很感兴趣。
对齐(居中/左/右)通常通过设置Format.Alignment 属性来完成,如下所示:
Paragraph par = new Paragraph();
par.Format.Alignment = ParagraphAlignment.Center;
具有居中内容的文档的简短版本是:
// first you need a document
Document MigraDokument = new Document();
// each document needs at least one section
Section section = MigraDokument.AddSection();
section.PageSetup.PageFormat = PageFormat.A4;
// and then you add paragraphs to the section
Paragraph par = section.AddParagraph();
// and set the alignment as you wish
par.Format.Alignment = ParagraphAlignment.Center;
// now just fill it with content and set the rest of the parameters...
par.AddText("text");