Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 确定文本是否包装在段落/单元格中_.net_Vb.net_Itextsharp - Fatal编程技术网

.net 确定文本是否包装在段落/单元格中

.net 确定文本是否包装在段落/单元格中,.net,vb.net,itextsharp,.net,Vb.net,Itextsharp,我已经完全对齐了文本块(长度不同的字符串),后跟一个包含DottedLineSeparator对象的块。我需要知道的是,长度不同的字符串是否会绕到第二行(理论上它可以绕到第三行,但可能性很小) 我熟悉在Chunk对象上使用GetWidthPoint()方法(我读过),这就是我正在使用的方法;但我相信它不能100%准确地工作,因为文本有充分的理由。。。这意味着空间的实际宽度可能会有所不同 我希望能有某种方法来确定包含这两个块的段落的高度,或者我正在使用.AddElement()添加段落对象的Pdf

我已经完全对齐了文本块(长度不同的字符串),后跟一个包含DottedLineSeparator对象的块。我需要知道的是,长度不同的字符串是否会绕到第二行(理论上它可以绕到第三行,但可能性很小)

我熟悉在Chunk对象上使用GetWidthPoint()方法(我读过),这就是我正在使用的方法;但我相信它不能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)

我终于找到了一个方法。。。这是一个100%的黑客行为,我可能应该从使用表格改为使用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%的时间内有效:)

即使现在,我想如果我没有将段落和单元格水平对齐设置为Element.ALIGN\u,它可能会有效,但它不会。。。