473,398 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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_PrintPage 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_PrintPage()'. Can you please tell me what that code should be??


Thank you in advance.


- Swetha
Aug 8 '08 #1
2 3233
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
Aug 8 '08 #2
I could now generate the print preview of any file. using the following code:

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.IO;
  3. using System.Drawing;
  4. using System.Drawing.Printing;
  5. using System.Windows.Forms;
  6.  
  7.  
  8.  public class PrintingExample : System.Windows.Forms.Form 
  9.  {
  10.      private System.ComponentModel.Container components;
  11.      private System.Windows.Forms.Button printButton;
  12.      private Font printFont;
  13.      private StreamReader streamToPrint;
  14.  
  15.     public PrintingExample() : base() 
  16.     {
  17.        // The Windows Forms Designer requires the following call.
  18.        InitializeComponent();
  19.     }
  20.  
  21.     // The Click event is raised when the user clicks the Print button.
  22.     private void printButton_Click(object sender, EventArgs e) 
  23.     {
  24.        try 
  25.        {
  26.            streamToPrint = new StreamReader
  27.               ("C:\\My Documents\\MyFile.txt");
  28.            try 
  29.            {
  30.               printFont = new Font("Arial", 10);
  31.               PrintDocument pd = new PrintDocument();
  32.               pd.PrintPage += new PrintPageEventHandler               (this.pd_PrintPage);
  33.               printPreviewDialog1.Document = pd;
  34.               printPreviewDialog1.ShowDialog();
  35.               pd.Print();
  36.            }  
  37.            finally 
  38.            {
  39.               streamToPrint.Close();
  40.            }
  41.        }  
  42.        catch(Exception ex) 
  43.        {
  44.            MessageBox.Show(ex.Message);
  45.        }
  46.     }
  47.  
  48.     // The PrintPage event is raised for each page to be printed.
  49.     private void pd_PrintPage(object sender, PrintPageEventArgs ev) 
  50.     {
  51.        float linesPerPage = 0;
  52.        float yPos = 0;
  53.        int count = 0;
  54.        float leftMargin = ev.MarginBounds.Left;
  55.        float topMargin = ev.MarginBounds.Top;
  56.        string line = null;
  57.  
  58.        // Calculate the number of lines per page.
  59.        linesPerPage = ev.MarginBounds.Height / 
  60.           printFont.GetHeight(ev.Graphics);
  61.  
  62.        // Print each line of the file.
  63.        while(count < linesPerPage && 
  64.           ((line=streamToPrint.ReadLine()) != null)) 
  65.        {
  66.           yPos = topMargin + (count * 
  67.              printFont.GetHeight(ev.Graphics));
  68.           ev.Graphics.DrawString(line, printFont, Brushes.Black, 
  69.              leftMargin, yPos, new StringFormat());
  70.           count++;
  71.        }
  72.  
  73.        // If more lines exist, print another page.
  74.        if(line != null)
  75.           ev.HasMorePages = true;
  76.        else
  77.           ev.HasMorePages = false;
  78.     }
  79.  
  80.  
  81.     // The Windows Forms Designer requires the following procedure.
  82.     private void InitializeComponent() 
  83.     {
  84.        this.components = new System.ComponentModel.Container();
  85.        this.printButton = new System.Windows.Forms.Button();
  86.  
  87.        this.ClientSize = new System.Drawing.Size(504, 381);
  88.        this.Text = "Print Example";
  89.  
  90.        printButton.ImageAlign = 
  91.           System.Drawing.ContentAlignment.MiddleLeft;
  92.        printButton.Location = new System.Drawing.Point(32, 110);
  93.        printButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
  94.        printButton.TabIndex = 0;
  95.        printButton.Text = "Print the file.";
  96.        printButton.Size = new System.Drawing.Size(136, 40);
  97.        printButton.Click += new System.EventHandler(printButton_Click);
  98.  
  99.        this.Controls.Add(printButton);
  100.     }
  101.  
  102.     // This is the main entry point for the application.
  103.     public static void Main(string[] args) 
  104.     {
  105.        Application.Run(new PrintingExample());
  106.     }
  107.  }
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.
Aug 8 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Bruce Zigenfous | last post by:
Hello, I am using an ASP that uses a third party active-x control to upload a file to a file server using the ADO Stream Object. It uses the FileSystemObject to create folders and files, which...
0
by: Bruce Zigenfous | last post by:
Hello, Here is the code. followed by an explanation for what I am trying to do. In particular, this line: BinaryStream.Write byteArray 'Need to format <%Response.Buffer=0 %> <!--#include...
5
by: John Morgan | last post by:
I am using the following link to download a file of about 50k <a target= "_blank" href="http://www.bsecs.org.uk/ExecDocs/documentStore/elfridaWord.doc">open file</a> If I save the file to...
1
by: Bill Short | last post by:
I would like to print a report (or reports) from Access based on the Word document I have open. For instance, the Word doc I have open is called 10-789-65.doc. From Word, I would like to open...
1
by: ajk | last post by:
. Hi, All: I know how to insert files into a Word doc using C#. However, the program I've written to do this runs much too slowly. The "myObj".Application.Selection.InsertFile method executes...
1
by: Darin | last post by:
I would like to check if a document is a Word document. I'm currently just checking for the "doc" extension but the extension can be changed so that's no good. Any examples or guidance on how...
0
by: srivalli chavali via DotNetMonster.com | last post by:
Hi, I have to print a word document using C#.NET. I also have to customize the printing option, the Printer tray, programmatically. So, I have used the PrintDocument class available in C#....
1
by: Andrew | last post by:
I'm adding this as it to me a while to figure out all the pieces to be able to do this without using Microsoft.Office.Interop which caused me problems on the web-server. Streaming is the easy...
2
by: s.danyal.k | last post by:
Hi All, I have created an application in C# that converts HTML file to MS word documents. The HTML file may also have images , for e.g "<img src='http://www.google.com.pk/images/hp0.gif'></img>"....
1
by: arun raut | last post by:
On Apr 10, 10:32 pm, Rich <R...@discussions.microsoft.comwrote: help not compilation write all easier. But Rich, Without being sure completely, if you want to use HTML as help
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.