为什么使用iTextSharp向PDF文件添加元素会导致“此页面上存在错误”

时间:2014-11-25 14:18:26

标签: vb.net pdf itextsharp

我是iTextSharp的新手。我使用iTextSharp版本4.1.6.0生成PDF报告,我在Adobe Acrobat中读取文件时遇到错误消息。错误是“此页面上存在错误.Acrobat可能无法正确显示页面。请联系创建PDF文档的人员以更正问题”。

错误发生在从左到右添加5个框(包含文本内部)的位置。添加前4个没有任何错误。如果我将最后一个框添加到文档中,则会发生错误。

使用以下代码添加5个框:

    Dim objDirectContent As PdfContentByte = pdfW.DirectContent

...

    paragraph = New Paragraph(New Chunk("Text content 1.",
                                        New Font(bodyFont, FontSize, Font.NORMAL, Color.BLACK)))
    paragraph.Leading = DefaultLeading
    columnText = New ColumnText(objDirectContent)
    columnText.AddElement(paragraph)
    DrawTextBox(objDirectContent, columnText, 0.5, yLine - contentHeight, 0.5 + width * (1 / 5), yLine, New iTextSharp.text.Color(255, 255, 255))
    paragraph = New Paragraph(New Chunk("Text content 2.",
                                        New Font(bodyFont, FontSize, Font.NORMAL, Color.BLACK)))
    paragraph.Leading = DefaultLeading
    columnText = New ColumnText(objDirectContent)
    columnText.AddElement(paragraph)
    DrawTextBox(objDirectContent, columnText, 0.5 + width * (1 / 5), yLine - contentHeight, 0.5 + width * (2 / 5), yLine, New iTextSharp.text.Color(255, 255, 255))
    paragraph = New Paragraph(New Chunk("Text content 3.",
                                        New Font(bodyFont, FontSize, Font.NORMAL, Color.BLACK)))
    paragraph.Leading = DefaultLeading
    columnText = New ColumnText(objDirectContent)
    columnText.AddElement(paragraph)
    DrawTextBox(objDirectContent, columnText, 0.5 + width * (2 / 5), yLine - contentHeight, 0.5 + width * (3 / 5), yLine, New iTextSharp.text.Color(255, 255, 255))
    paragraph = New Paragraph(New Chunk("Text content 4.",
                                        New Font(bodyFont, FontSize, Font.NORMAL, Color.BLACK)))
    paragraph.Leading = DefaultLeading
    columnText = New ColumnText(objDirectContent)
    columnText.AddElement(paragraph)
    DrawTextBox(objDirectContent, columnText, 0.5 + width * (3 / 5), yLine - contentHeight, 0.5 + width * (4 / 5), yLine, New iTextSharp.text.Color(255, 255, 255))
    paragraph = New Paragraph(New Chunk("Text content 5.",
                                        New Font(bodyFont, FontSize, Font.NORMAL, Color.BLACK)))

    paragraph.Leading = DefaultLeading
    columnText = New ColumnText(objDirectContent)
    columnText.AddElement(paragraph)

    ' BUG IS HERE!
    DrawTextBox(objDirectContent, columnText, 0.5 + width * (4 / 5), yLine - contentHeight, 0.5 + width, yLine, New iTextSharp.text.Color(255, 255, 255))

DrawTextBox:

Private Function DrawTextBox(ByRef objDirectContent As PdfContentByte, ByRef columnText As ColumnText,
            ByVal sngX1 As Single, ByVal sngY1 As Single, ByVal sngX2 As Single, ByVal sngY2 As Single,
            ByVal objFillColour As iTextSharp.text.Color) As Single

    Dim objStrokeColour As Color = New iTextSharp.text.Color(0, 0, 0)
    Dim cellPadding As Single = 0.05


    columnText.SetSimpleColumn((sngX1 + cellPadding) * PPI, (sngY1 + cellPadding) * PPI, (sngX2 - cellPadding) * PPI, (sngY2 - cellPadding) * PPI, DefaultLeading, PdfContentByte.ALIGN_LEFT)
    DrawBox(objDirectContent, sngX1, sngY1, sngX2, sngY2, objStrokeColour, objFillColour)

    columnText.Go()

    Dim yLine As Single = sngY1
    Return yLine

End Function

DrawBox:

Private Sub DrawBox(ByRef objDirectContent As PdfContentByte, _
            ByVal sngX1 As Single, ByVal sngY1 As Single, ByVal sngX2 As Single, ByVal sngY2 As Single, _
            ByVal objStrokeColour As iTextSharp.text.Color, ByVal objFillColour As iTextSharp.text.Color)


    objDirectContent.SetLineWidth(1.0)
    objDirectContent.SetColorStroke(objStrokeColour)
    objDirectContent.SetColorFill(objFillColour)

    objDirectContent.Rectangle(sngX1 * PPI, sngY1 * PPI, (sngX2 - sngX1) * PPI, (sngY2 - sngY1) * PPI)
    If objFillColour.Equals(New iTextSharp.text.Color(255, 255, 255)) Then
        objDirectContent.ClosePathStroke()
    Else
        objDirectContent.ClosePathFillStroke()
    End If
End Sub

这段代码是由一位前同事写的,所以我可能无法回答所有“你为什么这样做'类型的问题,但是如果我使用iText不合适,我会感激任何有用的评论。< / p>

如果有人对可能导致此错误的原因或如何调试它有任何想法,请告诉我。谢谢。

0 个答案:

没有答案