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?
Related
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
I've a code where i am making a bitmap image in runtime and i want to get the print of that image. My image is saved in a bitmap variable. and i want to get the print of that image. or in other words i want to access that variable in "Print code": which is in my case is the second section of code.
But i am not getting anything after the executing the "Print Code"
here is my code for Image.
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
var bmp = new Bitmap(this.picBoxCard.Width, this.picBoxCard.Height);
this.picBoxCard.DrawToBitmap(bmp, new Rectangle(Point.Empty, bmp.Size));
e.Graphics.DrawImage(bmp,50,105);
bmp.Save(#"Image.png", ImageFormat.Png); //Just saving the Image for making sure
}
code to execute for printing.
private void picPrint_Click(object sender, EventArgs e)
{
PrintDialog pd = new PrintDialog();
PrintDocument doc = new PrintDocument();
doc.PrintPage += printDocument1_PrintPage;
pd.Document = doc;
if (pd.ShowDialog() == DialogResult.OK)
{
doc.Print();
}
}
Please guide me where I am making any mistake.
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
I found this code to print
// The PrintDialog will print the document
// by handling the document's PrintPage event.
private void document_PrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{
// Insert code to render the page here.
// This code will be called when the control is drawn.
// The following code will render a simple
// message on the printed document.
string text = "In document_PrintPage method.";
System.Drawing.Font printFont = new System.Drawing.Font
("Arial", 35, System.Drawing.FontStyle.Regular);
// Draw the content.
e.Graphics.DrawString(text, printFont,
System.Drawing.Brushes.Black, 10, 10);
}
// Declare the PrintDocument object.
private System.Drawing.Printing.PrintDocument docToPrint =
new System.Drawing.Printing.PrintDocument();
private void printButton_Click(object sender, EventArgs e)
{
PrintDialog PrintDialog1 = new PrintDialog();
// Allow the user to choose the page range he or she would
// like to print.
PrintDialog1.AllowSomePages = true;
// Show the help button.
PrintDialog1.ShowHelp = true;
// Set the Document property to the PrintDocument for
// which the PrintPage Event has been handled. To display the
// dialog, either this property or the PrinterSettings property
// must be set
PrintDialog1.Document = docToPrint;
DialogResult result = PrintDialog1.ShowDialog();
// If the result is OK then print the document.
if (result == DialogResult.OK)
{
docToPrint.Print();
}
}
I execute it and the result of the printing is an empty page, my question is where can i put the data to print? and how can i make the printed data as rows each row has label and value.
You create the PrintDocument as a private member:
// Declare the PrintDocument object.
private System.Drawing.Printing.PrintDocument docToPrint =
new System.Drawing.Printing.PrintDocument();
But that makes it hard to attach the event handler at the right moment. I suggest using the constructor:
// Declare the PrintDocument object.
private System.Drawing.Printing.PrintDocument docToPrint;
//= new System.Drawing.Printing.PrintDocument();
public Form1() // the Form ctor
{
InitializeComponents();
docToPrint = new System.Drawing.Printing.PrintDocument();
docToPrint.PrintPage += document_PrintPage; // the missing piece
}
I want to print the contents of a simple TextBox. After I click the print button, PrintDialog is shown.
I found a lot of info but they all use RichTextBoxes. Is there an easy way to do something like this?
This print contents of textbox named textbox1
PrintDocument document = new PrintDocument();
PrintDialog dialog = new PrintDialog();
public Form1()
{
InitializeComponent();
document.PrintPage += new PrintPageEventHandler(document_PrintPage);
}
void document_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawString(textBox1.Text, new Font("Arial", 20, FontStyle.Regular), Brushes.Black, 20, 20);
}
private void btnPrint_Click(object sender, EventArgs e)
{
dialog.Document = document;
if (dialog.ShowDialog() == DialogResult.OK)
{
document.Print();
}
}
Take a look at this: http://answers.yahoo.com/question/index?qid=20081230163003AA4xOaT,
and this: How to print the contents of a TextBox
Also, there is a tutorial on printing in C#: http://www.dreamincode.net/forums/topic/44330-printing-in-c%23/
If, after this, you still cannot print TextBox content from some reason, you can always create a new RichTextBox object and assign your TextBox's Text to its text. Then proceed with printing using the RichTextBox.