【问题标题】:itextsharp c# need a perfect method for page breakitextsharp c#需要一个完美的分页方法
【发布时间】:2017-06-06 05:48:25
【问题描述】:
我正在使用itextsharp 库。我设计了一个 HTML 页面并转换为 PDF 。在这种情况下,某些表没有完美拆分,行也没有正确拆分
table.keepTogether;
table.splitRows;
table.splitLate
我尝试了这个扩展,但它不能正常工作,它弄乱了我的 CSS 和 PDF 中的数据。如果你有方法..回答我:)
【问题讨论】:
标签:
c#
.net
asp.net-mvc
itext
【解决方案1】:
终于明白了
public class TableProcessor : Table
{
const string NO_ROW_SPLIT = "no-row-split";
public override IList<IElement> End(IWorkerContext ctx, Tag tag, IList<IElement> currentContent)
{
IList<IElement> result = base.End(ctx, tag, currentContent);
var table = (PdfPTable)result[0];
if (tag.Attributes.ContainsKey(NO_ROW_SPLIT))
{
// if not set, table **may** be forwarded to next page
table.KeepTogether = false;
// next two properties keep <tr> together if possible
table.SplitRows = true;
table.SplitLate = true;
}
return new List<IElement>() { table };
}
}
使用这个类和
var tagfac = Tags.GetHtmlTagProcessorFactory();
tagfac.AddProcessor(new TableProcessor(), new string[] { HTML.Tag.TABLE });
htmlContext.SetTagFactory(tagfac);
将此方法与 htmlpipeline 上下文集成
此方法每次在标签有no-row-split时运行。如果它包含键,它将通过keep together关键字保持分页
html
<table no-row-split style="width:100%">
<tr>
<td>
</td>
</tr>
</table>