1. Have C# PrintDocument and VC++6 DLL printing on the same print job - CSharp/C#
2. Print pdf document using PRintDocument
Hi, Can we print a pdf document using .Net PrintDocument class? If yes then how? I dont wanna use Win32 spooler functions as it require all that DLLIMports etc. Thanks
3. Printing to a Word document using PrintDocument class - VB.Net
4. Print Documents PrintDocument-- what am I doing wrong? Must be something stupid
I am running out of printing paper trying to debug this...it has to be
trivial, but I cannot figure it out--can you? Why am I not printing
text, but just the initial string "howdy"?
On the screen, when I open a file, the entire contents of the file is
in fact being shown...so why can't I print it later? All of this code
I am getting from a book (Chris Sells) and the net. The solution is
to be found in the fact that stringbuilder is not retaining
information outside the 'using' bracket, despite the fact I made it
'global'.
Keyword search //!!! below to see where I think the problem lies.
RL
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Diagnostics;
namespace MyNameSpace1
{
public partial class MyForm : Form
{
string myPrintFilename;
StringBuilder myGlobalStringBuilder; //!!! this is supposed
to be global to the form MyForm, right?
string strModified; // = String.Copy(strOriginal); //not used
public MyForm()
{
InitializeComponent();
myGlobalStringBuilder = new StringBuilder("howdy"); //!!!
the only thing that gets printed is 'howdy'!
}
private void toolStripButton1_Click(object sender, EventArgs
e)
{
Stream myStream;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Title = "Open text file";
openFileDialog1.InitialDirectory = @"c:\";
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All
files (*.*)|*.*";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = openFileDialog1.OpenFile()) !=
null)
{
using (myStream)
{
StreamReader sr =
File.OpenText(openFileDialog1.FileName);
string s = sr.ReadLine();
StringBuilder sb = new StringBuilder();
while (s != null)
{
sb.Append(s);
s = sr.ReadLine();
myGlobalStringBuilder.Append(s); //!!! ??? Why is myGlobal not
appending here?
}
sr.Close();
textBox1.Text = sb.ToString(); //this
works, to show the file text on the screen textBox1
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: could not read file from
disk (myStream); Err: " + ex.Message);
}
}
}
private void printToolStripButton_Click(object sender,
EventArgs e)
{
if (myPrintFilename != "")
{
this.printDocument1.DocumentName =
this.myPrintFilename;
this.printDocument1.Print();
}
}
private void printDocument1_PrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{
//p. 292 Chris Sells
//draw to the e.Graphics object that wraps the print
target
Graphics g = e.Graphics;
using (Font font = new Font("Lucida Console", 48) )
{
string mylocalstring;
mylocalstring =
myGlobalStringBuilder.ToString(); //!!! only prints "Howdy"--the
initial string--never the appended string from myGlobalStringBuilder--
why?
if (myGlobalStringBuilder.Length != 0)
{
g.DrawString(mylocalstring, font, Brushes.Blue, 0,
0);
}
}
}
}
}
5. Print Documents PrintDocument-- what am I doing wrong? Must be so - CSharp/C#
6. Print Documents PrintDocument-- what am I doing wrong? Must be so
Set a breakpoint in printDocument1_PrintPage, at the line where
"mylocalstring" is set. I believe you will see that the value is correct for
"myStringBuilder". The lack of carriage returns will hide the fact that the
string is correct when printed. I believe it is going off the page. The
print document functions won't fix newlines, wrapping, etc. Also, you need
to worry about the next page when the text is long. Printing is very
complicated in .net, until you get the hang of it...
"raylopez99" wrote:
> I am running out of printing paper trying to debug this...it has to be
> trivial, but I cannot figure it out--can you? Why am I not printing
> text, but just the initial string "howdy"?
>
> On the screen, when I open a file, the entire contents of the file is
> in fact being shown...so why can't I print it later? All of this code
> I am getting from a book (Chris Sells) and the net. The solution is
> to be found in the fact that stringbuilder is not retaining
> information outside the 'using' bracket, despite the fact I made it
> 'global'.
>
> Keyword search //!!! below to see where I think the problem lies.
>
> RL
>
> using System;
> using System.Collections.Generic;
> using System.ComponentModel;
> using System.Data;
> using System.Drawing;
> using System.Linq;
> using System.Text;
> using System.IO;
> using System.Windows.Forms;
> using System.Diagnostics;
>
>
> namespace MyNameSpace1
> {
> public partial class MyForm : Form
> {
> string myPrintFilename;
> StringBuilder myGlobalStringBuilder; //!!! this is supposed
> to be global to the form MyForm, right?
>
> string strModified; // = String.Copy(strOriginal); //not used
>
> public MyForm()
> {
> InitializeComponent();
> myGlobalStringBuilder = new StringBuilder("howdy"); //!!!
> the only thing that gets printed is 'howdy'!
>
> }
>
> private void toolStripButton1_Click(object sender, EventArgs
> e)
> {
> Stream myStream;
> OpenFileDialog openFileDialog1 = new OpenFileDialog();
> openFileDialog1.Title = "Open text file";
> openFileDialog1.InitialDirectory = @"c:\";
> openFileDialog1.Filter = "txt files (*.txt)|*.txt|All
> files (*.*)|*.*";
>
> if (openFileDialog1.ShowDialog() == DialogResult.OK)
> {
> try
> {
> if ((myStream = openFileDialog1.OpenFile()) !=
> null)
> {
> using (myStream)
> {
> StreamReader sr =
> File.OpenText(openFileDialog1.FileName);
> string s = sr.ReadLine();
> StringBuilder sb = new StringBuilder();
> while (s != null)
> {
> sb.Append(s);
> s = sr.ReadLine();
>
> myGlobalStringBuilder.Append(s); //!!! ??? Why is myGlobal not
> appending here?
> }
> sr.Close();
> textBox1.Text = sb.ToString(); //this
> works, to show the file text on the screen textBox1
>
> }
> }
> }
> catch (Exception ex)
> {
> MessageBox.Show("Error: could not read file from
> disk (myStream); Err: " + ex.Message);
> }
> }
> }
>
> private void printToolStripButton_Click(object sender,
> EventArgs e)
> {
> if (myPrintFilename != "")
> {
> this.printDocument1.DocumentName =
> this.myPrintFilename;
>
> this.printDocument1.Print();
> }
> }
>
> private void printDocument1_PrintPage(object sender,
> System.Drawing.Printing.PrintPageEventArgs e)
> {
> //p. 292 Chris Sells
> //draw to the e.Graphics object that wraps the print
> target
>
> Graphics g = e.Graphics;
> using (Font font = new Font("Lucida Console", 48) )
> {
> string mylocalstring;
>
> mylocalstring =
> myGlobalStringBuilder.ToString(); //!!! only prints "Howdy"--the
> initial string--never the appended string from myGlobalStringBuilder--
> why?
>
> if (myGlobalStringBuilder.Length != 0)
> {
> g.DrawString(mylocalstring, font, Brushes.Blue, 0,
> 0);
> }
> }
>
> }
>
>
> }
> }
>
>
>
>
7. Print Documents PrintDocument-- what am I doing wrong? Must be something stupid - CSharp/C#
8. Can I monitor PrintDocument.Print?
I want to do something like this to interrupt the print process (But
this does not work because the error is "cannot implicitly conver type
void to bool"):
while(pd.Print())
{
pCounter++;
if(printerSettings.Copies == 2)
{
pd.DefaultPageSettings.PaperSource =
pd.PrinterSettings.PaperSources[comboBox1.SelectedIndex = 5];
}
if(printerSettings.Copies == 1)
{
pd.DefaultPageSettings.PaperSource =
pd.PrinterSettings.PaperSources[comboBox1.SelectedIndex = 6];
}
if(pCounter == 6)
break;
}
Thanks,
Trint