Encoding Problem In PrintDocument When Using Dot Matrix Printer - c#

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;
}

Related

TextRenderer Text is printing on the left top very small

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.

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);

PrintDocument just prints some part of my JPG file

I created an application that allows users to print multiple jpg files. So I send my print request directly, like this:
if (existfile == true)
{
PrinterSettings a=new PrinterSettings();
PrintDocument pd = new PrintDocument();
IEnumerable<PaperSize> size = a.PaperSizes.Cast<PaperSize>();
PaperSize a4 = size.First<PaperSize>(i => i.Kind == PaperKind.A4);
pd.DefaultPageSettings.Landscape = true;
pd.DefaultPageSettings.PaperSize = a4;
pd.PrintPage += PrintPage;
pd.Print();
}
And the print function :
private void PrintPage(object o, PrintPageEventArgs e)
{
System.Drawing.Image img = System.Drawing.Image.FromFile(CurrentAddress);
Point loc = new Point(100, 100);
e.Graphics.DrawImage(img, loc);
}
But this code just prints some part of my image, not all of it .I want to print them scale to fit. How can I do that?
This might fix the issue.
e.Graphics.DrawImage(img, e.MarginBounds);
or
e.Graphics.DrawImage(img, e.PageBounds);
or
ev.Graphics.DrawImage(Image.FromFile("C:\\My Folder\\MyFile.bmp"), ev.Graphics.VisibleClipBounds);

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;

How do I print a string with a printer?

I'm writing an application that need to print some information that would came from a DataGridView, I already have the string I'd like to print, I just don't know how. I found some stuff on the web that said I'd need to use a PrintDocument object and a PrintDialog.
Lets suppose I have 3 strings and I want to print to each one in one line (line 1, 2 and 3),but the first must be in bold and using the Arial font.
The output (on the paper) would be:
string 1 (in bold and using the Arial font)
string 2
string 3
EDIT: (asked by abelenky)
The Code:
private void PrintCoupon()
{
string text = "Coupon\n";
foreach (DataGridViewRow dgvRow in dataGridViewCarrinho.Rows)
{
foreach (DataGridViewCell dgvCell in dgvRow.Cells)
{
text += dgvCell.Value.ToString() + " ";
}
text += "\n";
}
MessageBox.Show(text);
// I should print the coupon here
}
So how do I do that using C#?
Thanks.
for printing strings on a paper you should draw them first on a PrintDocument using GDI+ in c#
in a Winform add PrintDocument tool to your project , and double click on it to access the PrintPage event handler of it ,
assuming you already have s1,s2 and s3 as String Variables ,
in the PrintPage event handler we use :
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Font f1 = new Font("Arial", 24, FontStyle.Bold, GraphicsUnit.Pixel);
Font f2 = new Font("Arial", 12, FontStyle.Regular, GraphicsUnit.Pixel);
Font f3 = new Font("Arial", 12, FontStyle.Regular, GraphicsUnit.Pixel);
e.Graphics.DrawString(s1, f1, Brushes.Black, new Point(10, 10));
e.Graphics.DrawString(s2, f2, Brushes.Black, new Point(10, 40));
e.Graphics.DrawString(s3, f3, Brushes.Black, new Point(10, 60));
}
and whenever you wanted to print the document :
printDocument1.Print();
you may also consider using a PrintPreviewDialog to see what's going on before printing the document
Try this ..
using System.Drawing;
private void printButton_Click(object sender, EventArgs e)
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler
(this.pd_PrintPage);
pd.Print();
}
// The PrintPage event is raised for each page to be printed.
void pd_PrintPage(Object* /*sender*/, PrintPageEventArgs* ev)
{
Font myFont = new Font( "m_svoboda", 14, FontStyle.Underline, GraphicsUnit.Point );
float lineHeight = myFont.GetHeight( e.Graphics ) + 4;
float yLineTop = e.MarginBounds.Top;
string text = "Coupon\n";
foreach (DataGridViewRow dgvRow in dataGridViewCarrinho.Rows)
{
foreach (DataGridViewCell dgvCell in dgvRow.Cells)
{
text += dgvCell.Value.ToString() + " ";
}
text += "\n";
}
//MessageBox.Show(text);
// I should print the coupon here
e.Graphics.DrawString( text, myFont, Brushes.Black,
new PointF( e.MarginBounds.Left, yLineTop ) );
yLineTop += lineHeight;
}

Categories

Resources