Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?

VB
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
Posted
Updated 24-Jul-14 8:02am
v2

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900