how to generate the print preview of a Word docuement(.doc) file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sswetha21
    New Member
    • Aug 2008
    • 2

    how to generate the print preview of a Word docuement(.doc) file

    Hi,



    I am a beginner in C# programming and I am trying to generate a print preview for a .doc file using C# .net. I got a code for the same in msdn. The link is click here..


    If we directly run this code, it produces the print preview with a default message "In document_PrintP age method".

    Thus, the code to render the page(for which the print preview has to be generated) has to be written in the function 'document_Print Page()'. Can you please tell me what that code should be??


    Thank you in advance.


    - Swetha
  • shoebkool
    New Member
    • Feb 2008
    • 2

    #2
    Hey swetha
    Even i been trying that print preview i was unable to find out. just inform me even if get an answer.
    Thank a lot
    Last edited by shoebkool; Aug 8 '08, 05:38 AM. Reason: BY mistake send before completing

    Comment

    • sswetha21
      New Member
      • Aug 2008
      • 2

      #3
      I could now generate the print preview of any file. using the following code:

      Code:
      using System;
      using System.IO;
      using System.Drawing;
      using System.Drawing.Printing;
      using System.Windows.Forms;
      
      
       public class PrintingExample : System.Windows.Forms.Form 
       {
           private System.ComponentModel.Container components;
           private System.Windows.Forms.Button printButton;
           private Font printFont;
           private StreamReader streamToPrint;
      
          public PrintingExample() : base() 
          {
             // The Windows Forms Designer requires the following call.
             InitializeComponent();
          }
      
          // The Click event is raised when the user clicks the Print button.
          private void printButton_Click(object sender, EventArgs e) 
          {
             try 
             {
                 streamToPrint = new StreamReader
                    ("C:\\My Documents\\MyFile.txt");
                 try 
                 {
                    printFont = new Font("Arial", 10);
                    PrintDocument pd = new PrintDocument();
                    pd.PrintPage += new PrintPageEventHandler               (this.pd_PrintPage);
                    printPreviewDialog1.Document = pd;
                    printPreviewDialog1.ShowDialog();
                    pd.Print();
                 }  
                 finally 
                 {
                    streamToPrint.Close();
                 }
             }  
             catch(Exception ex) 
             {
                 MessageBox.Show(ex.Message);
             }
          }
      
          // The PrintPage event is raised for each page to be printed.
          private void pd_PrintPage(object sender, PrintPageEventArgs ev) 
          {
             float linesPerPage = 0;
             float yPos = 0;
             int count = 0;
             float leftMargin = ev.MarginBounds.Left;
             float topMargin = ev.MarginBounds.Top;
             string line = null;
      
             // Calculate the number of lines per page.
             linesPerPage = ev.MarginBounds.Height / 
                printFont.GetHeight(ev.Graphics);
      
             // Print each line of the file.
             while(count < linesPerPage && 
                ((line=streamToPrint.ReadLine()) != null)) 
             {
                yPos = topMargin + (count * 
                   printFont.GetHeight(ev.Graphics));
                ev.Graphics.DrawString(line, printFont, Brushes.Black, 
                   leftMargin, yPos, new StringFormat());
                count++;
             }
      
             // If more lines exist, print another page.
             if(line != null)
                ev.HasMorePages = true;
             else
                ev.HasMorePages = false;
          }
      
      
          // The Windows Forms Designer requires the following procedure.
          private void InitializeComponent() 
          {
             this.components = new System.ComponentModel.Container();
             this.printButton = new System.Windows.Forms.Button();
      
             this.ClientSize = new System.Drawing.Size(504, 381);
             this.Text = "Print Example";
      
             printButton.ImageAlign = 
                System.Drawing.ContentAlignment.MiddleLeft;
             printButton.Location = new System.Drawing.Point(32, 110);
             printButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
             printButton.TabIndex = 0;
             printButton.Text = "Print the file.";
             printButton.Size = new System.Drawing.Size(136, 40);
             printButton.Click += new System.EventHandler(printButton_Click);
      
             this.Controls.Add(printButton);
          }
      
          // This is the main entry point for the application.
          public static void Main(string[] args) 
          {
             Application.Run(new PrintingExample());
          }
       }
      But the problem is, it works fine for any text. But it is showing the garbage values for rest of the things like bullets or images..

      And for pdfs it is showing total garbage.

      Could someone please tell why this is happening. and what should i do to avoid it..
      Thanks in advance.

      Comment

      Working...