使用 ItextSharp 我得到不平衡的开始/结束文本运算符。但他们是相配的

发布于 2024-12-07 16:48:35 字数 2458 浏览 0 评论 0原文

我正在开发一个应用程序,它使用 ItextSharp 生成 PDF 文件,供学生打印姓名标签和停车许可证...但是它不断抛出:不平衡的开始/结束文本运算符。在 doc.close() 处 这些块似乎已正确打开和关闭。以下是该功能:

        Function ID_and_Parking(ByVal id As Integer) As ActionResult
        Dim _reg_info As reg_info = db.reg_info.Single(Function(r) r.id = id)
        Dim _conf_info As conf_info = db.conf_info.Single(Function(f) f.id = 0)
        Dim _LastName As String = _reg_info.last_name
        Dim _Employer As String = _reg_info.business_name
        Dim _Class_1 As String = _reg_info.tues_class
        Dim _Class_2 As String = _reg_info.wed_class
        Dim _Class_3 As String = _reg_info.thur_class
        Dim _Class_4 As String = _reg_info.fri_class
        Dim _BeginDate As String = _conf_info.conf_start_date
        Dim _endDate As String = _conf_info.conf_end_date
        If IsDBNull(_reg_info.tues_class) Then
            _Class_1 = ""
        End If
        If IsDBNull(_reg_info.wed_class) Then
            _Class_2 = ""
        End If
        If IsDBNull(_reg_info.thur_class) Then
            _Class_3 = ""
        End If
        If IsDBNull(_reg_info.fri_class) Then
            _Class_4 = ""
        End If

        'Dim pdfpath As String = Server.MapPath("PDFs")
        'Dim imagepath As String = Server.MapPath("Images")
        Dim pdfpath As String = "C:\temp\"
        Dim imagepath As String = "C:\temp\"
        Dim doc As New Document
        doc.SetPageSize(iTextSharp.text.PageSize.A4)
        doc.SetMargins(0, 0, 2, 2)

        Try
            Dim writer As PdfWriter = PdfWriter.GetInstance(doc, New FileStream(pdfpath + "/Images.pdf", FileMode.Create))
            doc.Open()
            Dim cb As PdfContentByte = writer.DirectContent
            cb.BeginText()
            cb.SetTextMatrix(100, 400)
            cb.ShowText(_LastName)
            cb.EndText()
            doc.Add(New Paragraph("JPG"))
            Dim jpg As Image = Image.GetInstance(imagepath + "/Asads_Tags.jpg")
            jpg.Alignment = iTextSharp.text.Image.UNDERLYING
            jpg.ScaleToFit(576, 756)
            doc.Add(jpg)
        Catch dex As DocumentException
            Response.Write(dex.Message)
        Catch ioex As IOException
            Response.Write(ioex.Message)
        Catch ex As Exception
            Response.Write(ex.Message)
        Finally
            doc.Close()

        End Try



        Return RedirectToAction("Index")


    End Function

任何人都知道我可能在哪里出错??????

I working on a app that uses ItextSharp to generate PDF files for students to print name tags and parking permits... However it keeps throwing: Unbalanced begin/end text operators. at the doc.close()
The blocks appear to be properly openned and closed.. Below is the function:

        Function ID_and_Parking(ByVal id As Integer) As ActionResult
        Dim _reg_info As reg_info = db.reg_info.Single(Function(r) r.id = id)
        Dim _conf_info As conf_info = db.conf_info.Single(Function(f) f.id = 0)
        Dim _LastName As String = _reg_info.last_name
        Dim _Employer As String = _reg_info.business_name
        Dim _Class_1 As String = _reg_info.tues_class
        Dim _Class_2 As String = _reg_info.wed_class
        Dim _Class_3 As String = _reg_info.thur_class
        Dim _Class_4 As String = _reg_info.fri_class
        Dim _BeginDate As String = _conf_info.conf_start_date
        Dim _endDate As String = _conf_info.conf_end_date
        If IsDBNull(_reg_info.tues_class) Then
            _Class_1 = ""
        End If
        If IsDBNull(_reg_info.wed_class) Then
            _Class_2 = ""
        End If
        If IsDBNull(_reg_info.thur_class) Then
            _Class_3 = ""
        End If
        If IsDBNull(_reg_info.fri_class) Then
            _Class_4 = ""
        End If

        'Dim pdfpath As String = Server.MapPath("PDFs")
        'Dim imagepath As String = Server.MapPath("Images")
        Dim pdfpath As String = "C:\temp\"
        Dim imagepath As String = "C:\temp\"
        Dim doc As New Document
        doc.SetPageSize(iTextSharp.text.PageSize.A4)
        doc.SetMargins(0, 0, 2, 2)

        Try
            Dim writer As PdfWriter = PdfWriter.GetInstance(doc, New FileStream(pdfpath + "/Images.pdf", FileMode.Create))
            doc.Open()
            Dim cb As PdfContentByte = writer.DirectContent
            cb.BeginText()
            cb.SetTextMatrix(100, 400)
            cb.ShowText(_LastName)
            cb.EndText()
            doc.Add(New Paragraph("JPG"))
            Dim jpg As Image = Image.GetInstance(imagepath + "/Asads_Tags.jpg")
            jpg.Alignment = iTextSharp.text.Image.UNDERLYING
            jpg.ScaleToFit(576, 756)
            doc.Add(jpg)
        Catch dex As DocumentException
            Response.Write(dex.Message)
        Catch ioex As IOException
            Response.Write(ioex.Message)
        Catch ex As Exception
            Response.Write(ex.Message)
        Finally
            doc.Close()

        End Try



        Return RedirectToAction("Index")


    End Function

Anyone know where I could be going wrong at??????

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴梦一场 2024-12-14 16:48:35

Try Catch 块是导致不平衡的开始/结束异常的原因......一旦我将 doc.close() 移至 try 的底部,错误就消失了......哦,好吧,也许其他人需要洞察力。 .. –

The Try Catch block is what caused the unbalanced start/end exception.....Once I moved the doc.close() up to the bottom of try the error went away... Oh well maybe someone else will need the insight... –

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文