TextRenderer Text is printing on the left top very small - c#

I want to print a Multiline Text.
I searched and I found that I can print it with TextRenderer, but the text is printed very small in the left top of the paper. I don´t know why.
Code:
PrintDocument pd = new PrintDocument();
pd.PrintPage += PrintPage_Header;
pd.PrintPage += PrintPage_Adresse;
// pd.Print();
printPrvDlg.Document = pd;
printPrvDlg.ShowDialog();
private void PrintPage_Adresse(object sender, PrintPageEventArgs e)
{
printFont = new Font("Arial", 10, FontStyle.Bold);
Size size = TextRenderer.MeasureText(e.Graphics, stringToPrint, this.printFont, proposedSize, TextFormatFlags.WordBreak);
xPos = new System.Drawing.Rectangle(new Point(22, 150), size);
TextRenderer.DrawText(e.Graphics, stringToPrint, this.printFont, xPos, Color.Black, Color.White, TextFormatFlags.WordBreak);
}
I can´t find the problem.

Related

C# printing bitmap is blurry when printed on paper but clean when "printed" to PDF format

I want to print UserControl that contains labels, barcode, and a datagridview but when I want to print it on paper it is blury. But when i print it to PDF it's clean. Here is my code:
{
Graphics g = control.CreateGraphics();
var bitmap = new Bitmap(control.Width, control.Height, g);
bitmap.SetResolution(bitmap.HorizontalResolution, bitmap.VerticalResolution);
control.DrawToBitmap(bitmap, new System.Drawing.Rectangle(0, 0, control.Width, control.Height));
var pd = new PrintDocument();
pd.PrintPage += (s, e) => e.Graphics.DrawImage(bitmap, 0, 0);
PaperSize ps = new PaperSize();
ps.RawKind = (int)PaperKind.A4;
pd.DefaultPageSettings.PaperSize = ps;
pd.DefaultPageSettings.Landscape = false;
pd.Print();
}

Image printing not correct aspect ratio?

I'm trying to print an image however my aspect ration on the A4 papaer is incorrect. I did my search and tried to use the same mm size as the A4 paper but no luck. I'm working on Unity3D 2017.0.3f using .NET 3.5.
As you can see in the image above, it's in the bottom right and leaving all the space behind. I want it just landscape normal A4 printed.
Here's my code that I'm using to print:
public void btnPrint_Click()
{
PrintDocument pd = new PrintDocument();
pd.PrinterSettings.PrinterName = availablePrinter[0];
pd.DefaultPageSettings.Margins = new Margins(100, 100, 100, 100);
pd.OriginAtMargins = true;
pd.DefaultPageSettings.Landscape = true;
pd.PrinterSettings.DefaultPageSettings.Landscape = true;
pd.PrintPage += new PrintPageEventHandler(pqr);
pd.Print();
}
void pqr(object o, PrintPageEventArgs e)
{
Debug.Log("PrintingImage");
System.Drawing.Image i = System.Drawing.Image.FromFile(path);
e.Graphics.DrawImage(i, e.MarginBounds);
}
I tried using a rect and making it manually but still didn't work.
So i managed to fix it but changing the Margins
pd.DefaultPageSettings.Margins = new Margins(0, 0, 50, 125);

Graphics.DrawString centre text on printed page

I'm using the following to print out some text from my C# WPF app:
private void Button_Click(object sender, RoutedEventArgs e)
{
PrintDocument printDocument = new PrintDocument();
printDocument.PrinterSettings.PrinterName = "\\\\servername\\printername";
printDocument.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
if (printDocument.PrinterSettings.IsValid)
{
printDocument.Print();
}
}
// The PrintPage event is raised for each page to be printed.
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
string stringToPrint = "SOME TEXT TO PRINT";
// Create font and brush.
Font drawFont = new Font("Arial", 16);
SolidBrush drawBrush = new SolidBrush(System.Drawing.Color.Black);
System.Drawing.Point pos = new System.Drawing.Point(100, 100);
ev.Graphics.DrawString(stringToPrint, drawFont, drawBrush, pos);
ev.HasMorePages = false;
}
The above example is using a fixed position but I need to print out several lines of text all different lengths and I want to centre them all on the page (The x position).
How can I do this?
There is an overload of Graphics.DrawString that uses a StringFormat parameter that you can use to set the horizontal and vertical alignment of the text in the rectangle. I have used something like this in the past.
string stringToPrint = "SOME TEXT TO PRINT";
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
// Create font and brush.
Font drawFont = new Font("Arial", 16);
SolidBrush drawBrush = new SolidBrush(System.Drawing.Color.Black);
//Starting point of left margin,Width of page, Height of Text
System.Drawing.RectangleF rect = new System.Drawing.RectangleF(0, 100, 100, 50);
ev.Graphics.DrawString(stringToPrint, drawFont, drawBrush, rect, sf);
ev.HasMorePages = false;

I want to print a document usin c# . But my printer interpeat that i want to print with phografer paper but i want to print A4 normal paper

//Button print
printPreviewDialog1.Document = printDocument1;
printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage);
DialogResult result = printPreviewDialog1.ShowDialog();
if (result==DialogResult.OK)
{
printDocument1.Print ();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
e.Graphics.DrawString("Nome Equipamento: ", new Font("Arial", 12), Brushes.Black, new Point(25, 210));
e.Graphics.DrawString(comboBox1.Text, new Font("Arial", 12), Brushes.Black, new Point(25, 250));
Try something like this. I only hacked this down without testing and I added no error handling code. It assumes your printer supports printing A4. But you should get the idea.
PaperSize search;
foreach (PaperSize item in printDocument1.PrinterSettings.PaperSizes)
{
if (item.Kind == PaperKind.A4)
{
search = item;
break;
}
}
printDocument1.DefaultPageSettings.PaperSize = search;
printDocument1.Print();
Alternatively you can create your own paper size:
printDocument.DefaultPageSettings.PaperSize =
new PaperSize("My A4", A4width, A4height);

Encoding Problem In PrintDocument When Using Dot Matrix Printer

I have prepared a text to print with dot matrix printer. The text contains turkish characters like ü,ğ, etc.
When I send this text to the dot matrix printer with .net' s PrintDocument class, the turkish characters on the printed document does not appear correctly. But when I send the same text to Laser printer, there is no problem. How can I solve this problem? thanks for your help.
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
pd.Print();
...
static void pd_PrintPage(object sender, PrintPageEventArgs e)
{
Font fnt = new Font("Courier", 10, FontStyle.Regular);
TextRenderer.DrawText(e.Graphics, printText, fnt, new Point(0, 0), SystemColors.ControlText);
e.HasMorePages = false;
}

Categories

Resources