ColumnText.ShowTextAligned()在换行后切断文本 [英] ColumnText.ShowTextAligned() cuts off text after linebreak

查看:2976
本文介绍了ColumnText.ShowTextAligned()在换行后切断文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码使用iTextSharp将文本打印到PDF文档:

I have the following code to print a text onto a PDF document using iTextSharp:

canvas = stamper.GetOverContent(i)
watermarkFont = iTextSharp.text.pdf.BaseFont.CreateFont(iTextSharp.text.pdf.BaseFont.HELVETICA, iTextSharp.text.pdf.BaseFont.CP1252, iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED)
watermarkFontColor = iTextSharp.text.BaseColor.RED
canvas.SetFontAndSize(watermarkFont, 11)
canvas.SetColorFill(watermarkFontColor)

Dim sText As String = "Line1" & vbCrLf & "Line2"
Dim nPhrase As New Phrase(sText)

ColumnText.ShowTextAligned(canvas, Element.ALIGN_TOP, nPhrase, 0, 50, 0)

但是,只打印第一行(Line1),第二行(Line2)不打印。

However, only the first line ("Line1") is printed, the second line ("Line2") isn't.

我是否必须通过任何标志才能生效?

Do I have to pass any flags to make that work?

推荐答案

ShowTextAligned()方法只能用于绘制单行。如果要绘制两行,则有两个选项:

As documented, the ShowTextAligned() method can only be used to draw a single line. If you want to draw two lines, you have two options:

选项1:使用该方法两次:

ColumnText.ShowTextAligned(canvas, Element.ALIGN_TOP, new Phrase("line 1"), 0, 50, 0)
ColumnText.ShowTextAligned(canvas, Element.ALIGN_TOP, new Phrase("line 2"), 0, 25, 0)

选项2:使用 ColumnText 以不同的方式:

ColumnText ct = new ColumnText(canvas);
ct.SetSimpleColumn(rect);
ct.AddElement(new Paragraph("line 1"));
ct.AddElement(new Paragraph("line 2"));
ct.Go();

在此代码段中, rect 是键入矩形。它定义了要添加文本的区域。请参阅如何使用itextsharp在PdfContentByte矩形中添加文本?​​

In this code snippet, rect is of type Rectangle. It defines the area where you want to add the text. See How to add text in PdfContentByte rectangle using itextsharp?

这篇关于ColumnText.ShowTextAligned()在换行后切断文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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