【问题标题】:text not wrapping in table cell using iText5 for .NET使用 iText5 for .NET 的文本未在表格单元格中换行
【发布时间】:2014-11-11 00:33:44
【问题描述】:

我有一个生成文本报告的函数。我从数据库中获取文档文本列表并创建 PDF 报告。 PDF 报告格式应如下所示

logo(左上)............一些关于公司的静态文本(右上) [标题信息是……………………………………………………………………………… .........................对于列表中的每个文档都不同] […………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………文档文本...] 日期(左下) 页数(右下)

文档文本可能超过一页,在这种情况下,该文档的标题信息会重复。并且,如果文档文本运行第二页并在中间页面终止,我们将在新页面上开始下一个文档。这是我的尝试:

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();

    } 

这显示了报告和所有必需的值,除了在内容表中,当文档文本有很长的行时,它会超出页面的边缘并且您看不到它。有没有办法更好地实现这一目标?谢谢!

【问题讨论】:

    标签: c# pdf printing itext


    【解决方案1】:

    我首先固定页面的边距,如下所示:

    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;
    }
    

    【讨论】:

    • 这更像是一个重构建议。我按照你的建议做了,但是当我在主表中添加外部表和内容表时,两个单元格并行保持空间。有没有设置让这两个单元格重叠,所以第一个字段可以在顶部,下一个在底部?
    • 我没有完全关注,但你的意思是表格单元格内的内容并排放置,彼此相对?
    • mainTable.AddCell(outerTblCell); mainTable.AddCell(contentTblCell);这两个表在 mainTable 中并排显示
    • 我认为这是因为您为 mainTable 指定了 2 列。因此,当您调用 AddCell 两次时,它会将其创建到同一行的两个单元格中。您需要将outerTblCellcontentTblCell 上的colspan 属性设置为2。这将导致它占据mainTable 的整个宽度,因此彼此重叠。
    • 你把我引向了正确的方向,所以我会把它标记为答案。谢谢!
    猜你喜欢
    • 2022-12-14
    • 2016-09-09
    • 2017-12-15
    • 1970-01-01
    • 2012-01-16
    • 2015-07-01
    • 2010-09-22
    • 2018-09-18
    • 1970-01-01
    相关资源
    最近更新 更多