【发布时间】:2013-12-03 16:29:22
【问题描述】:
我有完全对齐的文本块(不同长度的字符串),后跟一个包含 DottedLineSeparator 对象的块。我需要知道的是,不同长度的字符串是否换行到第二行(理论上它可以换行到第三行,但可能性很小)。
我熟悉在 Chunk 对象上使用 GetWidthPoint() 方法(我读过 this question),这就是我正在使用的;但由于文本的完全对齐,我认为它不能以 100% 的准确度工作......这意味着空格的实际宽度可能会有所不同。
我希望有某种方法可以确定包含这两个块的段落的高度,或者我使用 .AddElement() 来添加段落对象的 PdfPCell 对象,甚至就此而言我将 PdfPCell 对象添加到的 PdfPTable 对象。我知道我可以在将 PdfPTable 对象写入文档(使用 .TotalHeight)之后获得表格的高度。这很令人困惑,因为我可以调用 cell.GetMaxHeight() 但它总是返回 8.75...无论文本是否足够长以换行!
不幸的是,在为剩余的单元格生成内容之前,我需要知道第一个单元格的高度是多少——这是两难的选择。
有什么想法/指针/指导吗?
这里有一些代码来说明:
Dim c As PdfPCell
Dim t As PdfPTable
Dim p As Paragraph
Dim textWrapsAround As Boolean = False
Dim amendedTextWrapsAround As Boolean = False
t = New PdfPTable(3)
t.SetWidths({40, 12, 12})
t.WidthPercentage = 95.0F
t.HorizontalAlignment = Element.ALIGN_CENTER
p = New Paragraph("", DEFAULTFONT)
p.SetLeading(0.5F, 1.0F)
c = New PdfPCell() With {.Border = 0}
c.MinimumHeight = PdfDocument.LineHeightPts
p.Add(New Chunk(name.Trim(), DEFAULTFONT))
p.Add(New Chunk(leaderLine))
p.Alignment = Element.ALIGN_JUSTIFIED
If p.Chunks(0).GetWidthPoint >= 216 Then textWrapsAround = True
c.AddElement(p)
c.HorizontalAlignment = Element.ALIGN_JUSTIFIED
c.PaddingTop = 0
t.SetExtendLastRow(True, False)
t.AddCell(c)
c = emitPdfColumnCell(col1, lFormat, textWrapsAround)
c.MinimumHeight = PdfDocument.LineHeightPts
c.HorizontalAlignment = Element.ALIGN_RIGHT
c.PaddingTop = If(textWrapsAround AndAlso col2.Contains(vbCrLf), top_padding, 0)
t.SetExtendLastRow(True, False)
t.AddCell(c)
c = emitPdfColumnCell(col2, lFormat, textWrapsAround)
c.MinimumHeight = PdfDocument.LineHeightPts
c.HorizontalAlignment = Element.ALIGN_RIGHT
c.PaddingTop = If(textWrapsAround AndAlso col2.Contains(vbCrLf), top_padding, 0)
t.SetExtendLastRow(True, False)
t.AddCell(c)
t.KeepTogether = True
pdf_doc.Add(t)
【问题讨论】:
-
即使现在我也想,如果我没有将段落和单元格 HorizontalAlignment 设置为 Element.ALIGN_JUSTIFIED,它可能会起作用,但它不会...
标签: .net vb.net itextsharp