相关文章推荐
稳重的佛珠  ·  Camunda 7工作流引擎 API ...·  3 天前    · 
闯红灯的牛肉面  ·  高斯模糊 - 知乎·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am attempting to create a print preview of a document when the user selects the document from a listview. I know I need to create a print document and pass it to the printpreviewcontrol, but I don't know how to "assign" a file to the printdocument (I know my example below simply gives it a name). Is this possible? All examples I have found in forums and MSDN deal with basic textfiles. For example, how to printpreview office docs, pdf, etc.?

Imports System.Windows.Forms
Imports System.Drawing.Printing.PrintDocument
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    PrintDocument1.DocumentName = "C:\Documents and Settings\Practice.xlsx"
    PrintPreviewControl1.Document = PrintDocument1
End Sub
End Class

Any help would be appreciated. I feel like I am missing something simple. Thanks!

The PrintDocument object, despite its name, is not a document "reader." The only thing it does is manage the printing process for whatever thing it is that you want to print. The "thing" you want to print can be anything, and the way you print it is by making GDI+ graphics calls, like "draw a line from here to here" and "put this text here at this size." In other words, you create the printed document by calling methods on a System.Drawing.Graphics.Graphics object.

So, in order to load a PDF, Word Doc, or any other "document" format, you will need to find a library that allows you to render the document using GDI+. A quick Google search turned up PDFRasterizer.Net for PDF files, for example.

Thank you for clarifying this; I was beginning to stumble upon the graphic object caveat. Thank you for helping. – Josh Weston Mar 23, 2013 at 20:00

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.