如何在iTextSharp中使用非中断空格 [英] How to use non breaking space in iTextSharp

查看:132
本文介绍了如何在iTextSharp中使用非中断空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将非中断空间用于在PdfPTable单元格中具有多行内容。 iTextSharp用空格字符分解单词。

How can the non breaking space can be used to have a multiline content in a PdfPTable cell. iTextSharp is breaking down the words with the space characters.

场景是我想要一个表头中的多行内容,例如在第一行它可能会显示Text1& ;在第二行显示文本,在渲染PDF时,Text1显示在第一行,然后显示在第二行&显示,在第三行,它取第一行的长度,并将剩余的字符截断到下一行。

The scenario is I want a multiline content in a table head, such as in first line it may display "Text1 &" and on second line it would display "Text", on rendering the PDF the Text1 is displayed in first line, then on second line & is displayed and on third it takes the length of the first line and truncates the remaining characters to the next line.

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

Or can I set specific width for each and every column of the table so as to accomodate text content within it, such as the text would wrap within that specific width.

推荐答案

你没有' t指定一种语言,所以我将在VB.Net中回答,但如果需要,你可以很容易地将它转换为C#。

You didn't specify a language so I'll answer in VB.Net but you can easily convert it to C# if needed.

对于你的第一个问题,使用非破坏性空间只需使用适当的Unicode代码点 U + 00A0

To your first question, to use a non-breaking space just use the appropriate Unicode code point U+00A0:

在VB.Net中,您声明如下:

In VB.Net you'd declare it like:

Dim NBSP As Char = ChrW(&HA0)

在C#中:

Char NBSP = '\u00a0';

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

Then you can just concatenate it where needed:

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

您可能还会找到非破解连字符(U + 2011)也很有帮助。

You might also find the non-breaking hyphen (U+2011) helpful, too.

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

To your second question, yes you can set the width of every column. However, column widths are always set as relative widths so if you use:

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

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

What you are actually saying is that for the given table, the first column should be twice as large as the second column, you are NOT saying that the first column is 2px wide and the second is 1px. This is very important to understand. The above code is the exact same as the next two lines:

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

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

The column widths are relative to the table's width which by default (if I remember correctly) is 80% of the writable page's width. If you would like to fix the table's width to an absolute width you need to set two properties:

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

综上所述,下面是一个完整的WinForms应用程序,目标是iTextSharp 5.1.1.0:

Putting the above all together, below is a full working WinForms app targetting iTextSharp 5.1.1.0:

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中使用非中断空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆