itextsharp - 如何在 iTextSharp 中使用不间断空格

标签 itextsharp whitespace

不间断空格如何用于在 PdfPTable 单元格中包含多行内容。 iTextSharp 正在用空格字符分解单词。

场景是我想要在表头中显示多行内容,例如在第一行可能显示“Text1 &”,在第二行显示“Text”,在呈现 PDF 时,Text1 显示在第一行,然后在第二行显示 &,在第三行显示第一行的长度并将剩余字符截断到下一行。

或者我可以为表格的每一列设置特定的宽度,以便在其中容纳文本内容,例如文本将在该特定宽度内换行。

最佳答案

您没有指定语言,所以我将在 VB.Net 中回答,但如果需要,您可以轻松地将其转换为 C#。

对于您的第一个问题,要使用不间断空格,只需使用适当的 Unicode 代码点 U+00A0 :

在 VB.Net 中,您可以这样声明它:

Dim NBSP As Char = ChrW(&HA0)

在 C# 中:

Char NBSP = '\u00a0';

然后你可以在需要的地方连接它:

Dim Text2 As String = "This is" & NBSP & "also" & NBSP & "a test"

您可能还会找到 non-breaking hyphen (U+2011)也很有帮助。

对于第二个问题,是的,您可以设置每列的宽度。但是,列宽始终设置为相对宽度,因此如果您使用:

T.SetTotalWidth(New Single() {2.0F, 1.0F})

你实际上是说对于给定的表格,第一列应该是第二列的两倍,不是说第一列是 2px 宽并且第二个是 1px。理解这一点非常重要。上面的代码与接下来的两行完全相同:

T.SetTotalWidth(New Single() {4.0F, 2.0F})
T.SetTotalWidth(New Single() {100.0F, 50.0F})

列宽是相对于表格宽度的,默认情况下(如果我没记错的话)是可写页面宽度的 80%。如果您想将表格的宽度固定为绝对宽度,您需要设置两个属性:

''//Set the width
T.TotalWidth = 200.0F
''//Lock it from trying to expand
T.LockedWidth = True

将以上所有内容放在一起,下面是一个针对 iTextSharp 5.1.1.0 的完整工作 WinForms 应用程序:

Option Explicit On
Option Strict On

Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.pdf

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        ''//File that we will create
        Dim OutputFile As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "TableTest.pdf")

        ''//Standard PDF init
        Using FS As New FileStream(OutputFile, FileMode.Create, FileAccess.Write, FileShare.None)
            Using Doc As New Document(PageSize.LETTER)
                Using writer = PdfWriter.GetInstance(Doc, FS)
                    Doc.Open()

                    ''//Create our table with two columns
                    Dim T As New PdfPTable(2)
                    ''//Set the relative widths of each column
                    T.SetTotalWidth(New Single() {2.0F, 1.0F})
                    ''//Set the table width
                    T.TotalWidth = 200.0F
                    ''//Lock the table from trying to expand
                    T.LockedWidth = True

                    ''//Our non-breaking space character
                    Dim NBSP As Char = ChrW(&HA0)

                    ''//Normal string
                    Dim Text1 As String = "This is a test"
                    ''//String with some non-breaking spaces
                    Dim Text2 As String = "This is" & NBSP & "also" & NBSP & "a test"

                    ''//Add the text to the table
                    T.AddCell(Text1)
                    T.AddCell(Text2)

                    ''//Add the table to the document
                    Doc.Add(T)

                    Doc.Close()
                End Using
            End Using
        End Using

        Me.Close()
    End Sub
End Class

关于itextsharp - 如何在 iTextSharp 中使用不间断空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8132211/

相关文章:

forms - 使用 ITextSharp 创建可填写的 PDF 表单

css - Safari 中的 "Unexpected CSS token: {"和 div 之间无法解释的差距

java - 如何用末尾的空格替换字符串中的最后一个字符

itextsharp - 图表和图形

c# - 当我有 anchor 标记时 itextsharp "the document has no pages"错误

c# - 如何在 iTextSharp 中将 PDF 转换为文本文件

c# - MVC3将多个pdf作为zip文件返回

eclipse - 如何在 Eclipse 中显示尾随空格?

html - 在 css 行之间包含不同的间距

php - 如何使用PHP或regex在每行的开头和结尾处修剪空白