接收“AccessViolationException”合并PDF文件的vb.net 2010应用程序出错 [英] Receiving "AccessViolationException" Error on vb.net 2010 app that merges PDF files

查看:69
本文介绍了接收“AccessViolationException”合并PDF文件的vb.net 2010应用程序出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我正在编写一个vb.net 2010应用程序,以PDF格式生成9页报告。该应用程序使用单页PDF作为循环的资源并生成9个单独的文件,这些文件合并为单个PDF以供显示。



用户从中选择一个月一个ToolStripMenuItem和数据被拉出来。通过选择月份测试应用程序时,它正确显示九页PDF。然后我关闭了显示表单并选择了另一个月,再次正确显示。但是,当选择另一个月时,它会给出以下错误:



AccessViolationException未处理此错误发生在代码行上,其中包含:



Me.AxAcroPDF1.src = destinationFile



这总是发生在第三个月(任何序列)被选中后,任何想法?



Hello,

I am writing a vb.net 2010 app that generates a nine page report as a PDF. The app uses a single page PDF as a resource that loops through and generates 9 individual files that are merged into a single PDF for display.

The user selects a month from a ToolStripMenuItem and data is pulled for that month. When testing app by selecting month it displayed nine page PDF correctly. I then closed display form and selected another month, once again it displayed correctly. However, when another month is selected it gives the following Error:

"AccessViolationException was unhandled" this error happens on line of code that says:

Me.AxAcroPDF1.src = destinationFile

This always happens after the third month (of any sequence) is selected, any ideas?

Imports iTextSharp
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.text.xml
Imports System.IO
Public Class SysRatiosDisplay

    Private Sub SysRatiosDisplay_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        'Using one pdf template for nine page report, each page has same format. Each page will be saved as individual
        'file and merged at end to generate one pdf file with nine pages.
        Dim x As Integer = 0
        Dim pdfTemp(8) As String
        Dim newFile(8) As String
        destinationFile = IO.Path.GetTempFileName
        Dim menuTitle As String() = {"Electric Revenue and Usage", "Electric Operating Expense", _
                                   "Gas Revenue and Usage", "Gas Operating Expense", _
                                   "Water Revenue and Usage", "Water Operating Expense", _
                                   "Sewer Revenue and Usage", "Sewer Operating Expense", _
                                   "Administrative & General Operating Expense"}
        For x = 0 To 8
            'This section generates the common elements of title, run date, and page no.
            pdfTemp(x) = IO.Path.GetTempFileName
            newFile(x) = IO.Path.GetTempFileName
            My.Computer.FileSystem.WriteAllBytes(pdfTemp(x), My.Resources.RatiosExample_RevExp, False)
            Dim pdfReader As New PdfReader(pdfTemp(x))
            Dim pdfStamper As New PdfStamper(pdfReader, New FileStream(newFile(x), FileMode.Create))
            Dim pdfFormFields As AcroFields = pdfStamper.AcroFields

            pdfFormFields.SetField("Comparisons For", vbCr & "SYLACAUGA UTILITIES BOARD" & vbCr & CStr(menuTitle(x)))
            pdfFormFields.SetField("Run Date_2", CStr(Today))
            pdfFormFields.SetField("Page", CStr(x + 1))
            pdfFormFields.SetField("Text1", compYear)
            pdfFormFields.SetField("Text2", compLastYear)
            If x = 0 Then
                'This section populates first page of report with data
                pdfFormFields.SetField("Text4", "1")
                pdfFormFields.SetField("aRow2", "Residential Revenue per kWh")
                pdfFormFields.SetField("bRow2", (resdElecChg / resdKWH).ToString("c4"))
                pdfFormFields.SetField("cRow2", resdKWH.ToString("n0"))
                pdfFormFields.SetField("dRow2", resdElecChg.ToString("c0"))
                pdfFormFields.SetField("Text5", "2")
                pdfFormFields.SetField("aRow3", "Commercial Revenue per kWh")
                pdfFormFields.SetField("bRow3", (commElecChg / commKWH).ToString("c4"))
                pdfFormFields.SetField("cRow3", commKWH.ToString("n0"))
                pdfFormFields.SetField("dRow3", commElecChg.ToString("c0"))
                pdfFormFields.SetField("Text6", "3")
                pdfFormFields.SetField("aRow4", "Industrial Revenue per kWh")
                pdfFormFields.SetField("bRow4", (indElecChg / indKWH).ToString("c4"))
                pdfFormFields.SetField("cRow4", indKWH.ToString("n0"))
                pdfFormFields.SetField("dRow4", indElecChg.ToString("c0"))
                pdfFormFields.SetField("Text7", "4")
                pdfFormFields.SetField("aRow5", "St. & Traffic Light Revenue per kWh")
                pdfFormFields.SetField("bRow5", (lightsChg / lightsKWH).ToString("c4"))
                pdfFormFields.SetField("cRow5", lightsKWH.ToString("n0"))
                pdfFormFields.SetField("dRow5", lightsChg.ToString("c0"))
                pdfFormFields.SetField("Text8", "5")
                pdfFormFields.SetField("aRow6", "Total Revenue per kWh")
                pdfFormFields.SetField("bRow6", (totalElecChg / totalKWH).ToString("c4"))
                pdfFormFields.SetField("cRow6", totalKWH.ToString("n0"))
                pdfFormFields.SetField("dRow6", totalElecChg.ToString("c0"))
                pdfFormFields.SetField("Text10", "6")
                pdfFormFields.SetField("aRow8", "Residential Revenue per Customer")
                pdfFormFields.SetField("bRow8", (resdElecChg / resdElecCount).ToString("c2"))
                pdfFormFields.SetField("cRow8", resdElecCount.ToString("n0"))
                pdfFormFields.SetField("dRow8", resdElecChg.ToString("c0"))
                pdfFormFields.SetField("Text11", "7")
                pdfFormFields.SetField("aRow9", "Commercial Revenue per Customer")
                pdfFormFields.SetField("bRow9", (commElecChg / commElecCount).ToString("c2"))
                pdfFormFields.SetField("cRow9", commElecCount.ToString("n0"))
                pdfFormFields.SetField("dRow9", commElecChg.ToString("c0"))
                pdfFormFields.SetField("Text12", "8")
                pdfFormFields.SetField("aRow10", "Industrial Revenue per Customer")
                pdfFormFields.SetField("bRow10", (indElecChg / indElecCount).ToString("c2"))
                pdfFormFields.SetField("cRow10", indElecCount.ToString("n0"))
                pdfFormFields.SetField("dRow10", indElecChg.ToString("c0"))
                pdfFormFields.SetField("Text13", "9")
                pdfFormFields.SetField("aRow11", "St. & Traffic Light Revenue per Fixture")
                pdfFormFields.SetField("bRow11", (lightsChg / lightsCount).ToString("c2"))
                pdfFormFields.SetField("cRow11", lightsCount.ToString("n0"))
                pdfFormFields.SetField("dRow11", lightsChg.ToString("c0"))
                pdfFormFields.SetField("Text14", "10")
                pdfFormFields.SetField("aRow12", "Total Revenue per Customer/Fixture")
                pdfFormFields.SetField("bRow12", (totalElecChg / totalElecCount).ToString("c2"))
                pdfFormFields.SetField("cRow12", totalElecCount.ToString("n0"))
                pdfFormFields.SetField("dRow12", totalElecChg.ToString("c0"))
                pdfFormFields.SetField("Text16", "11")
                pdfFormFields.SetField("aRow14", "Residential kWh Usage per Customer")
                pdfFormFields.SetField("bRow14", (resdKWH / resdElecCount).ToString("n0"))
                pdfFormFields.SetField("cRow14", resdElecCount.ToString("n0"))
                pdfFormFields.SetField("dRow14", resdKWH.ToString("n0"))
                pdfFormFields.SetField("Text17", "12")
                pdfFormFields.SetField("aRow15", "Commercial kWh Usage per Customer")
                pdfFormFields.SetField("bRow15", (commKWH / commElecCount).ToString("n0"))
                pdfFormFields.SetField("cRow15", commElecCount.ToString("n0"))
                pdfFormFields.SetField("dRow15", resdKWH.ToString("n0"))
                pdfFormFields.SetField("Text18", "13")
                pdfFormFields.SetField("aRow16", "Industrial kWh Usage per Customer")
                pdfFormFields.SetField("bRow16", (indKWH / indElecCount).ToString("n0"))
                pdfFormFields.SetField("cRow16", indElecCount.ToString("n0"))
                pdfFormFields.SetField("dRow16", indKWH.ToString("n0"))
                pdfFormFields.SetField("Text19", "14")
                pdfFormFields.SetField("aRow17", "St. & Traffic Light Usage per Fixture")
                pdfFormFields.SetField("bRow17", (lightsKWH / lightsCount).ToString("n0"))
                pdfFormFields.SetField("cRow17", lightsCount.ToString("n0"))
                pdfFormFields.SetField("dRow17", lightsKWH.ToString("n0"))
                pdfFormFields.SetField("Text20", "15")
                pdfFormFields.SetField("aRow18", "Total kWh Usage per Customer/Fixture")
                pdfFormFields.SetField("bRow18", (totalKWH / totalElecCount).ToString("n0"))
                pdfFormFields.SetField("cRow18", totalElecCount.ToString("n0"))
                pdfFormFields.SetField("dRow18", totalKWH.ToString("n0"))
                pdfFormFields.SetField("Text22", "16")
                pdfFormFields.SetField("aRow20", "Net Revenue per kWh")
                pdfFormFields.SetField("Text23", "17")
                pdfFormFields.SetField("aRow21", "Net Revenue per Customer")
                pdfFormFields.SetField("Text25", "18")
                pdfFormFields.SetField("aRow23", "Cost per Btu")
                pdfFormFields.SetField("bRow23", ((totalElecChg / totalKWH) / 3412.9).ToString("c8"))
                pdfFormFields.SetField("Text27", "19")
                pdfFormFields.SetField("aRow25", "Current Net Versus Prior Net")

            End If
            pdfStamper.FormFlattening = True
            pdfStamper.Close()
        Next

        MergeFiles(destinationFile, newFile)
        Me.AxAcroPDF1.src = destinationFile
    End Sub
    Public Shared Sub MergeFiles(destinationFile As String, sourceFiles As String())
        Try
            Dim f As Integer = 0
            Dim reader As New PdfReader(sourceFiles(f))
            Dim n As Integer = reader.NumberOfPages
            Dim document As New Document(reader.GetPageSizeWithRotation(1))
            Dim writer As PdfWriter = PdfWriter.GetInstance(document, New FileStream(destinationFile, FileMode.Create))
            document.Open()
            Dim cb As PdfContentByte = writer.DirectContent
            Dim page As PdfImportedPage
            Dim rotation As Integer
            While f < sourceFiles.Length
                Dim i As Integer = 0
                While i < n
                    i += 1
                    document.SetPageSize(reader.GetPageSizeWithRotation(i))
                    document.NewPage()
                    page = writer.GetImportedPage(reader, i)
                    rotation = reader.GetPageRotation(i)
                    If rotation = 90 OrElse rotation = 270 Then
                        cb.AddTemplate(page, 0, -1.0F, 1.0F, 0, 0, reader.GetPageSizeWithRotation(i).Height)
                    Else
                        cb.AddTemplate(page, 1.0F, 0, 0, 1.0F, 0, 0)

                    End If
                End While
                f += 1
                If f < sourceFiles.Length Then
                    reader = New PdfReader(sourceFiles(f))
                    n = reader.NumberOfPages
                End If
            End While

            document.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)

        End Try

    End Sub
End Class

推荐答案

在MergeFiles函数中, Dispose 实现IDisposabl的所有项目e(调用具有它的所有对象的Dispose函数),可能是读者,文档,编写者。

为什么该函数是公共的和共享的?私人应该做;应该避免共享。
In the MergeFiles function, Dispose of all items which implement IDisposable (call the Dispose function of all objects which have it), likely reader, document, writer.
Why is that function Public and Shared? Private should do; Shared should be avoided.


这篇关于接收“AccessViolationException”合并PDF文件的vb.net 2010应用程序出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆