如何在PrintDocument C#

时间:2018-10-24 10:58:09

标签: c# .net winforms datagridview printdocument

大家好,我有一个带有DataGridView的WinForm和一些控件。我想从此DataGridViewPrintDocument打印特定的列。我设计了一种带有硬编码标题(如Sr.,Qty,Rate等)的帐单格式。

可能有足够的数据用于多个页面,所以我希望此标题显示在每页顶部,我也想添加页码,在打印完所有行之后,在页面末尾有总计,折扣和应付金额计算我想确保它出现在最后一页的末尾。

编辑:我已成功从选定的列中打印出来,因为我不是学习者,我不知道它是否会在A4以外运行。我想知道Bill Calculation部分如果有很多页面,该部分将在drawDataGridTab2()之后触发吗? 如果您能帮助我了解如何使A4以外的纸张打印而不丢失格式,那该如何做? 贝娄,我正在粘贴新的帐单格式代码。

private void printDocumentTab2_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {

        //Draw Header

        ////
        Graphics graphic = e.Graphics;

        Font font = new Font("Courier New", 12);

        float fontHeight = font.GetHeight();
        graphic.DrawString(txtSupplierTab2.Text, new Font("Courier New", 20), Brushes.Black, 20, 20);
        graphic.DrawString(txtRichAddressTab2.Text, new Font("Courier New", 12), Brushes.Black, 20, 50);
        graphic.DrawString(txtCityTab2.Text, new Font("Courier New", 12), Brushes.Black, 20, 105);
        graphic.DrawString("Ph: " + txtContactTab2.Text, new Font("Courier New", 12), Brushes.Black, 20, 120);
        graphic.DrawLine(new Pen(Color.Blue, 2), 0, 140, this.Width, 140);
        //Call method to Draw Headers
        drawHeaderTab2(new Font("Courier New", 12, FontStyle.Bold), graphic);
        graphic.DrawLine(new Pen(Color.Blue, 2), 0, 165, this.Width, 165);
        //Call method to Draw DataGrid
        drawDataGridTab2(new Font("Courier New", 10, FontStyle.Bold), graphic);
        //Bill Calucation
        graphic.DrawLine(new Pen(Color.Blue, 2), 0, 900, this.Width, 900);

        //Gross Total at the end of Bill
        graphic.DrawString("Total", new Font("Courier New", 12), Brushes.DarkRed, 600, 905);
        graphic.DrawString(txtGrossTotal_Tab2.Text, new Font("Courier New", 12), Brushes.DarkRed, 700, 905);
        //Discount At the End of Bill
        graphic.DrawString("Disc.", new Font("Courier New", 12), Brushes.DarkRed, 600, 925);
        graphic.DrawString(txtGrossTotalDisc_Tab2.Text, new Font("Courier New", 12), Brushes.DarkRed, 700, 925);
        //Payable at the end of the bill
        graphic.DrawLine(new Pen(Color.Black, 1.5f), 600, 945, this.Width, 945);
        graphic.DrawString("Payable", new Font("Courier New", 12), Brushes.DarkRed, 600, 945);
        graphic.DrawString(lblGrandAmmount_Tab2.Text, new Font("Courier New", 12), Brushes.DarkRed, 700, 945);
    }

DrawHeader函数

private void drawHeaderTab2(Font font, Graphics g)
    {
        g.DrawString("Sr#", font, Brushes.Black, 20, 145);
        string medicine = dataGridView1.Columns["medName"].HeaderText;
        g.DrawString(medicine, font, Brushes.Black, 70, 145);
        string qty = dataGridView1.Columns["purchasedQty"].HeaderText;
        g.DrawString(qty, font, Brushes.Black, 500, 145);
        string rate = dataGridView1.Columns["costPrice"].HeaderText;
        g.DrawString(rate, font, Brushes.Black, 600, 145);
        string amount = dataGridView1.Columns["grossTotal"].HeaderText;
        g.DrawString(amount, font, Brushes.Black, 750, 145);

    }

DrawDataGridView功能

private void drawDataGridTab2(Font font, Graphics g)
    {
        int yCord = 170;
        int y1 = 185;
        int y2 = 185;
        int rows = 1;
        foreach (DataGridViewRow dr in dataGridView1.Rows)
        {
            //g.DrawString(dr.ToString(), new Font("Courier New", 10), Brushes.Black, 10, 150);
            g.DrawString(rows.ToString(), font, Brushes.Black, 20, yCord);
            string medicine = dataGridView1.Rows[dr.Index].Cells["medName"].FormattedValue.ToString();
            g.DrawString(medicine, font, Brushes.Black, 70, yCord);
            string qty = dataGridView1.Rows[dr.Index].Cells["purchasedQty"].FormattedValue.ToString();
            g.DrawString(qty, font, Brushes.Black, 500, yCord);
            string rate = dataGridView1.Rows[dr.Index].Cells["costPrice"].FormattedValue.ToString();
            g.DrawString(rate, font, Brushes.Black, 600, yCord);
            string amount = dataGridView1.Rows[dr.Index].Cells["grossTotal"].FormattedValue.ToString();
            g.DrawString(amount, font, Brushes.Black, 750, yCord);
            g.DrawLine(new Pen(Color.Black, 1), 0, y1, this.Width, y2);
            yCord += 15;
            y1 += 15;
            y2 += 15;
            rows++;
        }
    }

1 个答案:

答案 0 :(得分:0)

我刚刚创建了一个要打印的模块。它需要更多的微调才能使其完美。

const jwt = require('jsonwebtoken');
const router = express.Router();
const User = require("../models/user");
const Book = require("../models/book");
const router = express.Router();


router.use('/books', passport.authenticate('jwt', { session: false }));

router.param('bookId', (req, res, next, id) => {
   Book.findById(id, (e, book) => {
     if (e) return next(e);
     // Simplified for 'not found', here req.book will just be null.  you can make custom error if you want
     req.book = book;
     return next();
   });
});

router.route('/books/:bookId')
   .get((req, res) => {
      if (req.book) return res.json(book);
      return res.status(404).json({ msg: 'Book not found' });
   });
// similarly you can add delete and put methods here if you want, and not have to req-query for the book.  

router.route('/books')
   .post((req, res) => {
      const { isbn, title, author, publisher } = req.body
      const newBook = new Book({ isbn, title, author, publisher });

      newBook.save((err) => {
        if (err) return next(err); // note, custom errors can be handy here
        // this returns HTTP 201, which is a success, and the newBook which includes its ID
        return res.status(201).json(newBook);
      });
  }).get((req, res) => {
      Book.find((err, books) => {
        if (err) return next(err);
        res.json(books);
      });
  });

module.exports = router;

按钮事件

public class BillPrintController : StandardPrintController
{
    private readonly DataGridViewRow[] rows;
    private readonly Font printFont;
    private int currenctline = 0;
    private float pageTop = 0;
    private float RowSize = 50;
    public BillPrintController(DataGridViewRow[] rows)
    {
        this.rows = rows;
        printFont = new Font("Courier New", 12);
    }

    public override void OnStartPrint(PrintDocument document, PrintEventArgs e)
    {
        currenctline = 0;
        document.PrintPage += OnPrintPage;
        base.OnStartPrint(document, e);
    }
    public override Graphics OnStartPage(PrintDocument document, PrintPageEventArgs e)
    {
        Graphics graphic = base.OnStartPage(document, e);
        if (graphic != null)
        {
            pageTop = e.MarginBounds.Top;
            Pen pen = new Pen(Brushes.Black, 2);
            float fontHeight = printFont.GetHeight();
            if (currenctline == 0)
            {
                graphic.DrawString("Suppiler", new Font("Courier New", 20), Brushes.Black, 10, pageTop);
                graphic.DrawString("Address", printFont, Brushes.Black, 10, pageTop += 40);
                graphic.DrawString("City", printFont, Brushes.Black, 10, pageTop += 25);
                graphic.DrawString("Ph: " + "0000000000", printFont, Brushes.Black, 10, pageTop += 25);
                pageTop += 50;
            }
            graphic.DrawLine(pen, 0, pageTop, e.PageBounds.Width, pageTop);
            string header = "Sr.".PadRight(8) + "Medicine".PadRight(40) + "Qty".PadRight(10) + "Rate".PadRight(15) + "Amount";
            graphic.DrawString(header, printFont, Brushes.Black, 10, pageTop += 5);
            pageTop += 25;
            graphic.DrawLine(pen, 0, pageTop, e.PageBounds.Width, pageTop);
            pageTop += 5;
            pen.Dispose();
        }
        return graphic;
    }
    public override void OnEndPage(PrintDocument document, PrintPageEventArgs e)
    {
        if (!e.HasMorePages)
        {
            Graphics graphic = e.Graphics;
            if (graphic != null)
            {
                float fontHeight = printFont.GetHeight();
                graphic.DrawLine(new Pen(Color.Black, 2), 0, 900, 900, 900);
                graphic.DrawString("Total", printFont, Brushes.Black, 600, 905);
                graphic.DrawString("Disc.", printFont, Brushes.Black, 600, 925);
                graphic.DrawString("Payable", printFont, Brushes.Black, 600, 945);
                Console.WriteLine("End Page");
            }
        }
        base.OnEndPage(document, e);
    }

    public override void OnEndPrint(PrintDocument document, PrintEventArgs e)
    {
        base.OnEndPrint(document, e);
        document.PrintPage -= OnPrintPage;
    }

    private void OnPrintPage(object sender, PrintPageEventArgs ev)
    {
        float linesPerPage = 0;
        float yPos = 0;
        int count = 0;
        float topMargin = pageTop;
        linesPerPage = (ev.MarginBounds.Height - pageTop) / RowSize;
        var rowHeight = (ev.MarginBounds.Height - pageTop) / linesPerPage;
        while (count < linesPerPage && currenctline < rows.Length)
        {
            string line = currenctline.ToString().PadRight(8) + ("Med " + currenctline.ToString()).PadRight(40) + "Qty".PadRight(10) + "Rate".PadRight(15) + "Amount";
            yPos = topMargin + (count * rowHeight);
            ev.Graphics.DrawString(line, printFont, Brushes.Black, 10, yPos, new StringFormat());
            currenctline++;
            count++;
        }

        // If more lines exist, print another page.
        if (currenctline < rows.Length)
            ev.HasMorePages = true;
        else
            ev.HasMorePages = false;
    }

}

希望这会帮助您清除。