从vb.net打印预览pdf [英] Print Preview pdf from vb.net

查看:130
本文介绍了从vb.net打印预览pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理菜单条Print Preview事件。我按照以下步骤查看了与我的工作簿位于同一路径的pdf文件。



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





我的预览窗口弹出但我的文件没有显示在我的窗户上。我认为我的问题出在PrintPage事件中,但我不知道发生了什么。



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.

推荐答案

我不确定但看起来你正在阅读该文件然后处理它,然后在它之前没有用它做任何事情。

确保documentContents在打印时包含一些东西。



您可以尝试移动此行

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
stringToPrint = documentContents






under

documentContents = reader.ReadToEnd()



并查看是否有所作为



Upadte:

我试过测试你的代码,我也想出了空白页。

我的改变没有什么区别。

另一个奇怪的是它从pdf文件中获取数据的方式。

尝试在文​​本框中查看数据并查看显示的内容。



我显示的数据不像普通文本那样文本文档,但隐藏在PDF文档中的所有隐藏信息和文本。

您可能需要查看类似的内容。



将PDF转换为C#中的文本 [ ^ ]



或类似的东西取决于你想要做什么。



编辑2:

再过一些测试我将PDF查看器添加到工具箱中。

查看了几个关于如何操作的问题后得到了想法。



选择项目> ; com组件> Adobe PDF Reader>好的

接下来你现在在你的工具箱中有这个项目。



创建一个新表格并将控件放到表格然后你你可以把它弄到底座上,或者你希望它在表格中。



接下来在表格1上设置你希望它打开然后打开的文件的路径传递路径的PDF表单。然后在加载或按钮点击(pdf表格)设置:


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}



只要它发现该控件中的pfd加载。



*******注意*****

关于这一点的事情是你必须将你的构建类型设置为X86,否则当你尝试在64位系统上运行它时会出现通信错误。



一旦pdf加载,打印选项就在那里。



只要在系统上安装了Acrobat reader,这应该可行。



希望这会有所帮助,现在我知道该怎么做了:)


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 :)


这篇关于从vb.net打印预览pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆