AxAcroPdf Control - Issues reloading PDF

3.4k views Asked by At

When using the axAcroPdfLib.AxAcroPDF control in my Windows Forms application, I'm not able to reload the same image. The image is initially loaded with the LoadFile() method.

  1. Upon using the LoadFile() method again on the same path AFTER saving changes to the PDF, the control becomes blank (no PDF shown).
  2. If I set the src property of the control to the path, I get a message saying the file does not begin with '%PDF-'. But it does. I opened it with Word and it clearly begins with %PDF-. It's not corrupt or locked either.
  3. I've even tried closing, disposing, or setting it to Nothing, and then completely re-instantiating it as I did the first time it's loaded - no effect. The window closes and shows with the control blank.
  4. Loading a different file via the above methods has the same effect - blank.

Using Windows 7 64-bit, VS 2010, VB.NET.

The code is below. For right now, I'm just trying to draw a simple line on it.

Private Sub _btnBarCode_Click(ByVal sender As Object, ByVal e As EventArgs) Handles _btnBarCode.Click
    Dim pdfReader As iTextSharp.text.pdf.PdfReader = Nothing

    Try
        pdfReader = New iTextSharp.text.pdf.PdfReader(File.ReadAllBytes(_path))

        Using fs As New FileStream(_path, FileMode.Create, FileAccess.Write)
            Using pdfStamper = New iTextSharp.text.pdf.PdfStamper(pdfReader, fs)
                Dim pdfPage As iTextSharp.text.pdf.PdfContentByte = pdfStamper.GetOverContent(1)

                Using barCodeForm As New FBarCode
                    barCodeForm.Barcode = _barCode
                    If (barCodeForm.ShowDialog(Me) = DialogResult.OK) Then

                        Dim screenBarCode As Point = barCodeForm.Location
                        Dim clientBarCode As Point = Point.op_Subtraction(PointToClient(screenBarCode), New Point(0, 50)) '_pdfControl.Location '_imgView.Location
                        clientBarCode = New Point(10, 50)

                        Dim barcodeImg As New Bitmap(200, 50)
                        Using gc As Graphics = Graphics.FromImage(barcodeImg)
                            gc.DrawLine(Pens.Red, New Point(10, 10), New Point(20, 20))
                            'barCodeForm._barCode.DrawBarCode(gc, clientBarCode)
                        End Using

                        Dim convert As ImageConverter = New ImageConverter()
                        Dim bmpBytes As Byte() = DirectCast(convert.ConvertTo(barcodeImg, GetType(Byte())), Byte())
                        Dim thisImage As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(bmpBytes)
                        thisImage.SetAbsolutePosition(clientBarCode.X, clientBarCode.Y)
                        thisImage.SetDpi(72, 72)

                        pdfPage.AddImage(thisImage)

                        rdrAdobePdf.LoadFile(_path) 'Blank pdf
                        'rdrAdobePdf.src = _path '"Does not begin with '%PDF-' (even though it does)

                        'Me.Close()
                        '_myParent.ResetPdfViewer()
                        'ReloadPdfViewer(Me.Barcode)

                    End If
                End Using
            End Using
        End Using

    Catch ex As Exception
        MessageBox.Show(ex.Message, "An error occurred.")
        Return
    Finally
        If Not pdfReader Is Nothing Then pdfReader.Close()
    End Try

End Sub

Any ideas what could be the problem here?

1

There are 1 answers

4
Paul On BEST ANSWER

Problem was solved by getting the reloading code out of that click method. Putting it in another click method solved the problem - not sure why. The code I used just ran .LoadFile, then Form.Show() and Form.Activate().