Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8342579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:48:55+00:00 2026-06-09T05:48:55+00:00

In my application I’m trying to create a function to print existing PDFs or

  • 0

In my application I’m trying to create a function to print existing PDFs or Doc. How can I do this in C# and provide a mechanism so the user can select a different printer or other properties.

I’ve looked at the PrintDialog but not sure what file it is attempting to print, if any, b/c the output is always a blank page. Maybe I’m just missing something there.

Any advice, examples or sample code would be great!

The below is my code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
         public Form1()
         {
              InitializeComponent();
         }

         private void button1_Click(object sender, EventArgs e)
         {
              string printPath = 
                   System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
              System.IO.StreamReader fileToPrint;
              fileToPrint= new System.IO.StreamReader(printPath + @"\myFile.txt");
              System.Drawing.Font printFont;
              printPDF(e);
              printDocument1.Print();
              fileToPrint.Close();
         }

         private void button2_Click(object sender, EventArgs e)
         {
              //printDoc(e);
         }

         public void printPDF(object sender ,  
                              System.Drawing.Printing.PrintPageEventArgs e))
         {       
              printFont = new System.Drawing.Font("Arial", 10);
              float yPos = 0f;
              int count = 0;
              float leftMargin = e.MarginBounds.Left;
              float topMargin = e.MarginBounds.Top;
              string line = null;
              float linesPerPage = e.MarginBounds.Height /     
                                   printFont.GetHeight(e.Graphics);
              while (count < linesPerPage)
              {
                    line = fileToPrint.ReadLine();
                    if (line == null)
                    {
                         break;
                    }
              yPos = topMargin + count * printFont.GetHeight(e.Graphics);
              e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos,
                                  new StringFormat());
              count++;
              }

              if (line != null)
              {
                   e.HasMorePages = true;
              }

              fileToPrint.Close();        
         }

         public void printDoc()
         {
         }
     }
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T05:48:56+00:00Added an answer on June 9, 2026 at 5:48 am

    This has worked in the past:

    using System.Diagnostics.Process;
    
    ...
    
    Process process = new Process();
    
    process.StartInfo.FileName = pathToPdfOrDocFile; 
    process.UseShellExecute = true;
    process.StartInfo.Verb = "printto";
    process.StartInfo.Arguments = "\"" + printerName + "\""; 
    process.Start();
    
    process.WaitForInputIdle();
    process.Kill();
    

    To print to the default printer, replace printto with print, and leave off the Arguments line.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Application: This is a workshop proposal system for a conference. A user can create
The application crashes on the row: [uids addObject:user.uid]; Who can tell why??
Application : Nerddinner. This SP is for an inserting password for newly created User.
Application : HTA (therefore IE) This is an application that uses SendKeys to populate
Application works fine. I can access everything in debug except for an Array Adapter
Application stores configuration data in custom section of configuration file. This information is used
Application running in django, apache, python and mysql: I want to create global variable
My application now needs to create and then write files to a temp directory
Application.Run(form); Actually I tried to call this from my project. I got this exception.
Application.Run(new Main()); This line gives TypeInitializationException was unhandled after I switched from 3.5 to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.