文本未使用iText5 for.NET在表格单元格中换行
本文关键字:表格 单元格 换行 NET 未使用 iText5 for 文本 | 更新日期: 2023-09-27 18:30:00
我有一个生成文本报告的函数。我从数据库中获得了一个文档文本列表,并创建了一个PDF报告。PDF报告格式应如下所示
徽标(左上)。。。。。。。。。一些关于公司的静态文本(右上角)[标题信息。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。…………..列表中的每个文件不同][…………文件文本日期(左下)页数(右下)
文档文本可以运行多个页面,在这种情况下,该文档的标题信息会重复。而且,如果文档文本运行第二个页面并在页面中间终止,我们将在新页面上开始下一个文档。这是我的尝试:
public static void printDocument(List<PdfData> lst, ref MemoryStream memoryStream)
{
iTextSharp.text.Font baseFontBig = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12f, iTextSharp.text.Font.BOLD, iTextSharp.text.BaseColor.BLACK);
iTextSharp.text.Document document = new iTextSharp.text.Document(PageSize.LETTER, 40, 25, 30, 30);
PDFEvents e = new PDFEvents();
PdfWriter pw = PdfWriter.GetInstance(document, memoryStream);
pw.PageEvent = e;
document.Open();
for (int i = 0; i < lst.Count; i++)
{
PdfPTable mainTable = new PdfPTable(2);
PdfPTable outerTable = new PdfPTable(4);
PdfPTable contentTable = new PdfPTable(1);
mainTable.KeepTogether = true;
outerTable.WidthPercentage = 50;
outerTable.KeepTogether = false;
outerTable.TotalWidth = 550f;
float[] sglTblHdWidths = new float[4];
sglTblHdWidths[0] = 5f;
sglTblHdWidths[1] = 15f;
sglTblHdWidths[2] = 5f;
sglTblHdWidths[3] = 5f;
outerTable.SetWidths(sglTblHdWidths);
outerTable.LockedWidth = true;
contentTable.LockedWidth = true;
outerTable.KeepTogether = true;
contentTable.WidthPercentage = 90;
contentTable.KeepTogether = false;
contentTable.TotalWidth = 800f;
contentTable.SpacingBefore = 30f;
string documentIdentifier = lst[i].patient;
string documentStatus =lst[i].documentStatus;
StringBuilder sb = new StringBuilder();
sb.Append(lst[i].documentText);
iTextSharp.text.Paragraph documentStatusP = new iTextSharp.text.Paragraph(documentStatus, new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.COURIER, 12, iTextSharp.text.Font.BOLD, iTextSharp.text.BaseColor.BLACK));
iTextSharp.text.Paragraph patient = new iTextSharp.text.Paragraph(documentIdentifier, new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.COURIER, 12, iTextSharp.text.Font.BOLD, iTextSharp.text.BaseColor.BLACK));
iTextSharp.text.Paragraph content = new iTextSharp.text.Paragraph(sb.ToString(), new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.COURIER, 10));
iTextSharp.text.Paragraph empty = new iTextSharp.text.Paragraph("", new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.COURIER, 12));
#region Content
PdfPCell contentCell = new PdfPCell(content);
contentCell.Border =1;
contentCell.VerticalAlignment = Element.ALIGN_TOP;
contentCell.HorizontalAlignment = Element.ALIGN_LEFT;
contentTable.AddCell(contentCell);
#endregion
PdfPCell firstHeader = new PdfPCell(empty);
firstHeader.Border = 0;
firstHeader.FixedHeight = 17f;
outerTable.AddCell(firstHeader);
PdfPCell secondHeader = new PdfPCell(patient);
secondHeader.Border = 0;
secondHeader.FixedHeight = 17f;
secondHeader.VerticalAlignment = 2;
outerTable.AddCell(secondHeader);
PdfPCell thirdHeader = new PdfPCell(documentStatusP);
thirdHeader.Border = 0;
thirdHeader.FixedHeight = 17f;
thirdHeader.HorizontalAlignment = 1;
thirdHeader.VerticalAlignment = 2;
outerTable.AddCell(thirdHeader);
PdfPCell forthHeader = new PdfPCell(empty);
forthHeader.Border = 0;
forthHeader.FixedHeight = 17f;
outerTable.AddCell(forthHeader);
PdfPCell outerTblCell = new PdfPCell(outerTable);
outerTblCell.Border = 0;
PdfPCell contentTblCell = new PdfPCell(contentTable);
contentTblCell.Border = 0;
mainTable.AddCell(outerTblCell);
mainTable.AddCell(contentTblCell);
document.Add(mainTable);
if (i != lst.Count - 1)
document.NewPage();
}
document.Close();
}
这显示了报告和所有必需的值,除了在内容表中,当文档文本有很长的行时,它超出了页面的边距,而你看不到它。有什么方法可以更好地实现这一点吗?谢谢
我首先修复页面的边距,如下所示:
const float leftMargin = 34;
const float rightMargin = 34;
const float topMargin = 134;
const float bottomMargin = 36;
var document = new Document(PageSize.A4, leftMargin, rightMargin, topMargin, bottomMargin);
然后我确定桌子的宽度。在我的情况下,文档是在纵向A4页面上,所以我知道页面的最大宽度,可以减去边距。我这样构建表格:
private PdfPTable CreateTable()
{
const float tableWidth = 527;
var table = new PdfPTable(1)
{
HorizontalAlignment = Element.ALIGN_LEFT,
TotalWidth = tableWidth,
LockedWidth = true,
SplitLate = true,
SplitRows = true
};
return table;
}