I want to do very common task in C# but I cannot figure how: My application will generate document with a lot of text and some pictures, let user preview the result and then let him print it.
What is the easiest way to do it? I take the text that I put to the document from database.
Remarks:
I do not care if the GUI will be WPF or Windows Forms.
I prefer some ready-to-use component for displaying preview, I don't want to create my own.
Preview must be exactly the same as the printed result.
Nowbody seems to be able to answer https://stackoverflow.com/questions/4634445/how-to-work-with-fixedpage
Maybe this example will help you. This is actually based on WindowsForms and comes partially from MSDN. Use the below code like:
using (Printer p = new Printer(this.richTextBox.Text, 1)) { }
Here it takes text from the richTextBox, but you can put any string there.
Create a new Form in your application and add the following code:
using System;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Printing;
namespace PrinterExample
{
public partial class Printer : Form
{
private string textToDisplay;
private Font printFont;
private StreamReader streamToPrint;
private int mode;
//mode 1 - Preview, 2 - Print
public Printer(string textToDisplay,int mode)
{
this.textToDisplay = textToDisplay;
this.mode = mode;
InitializeComponent();
PreviewPage();
}
internal void PreviewPage()
{
try
{
streamToPrint = new StreamReader(new MemoryStream(Encoding.ASCII.GetBytes(textToDisplay)));
printFont = DefaultFont;
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler
(this.pd_PrintPage);
var ppd = new PrintPreviewDialog();
ppd.Document = pd;
if (mode == 1) ppd.Show();
if (mode == 2) pd.Print();
}
catch
{
MessageBox.Show("Exception occured", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
float linesPerPage = 0;
float yPos = 0;
int count = 0;
float leftMargin = ev.MarginBounds.Left;
float rightMargin = ev.MarginBounds.Right;
float topMargin = ev.MarginBounds.Top;
string line = null;
// Calculate the number of lines per page.
linesPerPage = ev.MarginBounds.Height /
printFont.GetHeight(ev.Graphics);
float charsPerLine = (rightMargin - leftMargin) / (printFont.GetHeight(ev.Graphics)*0.65f);
// Print each line of the file.
while (count < linesPerPage &&
((line = streamToPrint.ReadLine()) != null))
{
string newLine = null;
int newLineCounter = 0;
for (int i = 0; i < line.Length; i++)
{
if (i % (int)charsPerLine == 0)
{
newLine = line.Substring((int)charsPerLine * newLineCounter, (int)charsPerLine > (line.Length - (int)charsPerLine * newLineCounter) ? (line.Length - (int)charsPerLine * newLineCounter) : (int)charsPerLine);
yPos = topMargin + (count *
printFont.GetHeight(ev.Graphics));
ev.Graphics.DrawString(newLine, printFont, Brushes.Black,
leftMargin, yPos, new StringFormat());
count++;
newLineCounter++;
}
}
newLineCounter = 0;
}
// If more lines exist, print another page.
if (line != null)
ev.HasMorePages = true;
else
ev.HasMorePages = false;
}
private void Printer_FormClosing(object sender, FormClosingEventArgs e)
{
this.streamToPrint.Close();
}
}
Be aware that for proffesional printing most people use external tools like Crystal Reports. I am not sure if you can modify this example to print images.
One option is to use built-in .rdlc reports for you task
http://msdn.microsoft.com/en-us/library/ms252067(v=VS.90).aspx
Drasto,
I created a rather complex custom printing tool that might be useful.
PrintPage PrintPageEventHandler Is Printing Too Many Copies
Feel free to steal as much of that code of mine as you want.
Related
I'm building a series of apps for my place of business so I'm trying to create my own printing class that I can refer to for all of my applications.
The issue is, I'm trying to figure out a way for the application to tell the class when to print the page, and I am unable to find a way to do so.
For example, this is what I have so far:
// Report Variables
private bool bPrinting = false;
private int iPage = 0;
private float fOverflow = 0.00F;
private string sPrintLine = null;
private Font fontTmpFont = null;
private PrintPageEventArgs ppeaEv = null;
private Margins mMargins = new System.Drawing.Printing.Margins(25, 25, 25, 25); // Set wide margins
// Clear the print line
public void LineClear()
{
sPrintLine = null;
}
// Insert a string into the print line at the specified position within the line
public void LineInsert(string _InsertString, int _InsertPosition)
{
if (sPrintLine.Length <= _InsertPosition)
sPrintLine = sPrintLine.PadLeft(_InsertPosition) + _InsertString;
else if (sPrintLine.Length <= (_InsertPosition + _InsertString.Length))
sPrintLine = sPrintLine.Substring(0, _InsertPosition) + _InsertString;
else
sPrintLine = sPrintLine.Substring(0, _InsertPosition) + _InsertString + sPrintLine.Substring(_InsertPosition + _InsertString.Length);
}
// Check to see if the line we're trying to print is at the end of the page
public bool AtEndOfPage()
{
return AtEndOfPage(new Font("Courier", 10));
}
public bool AtEndOfPage(Font _Font)
{
if ((fOverflow + _Font.GetHeight(ppeaEv.Graphics)) > ppeaEv.MarginBounds.Height)
return true;
else
return false;
}
// Attempt to print the line
public void LinePrint()
{
LinePrint(null, null);
}
public void LinePrint(Font _Font)
{
LinePrint(_Font, null);
}
public void LinePrint(Font _Font, Brush _Color)
{
if (_Font == null)
_Font = new Font("Courier", 10);
if (_Color == null)
_Color = Brushes.Black;
ppeaEv.Graphics.DrawString(sPrintLine, _Font, _Color,
ppeaEv.MarginBounds.Left, ppeaEv.MarginBounds.Top + fOverflow,
new StringFormat()); // 'Draw' line on page
fOverflow += _Font.GetHeight(ppeaEv.Graphics);
}
// We are done with the report, tell the Print Service to finish up
public void EndReport()
{
ppeaEv.HasMorePages = false;
}
// This is what gets called when the user clicks on 'Print'
private void Print_Click(object sender, EventArgs e)
{
PrintDocument printDocument = new PrintDocument();
printDocument.DefaultPageSettings.Margins = mMargins; // Set margins for pages
printDocument.DefaultPageSettings.Landscape = false; // Set Portrait mode
printDocument.BeginPrint += new PrintEventHandler(printDocument_BeginPrint);
printDocument.EndPrint += new PrintEventHandler(printDocument_EndPrint);
PrintDialog printDialog = new PrintDialog();
printDialog.Document = printDocument; // Set the Document for this print dialog
printDialog.AllowSomePages = true; // Allow the user to select only some pages to print
printDialog.ShowHelp = true; // Allow help button
DialogResult result = printDialog.ShowDialog();
if (result == DialogResult.OK)
{
printDocument.Print(); // Raises PrintPage event
}
printDocument.Dispose();
printDialog.Dispose();
}
void printDocument_BeginPrint(object sender, PrintEventArgs e)
{
iPage = 0;
fOverflow = 0.00F;
}
void printDocument_EndPrint(object sender, PrintEventArgs e)
{
}
As you may be able to tell, I'm trying to fill the page externally to the class, and then have the app tell the class when to print the page when "AtEndOfPage"
I know about "printDocument.PrintPage", but that doesn't seem to be what I need; It builds the page internal to the method and then prints it. I'm not going to be building the print page within that method.
I also am trying to allow for multiple fonts on a single page.
Is there a way to do this?
Thank you all in advance,
Robert
I had been experimenting on writing a code to generate images inside a FlowLayoutPanel.
This is what i had done so far, when i click on a button for the first time (by using a checkboxes - read in number of images to open), it will generate the images, when i click on the button on second try, it will not update the flowlayoutpanel.
Even though i tried to remove the controls inside the FlowLayoutPanel, it still doesn't show the second try of the images.
This is the code snippet of the method:
FlowLayoutPanel fwPanel = null;
private void btnOpenFile_Click(object sender, EventArgs e)
{
//if there is content inside the flowpanel, dump it
if (fwPanel != null)
{
listOfFile.Clear();
}
//create a new FLP
fwPanel = new FlowLayoutPanel();
int panelWidth = width * 4 + 50;
int panelHeight = height * 4 + 50;
fwPanel.Size = new Size(panelWidth, panelHeight);
fwPanel.Location = new Point(0, 0);
this.Controls.Add(fwPanel);
//each checked item would be stored into an arraylist
foreach(object itemChecked in clbFile.CheckedItems)
{
listOfFile.Add((clbFile.Items.IndexOf(itemChecked)+1).ToString());
}
int noOfCheckedFile = listOfFile.Count;
PictureBox[] listOfPicture = new PictureBox[noOfCheckedFile];
int positionX = 0, positionY = 0;
int maxPaddingX = (width * MATRIX_SIZE) - 1;
int maxPaddingY = (height * MATRIX_SIZE) - 1;
//dynamically create images.
for (int i = 0; i < noOfCheckedFile; i++)
{
listOfPicture[i] = new PictureBox();
listOfPicture[i].Image = resizeImage((Image)show_picture(Convert.ToInt32(listOfFile[i])), new Size(width, height));
listOfPicture[i].Size = new Size(width, height);
if (positionX > maxPaddingX)
{
positionX = 0;
positionY += height;
}
if (positionY > maxPaddingY)
{
positionY = 0;
}
listOfPicture[i].Location = new Point(positionX,positionY);
listOfPicture[i].Visible = true;
fwPanel.Controls.Add(listOfPicture[i]);
positionX += width;
}
}
show_picture is a method that takes in and integer and returns a bitmap image.
listOfFile is to trace which file to return.
listOfPicture is to store each images.
i tried replacing this lines
//if there is content inside the flowpanel, dump it
if (fwPanel != null)
{
listOfFile.Clear();
}
i have added this line into it, when i do a second click, everything just gone missing, but this is not what i want, because it does not re-populating the FlowLayoutPanel.
if (fwPanel != null)
{
fwPanel.SuspendLayout();
if (fwPanel.Controls.Count > 0)
{
for (int i = (fwPanel.Controls.Count - 1); i >= 0; i--)
{
Control c = fwPanel.Controls[i];
c.Dispose();
}
GC.Collect();
}
fwPanel.ResumeLayout();
listOfFile.Clear();
}
I also tried this, but on second click, nothing will happen.
if (fwPanel != null)
{
List<Control> listControls = fwPanel.Controls.Cast<Control>().ToList();
foreach (Control control in listControls)
{
fwPanel.Controls.Remove(control);
control.Dispose();
}
listOfFile.Clear();
}
I wonder if i miss out anything, can someone enlighten me on what did i miss out ? Or guide me for the best practice of doing this.
as Suggested, i shifted the creation outside (credit to Sinatr for spotting it)
FlowLayoutPanel fwPanel = new FlowLayoutPanel();
private void createFLP()
{
int panelWidth = width * 4 + 50;
int panelHeight = height * 4 + 50;
fwPanel.Size = new Size(panelWidth, panelHeight);
fwPanel.Location = new Point(0, 0);
this.Controls.Add(fwPanel);
}
that solves the nothing happen part. Followed by using this to remove controls
if (fwPanel != null)
{
List<Control> listControls = fwPanel.Controls.Cast<Control>().ToList();
foreach (Control control in listControls)
{
fwPanel.Controls.Remove(control);
control.Dispose();
}
listOfFile.Clear();
}
and everything works like a charm, hope that this answer will be able to help others who are facing the same problem too.
I want to print the data from data grid. The code works well for the first page, but the commented lines do not works well and don't move to next page. Can anyone help fix this?
private void DrawFactorA4(object sender, PrintPageEventArgs ev)
{
for (int j = 0; j < GrdRDocument.Rows.Count; j++)
{
i += 2;
//draw data grid
s++;
if(s == 10)
{
//ev.HasMorePages = true; //this line doesn't work
s = 0;
i = 0;
}
else
{
ev.HasMorePages = false;
}
}
}
_
private void BtnPrint_Click(object sender, EventArgs e)
{
printFont = new Font("Arial", 12);
IEnumerable<PaperSize> paperSizes =
pd.PrinterSettings.PaperSizes.Cast<PaperSize>();
sizeA4 = paperSizes.First<PaperSize>(size => size.Kind == PaperKind.A4);
pd.DefaultPageSettings.Landscape = true;
pd.DefaultPageSettings.PaperSize = sizeA4;
pd.PrintPage += new PrintPageEventHandler(this.DrawFactorA4);
printPreviewDialog.Document = pd;
printPreviewDialog.ShowDialog();
}
Stop and read what you have:
printFont = new Font("Arial", 12);
Fonts are unmanaged resources; here you're instantating one and never disposing it. Maybe that's harmless in this particular situation, but this is a bad habit to get in to.
pd.PrintPage += new PrintPageEventHandler(this.DrawFactorA4);
DrawFactorA4 is going to be called for every page in your document. Inside DrawFactorA4:
for (int j = 0; j < GrdRDocument.Rows.Count; j++)
You iterate through every Row in GrdRDocument, regardless the number of rows or the size of your page. That is wrong; you have to stop after the page is filled. By the way, I hope GrdRDocument is a local copy of immutable data and you're not passing UI controls to the printing thread.
s++;
if(s == 10)
{
//ev.HasMorePages = true; //this line doesn't work
s = 0;
Your commented line would work fine. The problem is you set ev.HasMorePages = true and then ignore it; you set s = 0 and keep iterating; next iteration s!=10 so you:
ev.HasMorePages = false;
Read the PrintDocument docs; it has an example of printing more than one page. You should create a class to store all the unmanaged resources and page state. Make it IDisposable so they get disposed. Iterate through only the rows or whatever you want to print on each page. Something like:
class PrintStuff : IDisposable
{
readonly IEnumerable<Whatever> data;
readonly PrintDocument pd;
Font font;
private int currentIndex;
public PrintStuff(IEnumerable<Whatever> data)
{
this.data = data;
pd = new PrintDocument();
pd.BeginPrint += OnBeginPrint;
pd.PrintPage += OnPrintPage;
pd.EndPrint += OnEndPrint;
}
public void Print()
{
pd.Print();
}
public void Dispose()
{
pd.Dispose();
}
private void OnBeginPrint(object sender, PrintEventArgs args)
{
font = new Font(FontFamily.GenericSansSerif, 12F);
currentIndex = 0;
}
private void OnEndPrint(object sender, PrintEventArgs args)
{
font.Dispose();
}
private void OnPrintPage(object sender, PrintPageEventArgs args)
{
var x = Convert.ToSingle(args.MarginBounds.Left);
var y = Convert.ToSingle(args.MarginBounds.Top);
var lineHeight = font.GetHeight(args.Graphics);
while ((currentIndex < data.Count())
&& (y <= args.MarginBounds.Bottom))
{
args.Graphics.DrawWhatever(data.ElementAt(currentIndex), font, Brushes.Black, x, y);
y += lineHeight;
currentIndex++;
}
args.HasMorePages = currentIndex < data.Count();
}
}
I am using the PrintDocument class to print to my Brother label printer. When I execute the Print() method, the printer starts flashing a red error light, but everything else returns successful.
I can run this same code on my laser printer and everything works fine.
How can I see what is causing the error on my label printer?
Code:
public class Test
{
private Font printFont;
private List<string> _documentLinesToPrint = new List<string>();
public void Run()
{
_documentLinesToPrint.Add("Test1");
_documentLinesToPrint.Add("Test2");
printFont = new Font("Arial", 10);
var pd = new PrintDocument();
pd.DefaultPageSettings.Margins = new Margins(25, 25, 25, 25);
pd.DefaultPageSettings.PaperSize = new PaperSize("Label", 400, 237);
var printerSettings = new System.Drawing.Printing.PrinterSettings();
printerSettings.PrinterName ="Brother QL-570 LE";
pd.PrinterSettings = printerSettings;
pd.PrinterSettings.Copies = 1;
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
pd.Print();
}
// The PrintPage event is raised for each page to be printed.
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
float linesPerPage = 0;
float yPos = 0;
int count = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
string line = null;
// Calculate the number of lines per page.
linesPerPage = ev.MarginBounds.Height /
printFont.GetHeight(ev.Graphics);
// Print each line of the file.
while ((count < linesPerPage) && (count < _documentLinesToPrint.Count))
{
line = _documentLinesToPrint[count];
yPos = topMargin + (count *
printFont.GetHeight(ev.Graphics));
ev.Graphics.DrawString(line, printFont, Brushes.Black,
leftMargin, yPos, new StringFormat());
line = null;
count++;
}
// If more lines exist, print another page.
if (line != null)
ev.HasMorePages = true;
else
ev.HasMorePages = false;
}
}
PrintDocument is a very basic API. You get simple generic printing, but it comes at the cost of reduced functionality not specific to the print driver. My HP printer usually gives me a printed error rather than an Exception. Its not surprising to see something similar happening to you.
The blinking is likely a code that you can lookup. If that fails you can try saving to an Image format, PDF or XPS. Or use a 3rd party library or write your own PCL file. There's a ton of options. Creating an output you can view as opposed to one in memory should debugging calculations like margins. You can look at a PDF and see if it looks wacky. Just keep in mind the way it looks on the PC may be slightly different than the output especially when printing near the edges.
I could be completely wrong on this, but my understanding is that when you print with this code, it has nothing to do with the printer itself, but with the operating system. Windows sets up a print queue, places the output in it, and your code returns.
Then Windows takes items off of the queue and sends them through the printer driver and to your printer. If there's an error in printing, it should show up as a failed document in the print queue. I think it's too late to trap the error as an exception at this stage.
Please correct me if I am mistaken.
I would surround your the method bodies using a Try/Catch Block then handle the exception(s) within the catch of each method. As an example:
public class Test
{
private Font printFont;
private List<string> _documentLinesToPrint = new List<string>();
public void Run()
{
try
{
_documentLinesToPrint.Add("Test1");
_documentLinesToPrint.Add("Test2");
printFont = new Font("Arial", 10);
var pd = new PrintDocument();
pd.DefaultPageSettings.Margins = new Margins(25, 25, 25, 25);
pd.DefaultPageSettings.PaperSize = new PaperSize("Label", 400, 237);
var printerSettings = new System.Drawing.Printing.PrinterSettings();
printerSettings.PrinterName = "Brother QL-570 LE";
pd.PrinterSettings = printerSettings;
pd.PrinterSettings.Copies = 1;
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
pd.Print();
}
catch (InvalidPrinterException exc)
{
// handle your errors here.
}
catch (Exception ex)
{
// handle your errors here.
}
}
// The PrintPage event is raised for each page to be printed.
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
try
{
float linesPerPage = 0;
float yPos = 0;
int count = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
string line = null;
// Calculate the number of lines per page.
linesPerPage = ev.MarginBounds.Height /
printFont.GetHeight(ev.Graphics);
// Print each line of the file.
while ((count < linesPerPage) && (count < _documentLinesToPrint.Count))
{
line = _documentLinesToPrint[count];
yPos = topMargin + (count *
printFont.GetHeight(ev.Graphics));
ev.Graphics.DrawString(line, printFont, Brushes.Black,
leftMargin, yPos, new StringFormat());
line = null;
count++;
}
// If more lines exist, print another page.
if (line != null)
ev.HasMorePages = true;
else
ev.HasMorePages = false;
}
catch (InvalidPrinterException exc)
{
// handle your errors here.
}
catch (Exception ex)
{
// handle your errors here.
}
}
}
I am trying add a page when horizontal or the x position is greater than a counter in order to keep a right side margin. When I run the code I end up in an infinate loop of hundreds of pages all displaying the same first page graphics. Thinking it might have to do with my lack of understanding HasMorePages. I could use some help. Thanks.
public static class PrintWave
{
public static void PrintPreWave()
{
PrintDocument pd = new PrintDocument();
if (WaveTools.MySettings == null)
{
pd.DefaultPageSettings.Landscape = true;
}
else
{
pd.DefaultPageSettings = WaveTools.MySettings;
}
pd.OriginAtMargins = true;
pd.PrintPage += new PrintPageEventHandler(OnPrintPage);
PrintDialog dlg = new PrintDialog();
PrintPreviewDialog printPreviewDlg = new PrintPreviewDialog();
printPreviewDlg.Document = pd;
Form p = (Form)printPreviewDlg;
p.WindowState = FormWindowState.Maximized;
printPreviewDlg.ShowDialog();
}
private static void OnPrintPage(object sender, PrintPageEventArgs e)
{
string MyTag = string.Empty;
MyTag = WaveActions.ActiveId;
Wave MyWave = WaveHolder.FindWave(MyTag);
int MyCount = 0;
int xOffset = e.MarginBounds.Location.X;
int yOffset = e.MarginBounds.Location.Y;
if (MyWave != null)
{
Graphics g = e.Graphics;
g.SetClip(e.PageBounds);
Pen MyPen = new Pen(WaveTools.WaveColor, WaveTools.PenWidth);
float dx = (float)e.PageBounds.Width / MyWave.NumSamples;
float dy = (float)e.PageBounds.Height / 255;
if (MyWave.Normal == false)
{
g.ScaleTransform(dx, dy);
}
for (int i = 0; i < MyWave.NumSamples - 1; i++)
{
g.DrawLine(MyPen, i, MyWave.Data[i], i + 1, MyWave.Data[i + 1]);
MyCount = MyCount + 1;
if (MyCount > e.MarginBounds.Width)
{
e.HasMorePages = true;
MyCount = 0;
return;
}
else
{
e.HasMorePages = false;
return;
}
}
}
}
}
}
for (int i = 0; i < MyWave.NumSamples - 1; i++)
That's the core problem statement, you start at 0 every time PrintPage gets called. You need to resume where you left off on the previous page. Make the i variable a field of your class instead of a local variable. Implement the BeginPrint event to set it to zero.
The else clause inside the loop need to be deleted.