How to use non breaking space in iTextSharp

2019-07-07 02:56发布

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.

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.

1条回答
老娘就宠你
2楼-- · 2019-07-07 03:45

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

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

In VB.Net you'd declare it like:

Dim NBSP As Char = ChrW(&HA0)

And in C#:

Char NBSP = '\u00a0';

Then you can just concatenate it where needed:

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

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

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

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

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
查看更多
登录 后发表回答