我有一个生成文本报告的功能。我从数据库中获取文档文本列表,然后创建PDF报告。 PDF报告格式应如下
logo(top left).........some static text about company (top right) [header information that is...................................... .........................different for each document in the list] [.....................document text.............................] date(bottom left) page count(bottom right)
文档文本可能会运行多个页面,在这种情况下,该文档的标题信息会重复。并且,如果文档文本运行第二页并在页面中间终止,我们将在新页面上启动下一个文档。这是我的尝试:
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();
}
这显示了报告和所有必需的值,但在内容表中,当文档文本的行很长时,它会在页面的边距之外运行,而您无法看到它。有没有办法更好地实现这一目标?谢谢!
答案 0 :(得分:0)
我首先修复页面的边距,如下所示:
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;
}