I'm using this code to print a collection of strings from a Rich Text Box:
private void printBtn_Click(object sender, EventArgs e)
{
PrintDocument p = new PrintDocument();
p.OriginAtMargins = true;
Margins pMargins = new Margins(100, 100, 100, 100);
p.DefaultPageSettings.Margins = pMargins;
p.PrintPage += delegate (object sender1, PrintPageEventArgs e1)
{
e1.Graphics.DrawString(summaryBox.Text, new Font("Times New Roman", 12), new SolidBrush(Color.Black), new RectangleF(0, 0, p.DefaultPageSettings.PrintableArea.Width, p.DefaultPageSettings.PrintableArea.Height));
};
try
{
p.Print();
this.Close();
}
catch (Exception ex)
{
throw new Exception("Error During Printing", ex);
}
}
However, long strings get cut off and don't automatically. Strings cut off to the right, and after the first page is filled, a second page is not printed and leftover content is simply ignored. Are the problems with the printer setup or with the rich text box or both? How can I make sure long strings wrap and second or third pages are printed if the content is too long?
You need to use PrintPageEventArgs.MarginBounds to get printable area within a print page.
Now your e1 is the parameter of type PrintPageEventArgs. and you will get MarginBounds like e1.MarginBounds.
So your code will be.
p.PrintPage += delegate (object sender1, PrintPageEventArgs e1)
{
e1.Graphics.DrawString(summaryBox.Text, new Font("Times New Roman", 12), new SolidBrush(Color.Black), new RectangleF(0, 0, e1.MarginBounds.Width, e1.MarginBounds.Height));
};
Related
I am implementing PrintDocument in my asp.net project wherein i need to print specific pages of my document but instead of document being printed, its the string of "fileName" which is getting printed. Below is my code
string fileName = "C:\\DocToPrint\\Sample2018.pdf";
protected void Page_Load(object sender, EventArgs e)
{
using (PrintDocument pd = new PrintDocument())
{
pd.PrinterSettings.FromPage = 1;
pd.PrinterSettings.ToPage = 1;
pd.PrinterSettings.PrintRange = PrintRange.SomePages;
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
pd.Print();
}
public void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
ev.Graphics.DrawString(fileName, new Font("Arial", 10), Brushes.Black,
ev.MarginBounds.Left, 0, new StringFormat());
}
}
What is being missed here? Please suggest..Thanks
There's a nuget package called Spire.Pdf that's very simple to use. The free version has a limit of 10 pages although, however, in my case it was the best solution once I don't want to depend on Adobe Reader and I don't want to install any other components.
https://www.nuget.org/packages/Spire.PDF/
PdfDocument pdfdocument = new PdfDocument();
pdfdocument.LoadFromFile(pdfPathAndFileName);
pdfdocument.PrinterName = "My Printer";
pdfdocument.PrintDocument.PrinterSettings.Copies = 1;
pdfdocument.PrintFromPage = index;
pdfdocument.PrintToPage = index;
pdfdocument.PrintDocument.Print();
pdfdocument.Dispose();
Sample for printing some page.
https://www.e-iceblue.com/forum/print-one-page-from-pdf-t6586.html
I am using a PrintDialog and PrintDocument in c# windows application, WinForms, to print a document.
Printing working fine but I want to display the document before printing, so that I can check if PointF for DrawString is like I want it.How I can do that? Is there any tool that make it easy to define a pointF on A4 document?
private void buttonPrintShows_Click(object sender, EventArgs e)
{
PrintDialog pd = new PrintDialog();
pd.Document = printDocumentStatistic;
if (pd.ShowDialog() == DialogResult.OK)
{
printDocumentStatistic.Print();
}
}
private void printDocumentStatistic_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawString("Shows:",new System.Drawing.Font("Arial", 20f), new SolidBrush(Color.Red), new PointF(35, 50));
e.Graphics.DrawString("Act:", new System.Drawing.Font("Arial", 20f), new SolidBrush(Color.Red), new PointF(35, 75));
e.Graphics.DrawString(show_name, new System.Drawing.Font("Arial", 20f), new SolidBrush(Color.Red), new PointF(100, 50));
e.Graphics.DrawString(akt_name, new System.Drawing.Font("Arial", 20f), new SolidBrush(Color.Red), new PointF(100, 75));
}
There is a simple way. Add a printPreviewDialog and give your document path to it.
printPreviewDialog1.Document = printDocument1;
You will find more info here.
How do I print out a String that I generated in Winforms? The String I'd like to print out is located in a UserControl.
This is what I already have. When I press the Printing Button, nothing is printed.
private void print_Click(object sender, EventArgs e)
{
PrintDialog printDialog = new PrintDialog();
PrintDocument printDocument = new PrintDocument();
printDialog.Document = printDocument;
printDocument.PrintPage += new PrintPageEventHandler(printDocument_PrintPage);
DialogResult result = printDialog.ShowDialog();
if (result == DialogResult.OK)
{
printDocument.Print();
}
PrintDocument recordDoc;
// Create the document and name it
recordDoc= new PrintDocument();
recordDoc.DocumentName = "Customer Receipt";
recordDoc.PrintPage += new PrintPageEventHandler(this.PrintReceiptPage);
// Preview document
dlgPreview.Document = recordDoc;
dlgPreview.ShowDialog();
// Dispose of document when done printing
recordDoc.Dispose();
}
In the PrintPage event try this
e1.Graphics.DrawString(s, new Font("Times New Roman", 12), new SolidBrush(Color.Black), new RectangleF(0, 0, p.DefaultPageSettings.PrintableArea.Width, p.DefaultPageSettings.PrintableArea.Height));
try
{
p.Print();
}
catch (Exception ex)
{
throw new Exception("Exception Occured While Printing", ex);
}
taken from https://social.msdn.microsoft.com/Forums/en-US/93e54c4f-fd07-4b60-9922-102439292f52/c-printing-a-string-to-printer?forum=csharplanguage
I am using ExportToXlsx() method in order to export data from GridView to Excel Sheet.
this seams to export and format the data just fine, even my conditional formatting is being exported as is.
The only thing I would like to know is that how do I add a logo and header in that excel file without having to add it manually. Like in a Crystal Report we can add these things at the design time. Is there something that can be done ?
You can do that with PrintableComponentLink and write a CreateReportHeaderArea event handler:
private void button_Export_Click(object sender, EventArgs e)
{
var y = new DevExpress.XtraPrinting.PrintableComponentLink(new PrintingSystem());
y.Component = gridControl1;
y.CreateReportHeaderArea += y_CreateReportHeaderArea;
if (saveFileDialog_Report.ShowDialog() == DialogResult.OK)
{
if (saveFileDialog_Report.FileName.ToLower().EndsWith("xlsx"))
{
y.ExportToXlsx(saveFileDialog_Report.FileName);
}
}
}
void y_CreateReportHeaderArea(object sender, CreateAreaEventArgs e)
{
DevExpress.XtraPrinting.TextBrick brick;
brick = e.Graph.DrawString("My Report Title Here", Color.Navy, new RectangleF(0, 0, 500, 40), DevExpress.XtraPrinting.BorderSide.None);
brick.Font = new Font("Arial", 16);
brick.StringFormat = new DevExpress.XtraPrinting.BrickStringFormat(StringAlignment.Center);
}
In addition to Milen's answer that added the header, I was also able to add an image to the report like below
void y_CreateReportHeaderArea(object sender, CreateAreaEventArgs e)
{
Image newimage = Image.FromFile("path");
DevExpress.XtraPrinting.ImageBrick iBrick;
iBrick = e.Graph.DrawImage(newimage, new RectangleF(0,0,40,40));
DevExpress.XtraPrinting.TextBrick brick;
brick = e.Graph.DrawString("My Report Title Here", Color.Navy, new RectangleF(40, 0, 500, 40), DevExpress.XtraPrinting.BorderSide.None);
brick.Font = new Font("Arial", 16);
brick.StringFormat = new DevExpress.XtraPrinting.BrickStringFormat(StringAlignment.Center);
}
just make sure that you keep the coordinates and size correct so the image and text dont overlap.
I have the following method that loads in a blank template image, draws the relevant information on it and saves it to another file. I want to change this slightly to achieve the following:
load in the template image
draw the relevant information on it
print it
I don't want to save it, just print it out. Here's my existing method:
public static void GenerateCard(string recipient, string nominee, string reason, out string filename)
{
// Get a bitmap.
Bitmap bmp1 = new Bitmap("template.jpg");
Graphics graphicImage;
// Wrapped in a using statement to automatically take care of IDisposable and cleanup
using (graphicImage = Graphics.FromImage(bmp1))
{
ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);
// Create an Encoder object based on the GUID
// for the Quality parameter category.
Encoder myEncoder = Encoder.Quality;
graphicImage.DrawString(recipient, new Font("Arial", 10, FontStyle.Regular), SystemBrushes.WindowText, new Point(480, 33));
graphicImage.DrawString(WordWrap(reason, 35), new Font("Arial", 10, FontStyle.Regular), SystemBrushes.WindowText, new Point(566, 53));
graphicImage.DrawString(nominee, new Font("Arial", 10, FontStyle.Regular), SystemBrushes.WindowText, new Point(492, 405));
graphicImage.DrawString(DateTime.Now.ToShortDateString(), new Font("Arial", 10, FontStyle.Regular), SystemBrushes.WindowText, new Point(490, 425));
EncoderParameters myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 100L);
myEncoderParameters.Param[0] = myEncoderParameter;
filename = recipient + " - " + DateTime.Now.ToShortDateString().Replace("/", "-") + ".jpg";
bmp1.Save(filename, jgpEncoder, myEncoderParameters);
}
}
Hope you can help,
Brett
Just print it to the printer without saving
This is the most simple example I could come up with.
Bitmap b = new Bitmap(100, 100);
using (var g = Graphics.FromImage(b))
{
g.DrawString("Hello", this.Font, Brushes.Black, new PointF(0, 0));
}
PrintDocument pd = new PrintDocument();
pd.PrintPage += (object printSender, PrintPageEventArgs printE) =>
{
printE.Graphics.DrawImageUnscaled(b, new Point(0, 0));
};
PrintDialog dialog = new PrintDialog();
dialog.ShowDialog();
pd.PrinterSettings = dialog.PrinterSettings;
pd.Print();
When you use the PrintDocument class, you can print without needing to save the image.
var pd = new PrintDocument();
pd.PrintPage += pd_PrintPage;
pd.Print()
And in the pd_PrintPage eventhandler:
void pd_PrintPage(object sender, PrintPageEventArgs e)
{
Graphics gr = e.Graphics;
//now you can draw on the gr object you received using some of the code you posted.
}
NOTE: Don't dispose the Graphics object you received in the eventhandler. This is done by the PrintDocument object itself...
Use the PrintPage event;
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) {
e.Graphics.DrawImage(image, 0, 0);
}