This is a migrated thread and some comments may be shown as answers.

PDFPrinting problem

1 Answer 85 Views
PdfViewer and PdfViewerNavigator
This is a migrated thread and some comments may be shown as answers.
Jeffrey
Top achievements
Rank 1
Jeffrey asked on 18 Jun 2014, 02:19 PM
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)
 
        document.Open()
 
        Dim over As PdfContentByte = pdfwriter.DirectContent
        over.BeginText()
 
        'Add logo.
        'Dim imagelog As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(My.Resources.manalivelogo, System.Drawing.Imaging.ImageFormat.Jpeg)
        'imagelog.ScalePercent(20)
        'imagelog.Transparency = New Integer() {255, 255}
        'imagelog.SetAbsolutePosition(25, 800)
        'over.AddImage(imagelog)
 
        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)
 
        over.EndText()
        pdfwriter.CloseStream = False
        document.Close()
        pdfStream.Position = 0
 
        RadPdfViewer1.LoadDocument(pdfStream)
        RadPdfViewer1.PrintPreview()
 
    End Sub


1 Answer, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 20 Jun 2014, 10:53 AM
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()
    InitializeComponent()
 
        AddHandler radPdfViewer1.DocumentLoaded, AddressOf radPdfViewer1_DocumentLoaded
    radPdfViewer1.LoadDocument("C:\test1.pdf")
 
End Sub
 
Private Sub radPdfViewer1_DocumentLoaded(sender As Object, e As EventArgs)
    radPdfViewer1.PrintPreview()
End Sub

Please let me know if there is something else I can help you with. 
 
Regards,
Dimitar
Telerik
 
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.
 
Tags
PdfViewer and PdfViewerNavigator
Asked by
Jeffrey
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Share this question
or