【问题标题】:How to introduce new line characters with iTextSharp如何使用 iTextSharp 引入换行符
【发布时间】:2016-09-30 12:28:16
【问题描述】:

我已经编写了以下代码来在 PDF 中编写文本,我想在一些文本之后换行。

 Dim document As Document

    document = New Document(PageSize.A4, 5.0F, 20.0F, 20.0F, 20.0F)

    Try

        Dim writer As PdfWriter
        writer = PdfWriter.GetInstance(document, New FileStream(filename, FileMode.Create))

        document.Open()

        Dim spacing As Integer

        spacing = 0

        Dim curY, lineHeight As Double

        curY = document.Top
        lineHeight = 0

        Const maxPerLine As Integer = 3


        For i As Integer = 0 To 5

            Dim table As PdfPTable
            table = New PdfPTable(4)

            table.DefaultCell.Border = Rectangle.NO_BORDER
            table.TotalWidth = 200.0F
            table.LockedWidth = True

            Dim cell As PdfPCell
            cell = New PdfPCell(New Phrase("hello \n" + i + "\n" + "wass up ?" ))
            cell.Colspan = 4
            cell.HorizontalAlignment = 0
            cell.Border = Rectangle.NO_BORDER
            cell.Padding = 30.0F
            table.AddCell(cell)

            table.WriteSelectedRows(0, -1, document.Left + spacing, curY, writer.DirectContent)

            spacing = spacing + 200

            lineHeight = Math.Max(lineHeight, table.TotalHeight)

            If 0 = (i + 1) Mod maxPerLine Then

                curY = curY - lineHeight
                spacing = 0
                lineHeight = 0

            End If

        Next

    Catch ex As Exception

    Finally

        document.Close()


    End Try

我已经尝试过使用段落,但我仍然无法在新行中输入文本。

我已经阅读了他们写的 iTextSharp 的文档,如果你想换行然后使用 "\n" 但它不起作用。

如何在一些文字后换行?

【问题讨论】:

    标签: vb.net pdf itext


    【解决方案1】:

    最简单的方法是在复合模式中使用PdfPCell(您使用的是文本模式)。当您使用AddElement 时,复合模式开始发挥作用:

    Dim cell As PdfPCell
    cell = New PdfPCell()
    cell.AddElement(New Paragraph("line 1"))
    cell.AddElement(New Paragraph("line 2"))
    

    请注意,在这种情况下,您不能在PdfPCell 级别设置对齐方式。当使用复合模式时,你必须在元素级别设置对齐方式(在本例中为Paragraph级别)。

    【讨论】:

    • 使用 Environment.NewLine 我不必添加单独的段落
    猜你喜欢
    • 1970-01-01
    • 2013-12-12
    • 2015-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-26
    • 2011-08-31
    • 2011-11-27
    相关资源
    最近更新 更多