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);
Related
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.
I've written a C# print test project with pre-printed form - System.Drawing.Printing.PrintDocument. The purpose of it is to print the image from a file on custom paper size. Before to print the image is scaling down according to the paper size.
public PrintDocument printDoc = new PrintDocument();
.....
private void PrintButton_Click(object sender, EventArgs e)
{
string FileName = "D:\\temp\\testprint.png";
try
{
if (string.IsNullOrWhiteSpace(FileName)) return; // Prevents execution of below statements if filename is not selected.
PrintDocument pd = new PrintDocument();
PaperSize paperSize = new PaperSize("TEST PAPER SIZE", 50, 50);
paperSize.RawKind = (int)PaperKind.Custom;
//Disable the printing document pop-up dialog shown during printing.
PrintController printController = new StandardPrintController();
pd.PrintController = printController;
//For testing only: Hardcoded set paper size to particular paper.
pd.PrinterSettings.DefaultPageSettings.PaperSize = paperSize;
pd.DefaultPageSettings.PaperSize = paperSize;
pd.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);
pd.PrinterSettings.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);
pd.PrintPage += (sndr, args) =>
{
System.Drawing.Image i = System.Drawing.Image.FromFile(FileName);
args.Graphics.PageUnit = System.Drawing.GraphicsUnit.Millimeter;
//Adjust the size of the image to the page to print the full image without loosing any part of the image.
System.Drawing.Rectangle m = args.MarginBounds;
//Logic below maintains Aspect Ratio.
if ((double)i.Width / (double)i.Height > (double)m.Width / (double)m.Height) // image is wider
{
m.Height = (int)((double)i.Height / (double)i.Width * (double)m.Width);
}
else
{
m.Width = (int)((double)i.Width / (double)i.Height * (double)m.Height);
}
//Calculating optimal orientation.
pd.DefaultPageSettings.Landscape = m.Width > m.Height;
//Putting image in center of page.
m.Y = (int)((((System.Drawing.Printing.PrintDocument)(sndr)).DefaultPageSettings.PaperSize.Height - m.Height) / 2);
m.X = (int)((((System.Drawing.Printing.PrintDocument)(sndr)).DefaultPageSettings.PaperSize.Width - m.Width) / 2);
args.Graphics.DrawImage(i, m);
};
pd.Print();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
The image scaling works fine but paper size isn't changing it always stay A4. I've googled a lot and applied some options but still can't set custom paper properties. Can anyone help me to find out what the problem is? Or at least point me where to dig on?
Thanks in advance!
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);
In my application i have a chart which is surrounded inside a panel. I added a printdocument component from the toolbox and when i want to print the chart, i am creating a bitmap and i get the panel inside the bitmap (and as a result the chart which is inside the panel). My problem is that when i create the bitmap, when i send it to the printer to print it, it eats up some of the chart from the bottom and from the right side. Below is my code to execute this
private void button1_Click(object sender, EventArgs e)
{
PrintDialog printDialog = new PrintDialog();
printDialog.Document = printDocument1;
printDialog.UseEXDialog = true;
//Get the document
if (DialogResult.OK == printDialog.ShowDialog())
{
printDocument1.DocumentName = "Test Page Print";
printPreviewDialog1.Document = printDocument1;
if (DialogResult.OK == printPreviewDialog1.ShowDialog())
printDocument1.Print();
}
}
This is the button to initialize the print_document. (I added a print preview so that i don't have to print it every time and spent paper and ink)
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Bitmap bm = new Bitmap(this.panel1.Width, this.panel1.Height);
this.panel1.DrawToBitmap(bm, new Rectangle(50, 50, this.panel1.Width + 50, this.panel1.Height + 50));
e.Graphics.DrawImage(bm, 0, 0);
}
I was thinking that maybe the chart is too big and it doesn't fit in the page but if i save the bitmap. it fits OK inside the page actually there is too much free space on the bottom and right side. (After the draw to bitmap function add
bm.Save(#"c:\LOAN_GRAPH.png");
instead of the draw image and the image is save in c:)
Anybody who can help me i would be truly thankful.
Its working.
I removed the panel and instead, i am creating the rectangle according to my chart.
Bitmap bm = new Bitmap(this.chart1.Width, this.chart1.Height);
this.chart1.DrawToBitmap(bm, new Rectangle(50, 50, this.chart1.Width + 50, this.chart1.Height + 50));
e.Graphics.DrawImage(bm, 0, 0);
I am able to print a chart from my c# project using:
chart1.Printing.PrintDocument.DocumentName = "Graph of data";
But is it possible to add a title to this? I was hoping the document name would achieve this, but apparently not!
You can print whatever you want directly to the page and then invoke the chart PrintPaint(). Note that if you don't switch the PageUnit to Pixels that the chart scaling gets confused.
void PrintChart(object sender, PrintPageEventArgs ev)
{
using (var f = new System.Drawing.Font("Arial", 10))
{
var size = ev.Graphics.MeasureString(Text, f);
ev.Graphics.DrawString("Whatever text you want", f, Brushes.Black, ev.PageBounds.X + (ev.PageBounds.Width - size.Width) / 2, ev.PageBounds.Y);
}
//Note, the chart printing code wants to print in pixels.
Rectangle marginBounds = ev.MarginBounds;
if (ev.Graphics.PageUnit != GraphicsUnit.Pixel)
{
ev.Graphics.PageUnit = GraphicsUnit.Pixel;
marginBounds.X = (int)(marginBounds.X * (ev.Graphics.DpiX / 100f));
marginBounds.Y = (int)(marginBounds.Y * (ev.Graphics.DpiY / 100f));
marginBounds.Width = (int)(marginBounds.Width * (ev.Graphics.DpiX / 100f));
marginBounds.Height = (int)(marginBounds.Height * (ev.Graphics.DpiY / 100f));
}
chart1.Printing.PrintPaint(ev.Graphics, marginBounds);
}
This menu handler opens a PrintDialog(). If you don't want a dialog you can just call pd.Print().
private void printToolStripMenuItem_Click(object sender, EventArgs e)
{
var pd = new System.Drawing.Printing.PrintDocument();
pd.PrintPage += new PrintPageEventHandler(PrintChart);
PrintDialog pdi = new PrintDialog();
pdi.Document = pd;
if (pdi.ShowDialog() == DialogResult.OK)
pdi.Document.Print();
}
Here is a workaround solution to your problem, if you place the ChartingControl inside a Panel control on the Windows Form. You can then print the panel, inside the panel you can add the document heading as a label and whatever other stuff you want to add.
Firstly from the toolbox add a PrintDocument control and call it MyPrintDocument
Then add a Panel control and put your chart inside it.
Make sure you have imported the System.Drawing namespace, then you can print the panel like this.
Bitmap MyChartPanel = new Bitmap(panel1.Width, panel1.Height);
panel1.DrawToBitmap(MyChartPanel, new Rectangle(0, 0, panel1.Width, panel1.Height));
PrintDialog MyPrintDialog = new PrintDialog();
if (MyPrintDialog.ShowDialog() == DialogResult.OK)
{
System.Drawing.Printing.PrinterSettings values;
values = MyPrintDialog.PrinterSettings;
MyPrintDialog.Document = MyPrintDocument;
MyPrintDocument.PrintController = new System.Drawing.Printing.StandardPrintController();
MyPrintDocument.Print();
}
MyPrintDocument.Dispose();
This code converts the panel into a Bitmap and then prints that Bitmap.
You could condense this into a function like:
public void PrintPanel(Panel MyPanel)
{
// Add code from above in here, changing panel1 to MyPanel...
}