C# Winform PrintDialog Pages popup - c#

in C# Winform, i'm sending an Image to the printer using the PrintDialog... using this code:
private void PrintSnippedImage()
{
PrintDocument pDoc = new PrintDocument();
pDoc.PrintPage += PrintPage;
PrintDialog pDialog = new PrintDialog();
pDialog.ShowHelp = true;
pDialog.AllowSelection = false;
pDialog.AllowSomePages = false;
pDialog.Document = pDoc;
if (pDialog.ShowDialog() == DialogResult.OK)
{
pDoc.Print();
};
}
private void PrintPage(object o, PrintPageEventArgs e)
{
System.Drawing.Image img = Glob.SnippedImage;
Point loc = new Point(10, 10);
e.Graphics.DrawImage(img, loc);
}
I get a popup that shows "Printing... Page 1 of document" with a Cancel button.
it appears in seemingly random locations on one of my monitors.
is there a way to inhibit this popup, or at least force it to appear on the monitor in the general location of the application that called it?
it looks like this:

It should be a case of swapping the PrintController implementation from PrintControllerWithStatusDialog (the default) to StandardPrintController. See https://learn.microsoft.com/en-us/dotnet/api/system.drawing.printing.printdocument.printcontroller?view=dotnet-plat-ext-6.0#system-drawing-printing-printdocument-printcontroller

Related

how to hide printing popup in c#?

I have a print function which prints received messages in my application this application is a background process, but as soon as I get a new message, it displays "printing box" for a brief moment, how can I hide this.
PrintDocument DocumentPrint = new PrintDocument();
DocumentPrint.PrintPage += new PrintPageEventHandler(page_print);
DocumentPrint.Print();
private void page_print(object sender, PrintPageEventArgs ev)
{
ev.Graphics.DrawString(strMessagePrint,
clsGlobals.DEFAULT_PRINTER_FONT,
Brushes.Black,
new Point(clsGlobals.DEFAULT_LEFT_MARGIN, clsGlobals.DEFAULT_TOP_MARGIN));
}
Solve the problem to use PrintDocument's PrintController to StandardPrintController should solve this.
PrintDocument printDocument = new PrintDocument();
PrintController printdisable = new StandardPrintController();
printDocument.PrintController = printdisable ;

C# - Use Windows 10 'Microsoft Print to PDF' for multiple images

I want to print multiple images to a pdf file, but not using the regular print-dialogue, but the 'print pictures' dialogue (see image below). As a printer, I want to use the 'Microsoft print to PDF'-Printer, which comes with windows 10.
My current code looks like this:
string fileName = #"C:\Users\mt\Pictures\test\test.jpg";
var p = new Process();
p.StartInfo.FileName = fileName;
p.StartInfo.Verb = "Print";
p.Start();
So far so good, the right dialogue is showing. I tried it first with other methods, which didn't give me this one. I can now print one image to a pdf page, but how do I add other images? Additionally, in this solution, I can't see how to set the printing-settings, like size of the image, which should be as large as possible without cutting the image and changing to horizontal format.
Earlier, I had this code:
try
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
pd.PrinterSettings.PrinterName = STR_PRINTER_NAME;
pd.Print();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
private void pd_PrintPage(object sender, PrintPageEventArgs e)
{
System.Drawing.Image img = System.Drawing.Image.FromFile(#"C:\Users\mt\Pictures\test\1.jpg");
Point loc = new Point(100, 100);
e.Graphics.DrawImage(img, loc);
img.Dispose();
}
which I changed for the 'Print Picture'-Dialog. In the end, the dialog itself doesn't matter, but the 'Print Picture'-Dialog got all settings I need. The only thing the user has to set, is the name for the pdf-file.
Solved the 'Multiple-Page' problem with the following code:
private void button1_Click(object sender, EventArgs e)
{
imgs.Add(img1);
imgs.Add(img2);
imgs.Add(img3);
imgs.Add(img4);
try
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
pd.PrinterSettings.PrinterName = "Microsoft Print to PDF";
pd.Print();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void pd_PrintPage(object sender, PrintPageEventArgs e)
{
Graphics graphic = e.Graphics;
Point p = new Point(10, 10);
Image img = Image.FromFile(imgs.ElementAt(page));
graphic.DrawImage(img, p);
page++;
e.HasMorePages = page < imgs.Count;
img.Dispose();
}
Still couldn't figure out how to set maximum size

Printpreview controller does not display the file

I have the codes below
fName =E:\Csharp\NewMacaron\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\\data00.txt"
From a method within my program I create, into application folder, a series of datas as fName above
After lunching the printing form I have the printcontroller preview on screen with Previous Next and some other buttons for printing and zooming.
On form_load I have ShowPage();
PrintPageEventHandler does not work and nothing is displayed.
However when I click the print_button correct page is printed:
What is wrong with the printpreviewcontroller ?
I'am using .net 3.5 and c# 2008
private void ShowPage()
{
try
{
streamToPrint = new StreamReader(fName, Encoding.UTF8);
pd = new PrintDocument();
pd.DocumentName = fName;
PrinterSettings ps = new PrinterSettings();
printFont = new Font("Courier", 10);
pd.PrinterSettings.PrinterName = prnName; // GetDefaultPrinter returns prName
pd.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("PaperA4", 840, 1180);
ppc = new PrintPreviewControl();
ppc.Document = pd;
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
}
catch (Exception es)
{
MessageBox.Show(es.Message.ToString());
}
}
private void print_button(object sender, EventArgs e)
{
streamToPrint = new StreamReader(fName, Encoding.UTF8);
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
try
{
pd.Print();
}
catch (Win32Exception es)
{
MessageBox.Show(es.Message.ToString());
streamToPrint.Close();
}
streamToPrint.Close();
PublicVariables.PrintFormStat = false;
PublicVariables.PrintData = 0;
this.Hide();
I have a CRIMINAL BUG in the program the printform was named ppc and the printpreviewcontroller also.
I don't know how assembly has accepted that.
After the correction of that tI can only use one time printpreviewcontroller
The second page is not displayed.

Printing a textbot text

I'm trying to make a program that prints a textbox full text. When I'm generating it to a pdf to see the page before I print it some of the text is missing.
This is my code:
private void Print_Click(object sender, EventArgs e)
{
PrintDialog PD = new PrintDialog();
PrintDocument PDoc = new PrintDocument();
PD.Document = PDoc;
PDoc.PrintPage += new PrintPageEventHandler(PDoc_PrintPage);
DialogResult result = PD.ShowDialog();
if (result == DialogResult.OK)
{
PDoc.Print();
}
}
void PDoc_PrintPage(object sender, PrintPageEventArgs e)
{
Graphics graph = e.Graphics;
graph.DrawString(textBox1.Text,
new Font("Arial", 12), new SolidBrush(Color.Black), 10, 10);
}
This is the text (Just for a test):
hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello
hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello
hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello
this is the textbox screenshot:
This is what I get:
as you can see some of the text is missing. I'm not really familiar with printing so I have no Idea how to solve this problem.
It would appear your problem is that you want the text to wrap when printing. Have a look at this previous thread: Automatically Word-Wrapping Text To A Print Page?

Printer only prints to default printer

Here is the code I use (just the printing-related part):
Button 1 onclick handler method:
printDialog1 = new PrintDialog();
printDialog1.AllowPrintToFile = true;
printDialog1.PrintToFile = false;
if (printDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
PrintDocument pd = new PrintDocument();
pd.DefaultPageSettings.PaperSize = new PaperSize("A4", 826, 1169);
pd.PrinterSettings.PrintToFile = true;
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
pd.Print();
}
And my pd_PrintPage method:
Bitmap bitmapCanvas = new Bitmap(1000, 1000);
Graphics g = Graphics.FromImage(bitmapCanvas);
g.Clear(Color.White);
...
some g.Draw...() stuff
...
e.Graphics.DrawImage(bitmapCanvas, A(2), A(2));
//where e is the PrintPageEventArgs defined in the method signature
First part of my problem is that, this doesn't print to the selected printer (selected on the print dialog).
It only prints to a printer if that is the default printer.
Under Windows 7 it works, it recognizes the default printer, so the default printer will be selected by default in the comboBox on the print dialog which comes up after I click the button.
My main problem is that, this doesn't work under Windows Xp at all (unfortunately I only can use that). And I'm kind of curious why. So I don't know if I made a mess, or it is not supported under Windows Xp.
With what should I complete or correct my code?
Any help is appreciated and thank you very much!
Mitulat bati
Try this,
printDialog1 = new PrintDialog();
printDialog1.AllowPrintToFile = true;
printDialog1.PrintToFile = false;
if (printDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
printDialog1.Document.DefaultPageSettings.PaperSize = new PaperSize("A4", 826, 1169);
printDialog1.Document.PrinterSettings.PrintToFile = true;
printDialog1.Document.PrintPage += new PrintPageEventHandler(pd_PrintPage);
printDialog1.Document.Print();
}

Categories