PDFPrinting problem

2 posts, 0 answers
  1. Jeffrey
    Jeffrey avatar
    21 posts
    Member since:
    Mar 2014

    Posted 18 Jun 2014 Link to this post

    When I run the following code I get the attached error. If I comment out this line the PDF does load and display correctly so I know that is now the issue. It can also be printed if I click the print button on the navigator. I want to be able to send it print automatically (either via print preview or directly with print). Not sure why this error is occurring.

    Imports iTextSharp.text.pdf
    Imports System.IO
    Imports iTextSharp.text
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim document As New Document()
            Dim pdfStream As New MemoryStream()
            Dim pdfwriter As PdfWriter = pdfwriter.GetInstance(document, pdfStream)
            Dim over As PdfContentByte = pdfwriter.DirectContent
            'Add logo.
            'Dim imagelog As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(My.Resources.manalivelogo, System.Drawing.Imaging.ImageFormat.Jpeg)
            'imagelog.Transparency = New Integer() {255, 255}
            'imagelog.SetAbsolutePosition(25, 800)
            document.Add(New iTextSharp.text.Phrase("More sample text for signature demo"))
            Dim tr As BaseFont = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.WINANSI, BaseFont.EMBEDDED)
            Dim btr As BaseFont = BaseFont.CreateFont(BaseFont.TIMES_BOLD, BaseFont.WINANSI, BaseFont.EMBEDDED)
            over.SetFontAndSize(btr, 12)
            over.ShowTextAligned(iTextSharp.text.Element.ALIGN_MIDDLE, "2117 Maryland Avenue, Baltimore, MD 21218", 45, 780, 0)
            over.ShowTextAligned(iTextSharp.text.Element.ALIGN_MIDDLE, "Phone: 410-837-4892 Fax: 410-837-0639", 55, 768, 0)
            pdfwriter.CloseStream = False
            pdfStream.Position = 0
        End Sub

  2. Dimitar
    Dimitar avatar
    2960 posts

    Posted 20 Jun 2014 Link to this post

    Hi Jeffrey,

    Thank you for writing back.

    The reason for this exception is that at the point where the PrintPreview is called the document is still not created and loaded. If you want to show the print preview dialog right after the document is loaded you can use the DocumentLoaded event:
    Public Sub New()
            AddHandler radPdfViewer1.DocumentLoaded, AddressOf radPdfViewer1_DocumentLoaded
    End Sub
    Private Sub radPdfViewer1_DocumentLoaded(sender As Object, e As EventArgs)
    End Sub

    Please let me know if there is something else I can help you with. 
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
Back to Top