Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on a menu strip Print Preview event. i worked out the following procedure to view a pdf file located in the same path as my workbook.

    Private WithEvents docPrint As New PrintDocument()
    ' Declare a string to hold the entire document contents. 
    ' Declare a string to hold the entire document contents. 
    Private documentContents As String

    ' Declare a variable to hold the portion of the document that 
    ' is not printed. 
    Private stringToPrint As String

Private Sub ReadDocument()

        Dim xlWBPath As String = Globals.ThisWorkbook.Application.ActiveWorkbook.Path
        Dim docName As String = xlWBPath & "\" & "Custom Ranges.pdf"

        docPrint.DocumentName = docName

        Dim stream As New FileStream(docName, FileMode.Open)
        Try
            Dim reader As New StreamReader(stream)
            Try
                documentContents = reader.ReadToEnd()
            Finally
                reader.Dispose()
            End Try
        Finally
            stream.Dispose()
        End Try
        stringToPrint = documentContents

    End Sub

    Private Sub docPrint_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles docPrint.PrintPage

        Dim charactersOnPage As Integer = 0
        Dim linesPerPage As Integer = 0

        ' Sets the value of charactersOnPage to the number of characters  
        ' of stringToPrint that will fit within the bounds of the page.
        e.Graphics.MeasureString(stringToPrint, Me.Font, e.MarginBounds.Size, _
            StringFormat.GenericTypographic, charactersOnPage, linesPerPage)

        ' Draws the string within the bounds of the page.
        e.Graphics.DrawString(stringToPrint, Me.Font, Brushes.Black, _
            e.MarginBounds, StringFormat.GenericTypographic)

        ' Remove the portion of the string that has been printed.
        stringToPrint = stringToPrint.Substring(charactersOnPage)

        ' Check to see if more pages are to be printed.
        e.HasMorePages = stringToPrint.Length > 0

        ' If there are no more pages, reset the string to be printed. 
        If Not e.HasMorePages Then
            stringToPrint = documentContents
        End If


    End Sub


    Private Sub prnPrvStripCustomRange_Click(sender As Object, e As EventArgs) Handles prnPrvStripCustomRanges.Click

        ReadDocument()
        prnPrvCustomRanges.Document = docPrint
        prnPrvCustomRanges.WindowState = FormWindowState.Maximized
        prnPrvCustomRanges.ShowDialog()

    End Sub


My preview window pops up but my file does not show up on my window. I think my problem is in the PrintPage event, but I can't tell what is going on.
Posted

1 solution

I'm not sure but it looks like you are reading the document then disposing of it and not actually doing anything with it before then.
make sure "documentContents" contains something by the time it get to print.

You may try to move this line up
VB
stringToPrint = documentContents

under
VB
documentContents = reader.ReadToEnd()

and see if that makes a difference

Upadte:
I tried testing your code and I am coming up with blank pages also.
My changes di not make a difference.
Another strange thing is the way it gets the data from the pdf file.
Try viewing the data in a text box and see what that shows.

The Data I was showing was not the normal text like a text document but all of the hidden info and text that goes into a PDF document.
you may need to look into something like.

Converting PDF to Text in C#[^]

or something similar depending what it is your trying to do.

Edit 2:
After some more Testing I added the PDF Viewer to the toolbox.
Got the Idea after viewing several question on how to do it.

Choose Items > com components > Adobe PDF Reader > ok
Next you now have that item in your tool box.

Create a new form and drop the control onto the form and then you can mess with dock or however you want it to to be in the form.

next set up on form one the path to the file you want it to open then open the PDF form passing the path. Then in the on load or a button click (of the pdf form) set:
AxAcroPDF1.src = {file path}

As long as it finds it the pfd loads in that Control.

******* Note *****
The thing about this is you Have to set your build type to X86 or you will get a comm error when trying to run it on a 64 bit system.

once the pdf loads, options to print are there.

As long as Acrobat reader is installed on a system this should work.

Hope this helps, now I know how to do it :)
 
Share this answer
 
v3
Comments
qbndl8 10-Aug-13 21:55pm    
Thanks, unfortunately it did not work.
ledtech3 11-Aug-13 10:36am    
Another note.
After building the last version it automaticly copies 2 of the Adobe dll's to the output folder, so reader may not need to be installed as long as these are with it.
I havent tested that on a system that does not have reader.
qbndl8 11-Aug-13 11:01am    
Thank you, I will test it and see what happens
ledtech3 11-Aug-13 11:15am    
let me know if you need my sample project,built in VS 2008 and I can email it to you or post it to my skydrive.
qbndl8 11-Aug-13 18:31pm    
Thank you, if you can email it that would be great.

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