【问题标题】:How to use non breaking space in iTextSharp如何在 iTextSharp 中使用不间断空格
【发布时间】:2012-03-26 03:52:09
【问题描述】:

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

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

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

【问题讨论】:

    标签: itextsharp whitespace


    【解决方案1】:

    您没有指定语言,所以我将在 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
    

    【讨论】:

    • 感谢您的回复。我使用的语言是 C#,仔细查看代码就可以知道要做什么。再次感谢。
    • 字符 NBSP = '\u00A0';在 iTextSharp 中为我打印出 。在 atm 中找不到修复程序。
    • 其实这似乎是因为我没有使用unicode编码。但是我将编码更改为 UTF-8 和我的  和  在工作,在忙。无论如何 +1。
    猜你喜欢
    • 2011-12-31
    • 1970-01-01
    • 2018-05-10
    • 2016-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多