0

I'm working on a few tools that require printed output for the user, and I have had some good results with the System.Drawing and System.Graphics classes to layout the document to be printed, but I'm needing more page layout features.

The code I've used and had good results with so far is here:

 //create the Print Dialog
    PrintDialog pd = new PrintDialog();
    //Create Document to Print
    PrintDocument doc = new PrintDocument();
    //Add document to dialog to print
    pd.Document = doc;
    //Adds the contents of the page
    doc.PrintPage += new PrintPageEventHandler(pd_printpage);
    //Launch the Print Dialog
    DialogResult res = pd.ShowDialog();
    //if everything is okay with the dialog, Print it.
    if(res == DialogResult.OK)
    {
        doc.DefaultPageSettings.Landscape = false;
        doc.PrinterSettings.DefaultPageSettings.PaperSize = new PaperSize("Env10", 4, 8);
        doc.Print();
    }

with the actual printing here

//Generate a Graphics object
    Graphics gfx = e.Graphics;
    //Set the Font to print with
    Font courier = new Font("Courier New", 12);
    //check to see if data is not set to default.
    if (check_text())
    {
        //if the manual radio is selected:
        if (rd_ManRcpt.Checked == true)
        {
            //Place Amount on page.
            gfx.DrawString(tx_amt.Text, courier, new SolidBrush(Color.Black), 55, 80);
            //Place Date on page
            gfx.DrawString(tx_date.Text, courier, new SolidBrush(Color.Black), 35, 105);
            //Place Serial number to page
            gfx.DrawString(tx_No.Text, courier, new SolidBrush(Color.Black), 250, 105);
            //place contributer name to page.
            gfx.DrawString(tx_CredTo.Text, courier, new SolidBrush(Color.Black), 75, 130);
            //Place Address Information on Page
            gfx.DrawString(tx_add1.Text + "/n" + tx_add2.Text + "/n" + tx_city.Text + ", " + tx_state.Text + " " + tx_zip.Text, courier, new SolidBrush(Color.Black), 75, 200);
        }
        else if (rd_BDRcpt.Checked == true)
        {
            gfx.DrawString("$40.75", courier, new SolidBrush(Color.Black), 515, 10);
            gfx.DrawString("03/13/2017", courier, new SolidBrush(Color.Black), 625, 105);
            gfx.DrawString("XXXXXXX", courier, new SolidBrush(Color.Black), 625, 165);
            gfx.DrawString("XXXXXX", courier, new SolidBrush(Color.Black), 625, 215);
            gfx.DrawString("Contributer Here", courier, new SolidBrush(Color.Black), 75, 175);
            gfx.DrawString("Address Information Here.", courier, new SolidBrush(Color.Black), 75, 195);
        }

The code here is just to generate one of two receipts and print them. I'm not sure how to define the page size and orientation where the printer will print on the size 10 envelope page.

The other part of this question is how to fully control the layout of the document being printed or if possible, use HTML to control the layout as I'm very familiar and I'm sure others would be interested in this technique as well.

Chris Rutherford
  • 1,592
  • 3
  • 22
  • 58
  • Have you tried designing the layout in Crystal Reports instead? – Ron Beyer Mar 15 '17 at 16:43
  • that sounds like trying to build a Popsicle stand by travelling to China to see how the great wall was built. – Chris Rutherford Mar 15 '17 at 16:48
  • The method you are using was designed to be very rigid, if you want flexibility such as adjusting paper size, page orientation, etc you need to either modify your `Draw` calls by adjusting to the paper size, or use something more flexible like Crystal Reports. It may look daunting from the outside, but it really isn't all that bad. There are other report generation frameworks as well if you don't want something as powerful. – Ron Beyer Mar 15 '17 at 16:54
  • Apologies for making the overblown comparison. We're a non-profit, and don't use the Crystal Reports setup. I just think it would be counter productive to learn something that needs so much overhead and setup for a simple app that gives the user just a form to fill in and a "print" button. We may want to integrate this into other systems, but not at this point. Also, the dimensions and items printed don't change, and will always be under a page. – Chris Rutherford Mar 15 '17 at 17:00
  • If you are using WPF another method would be to create a form with TextBoxes/Labels/etc that can be populated with your data & use the PrintDialog PrintVisual method. http://www.c-sharpcorner.com/uploadfile/mahesh/printing-a-control-in-wpf-using-C-Sharp/ You can obtain the PrintCapabilities of the selected printer to scale & set landscape/portrait mode. – PaulF Mar 15 '17 at 17:01
  • It sounds like you don't need the data interactive, correct? If you are familiar with html, why not process your data into either xml or html and then a framework to adjust the display of it? – William Karnesky Mar 15 '17 at 17:04
  • Let me know if you need any tips - it's the technique we use for printing serial numbered product labels. It has the advantage of being able to create the form in Visual Studio & display it as a "popup window/print preview" prior to printing. – PaulF Mar 15 '17 at 17:06
  • William, Yes! that sounds like a good solution as part of one of the documents is based on XML data already, and the other could easily be modified to fit! Paul, that could work as well. i like it and I don't necessarily want the document to be saved to file either. – Chris Rutherford Mar 15 '17 at 17:28
  • Quick demo program here : https://drive.google.com/file/d/0ByggNzCjcvtmdXpuTTNUbmJZU3c – PaulF Mar 16 '17 at 11:13
  • Obviously needs more work - but does show the basic principles. The separate form is not a necessity the grid could just as easily be part of the MainWindow if it better suited your purposes - it doesn't need to be a grid - any Visual container object (StackPanel/DockPanel/Canvas etc) will work.. – PaulF Mar 16 '17 at 13:30

0 Answers0