【问题标题】:identifying if text wrapped in a paragraph/cell识别是否包含在段落/单元格中的文本
【发布时间】: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)

【问题讨论】:

  • 即使现在我也想,如果我没有将段落和单元格 Horizo​​ntalAlignment 设置为 Element.ALIGN_JUSTIFIED,它可能会起作用,但它不会...

标签: .net vb.net itextsharp


【解决方案1】:

我终于找到了一种方法...这是 100% 的 hack,我可能应该从使用 Table 切换到 ColumnText,因为这就是我确定多少行的方式但是...这是解决方案:

'returns # of lines 
Private Function CheckParagraphTextWrap(ByVal p As Paragraph) As Integer

    Dim ct As New ColumnText(Me.pdf_writer.DirectContent)
    ct.SetIndent(2.0F, True)
    ct.RightIndent = 1.5F
    Dim show As Boolean = False
    Dim x1, x2, y1, y2 As Double
    Dim phrase As New Phrase()
    phrase.AddAll(p.Chunks)
    'hard-coded to match the EXACT width of the cell... this is essential and
    x1 = 104.25 : y1 = 580 : x2 = 320.72 : y2 = 620
    ct.SetSimpleColumn(phrase, x1, y1, x2, y2, 1.0F, Element.ALIGN_JUSTIFIED)
    ct.SetLeading(0.5F, 1.0F) 'also has to match what was being done for the cell
    ct.Go(Not show) 'simulate mode
    Return ct.LinesWritten

End Function

我为每个添加到表格中的单元格的段落调用此函数;这是必需的,因为我必须在此之后调整表格中的其他单元格。笨重,但它(至少到目前为止)100% 的时间都有效。我之前的方法只有大约 95% 的时间有效:)

【讨论】:

    猜你喜欢
    • 2020-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-21
    • 2019-11-27
    • 1970-01-01
    • 2023-04-08
    • 2021-12-25
    相关资源
    最近更新 更多