簡體   English   中英

如何在 c# 中為 printDocument 中的每個副本添加頁碼

[英]how can i add page number to each copy from printDocument in c#

請您幫我了解如何在打印多份文檔時添加頁碼?

我正在使用 Windows 窗體和 printdocument,在我的打印頁面中我插入了一個計數器,但它僅在我同時產生印象時才會尊重它,並且如果我放置 5 個副本,它會在 5 頁中向我發送相同的頁碼,我希望頁碼在副本中連續出現

這是我的代碼:

private int pagenum;

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    using (var g = e.Graphics)
    {
            using (var fnt = new Font("Courier New", 10, FontStyle.Bold))
            {
                string content = Contenido.Text;

                Barcode codigo = new Barcode();
                codigo.Alignment = AlignmentPositions.CENTER;
                codigo.LabelFont = new Font(FontFamily.GenericMonospace, 8, FontStyle.Bold);
                codigo.IncludeLabel = true;

                Image img = codigo.Encode(TYPE.CODE128, content, Color.Black, Color.White, 250, 70);
                pictureBox.Image = img;

                var f = new Font("Courier New", 8, FontStyle.Bold);

                g.DrawString("SEMayr", fnt, Brushes.Black, 147, 10);
                g.DrawString("1er. Turno", fnt, Brushes.Black, 150, 40);
                g.DrawImage(pictureBox.Image, 98, 168, 201, 55);

                e.Graphics.DrawString("Pg " + pagenum, f, Brushes.Black, 215, 230);

                pagenum++;

                var caption = textBox1.Text;
                g.DrawString(caption, fnt, Brushes.Black, 118, 70);
            }
    }
}

private void button2_Click(object sender, EventArgs e){ try
{
            PrintDocument pd = new PrintDocument();
            pd.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
            // Especifica que impresora se utilizara!!
            pd.PrinterSettings.PrinterName = "Datamax";
            PageSettings pa = new PageSettings();
            pa.Margins = new Margins(0, 0, 0, 0);
            pd.DefaultPageSettings.Margins = pa.Margins;
            PaperSize ps = new PaperSize("Custom", 403, 244);
            pd.DefaultPageSettings.PaperSize = ps;
            pd.PrinterSettings.Copies = (short)Convert.ToInt32(label4.Text);

            pd.Print();
        }
        catch (Exception exp)
        {
            MessageBox.Show("Printing " + exp.Message);
        }
}

好吧,副本應該是相同的。 因此,當您設置PrinterSettings.Copies = n時,您指示打印機輸出您在PrintPage事件處理程序中擁有的n副本,包括頁碼,它不會為每個副本增加。

要保留計數器,請不要設置PrinterSettings.Copies屬性並使用e.HasMorePages打印內容n次。

因此(閱讀評論):

// +
using System.Drawing.Printing;
//

int copies;
int pageNum;

private void button2_Click(object sender, EventArgs e)
{
    pageNum = 0;
    copies = int.Parse(label4.Text);

    // If this does not change, then no need 
    // to have it in the PrintPage event.
    // Otherwise keep it as is.
    Barcode codigo = new Barcode
    {
        Alignment = AlignmentPositions.CENTER,
        LabelFont = new Font(FontFamily.GenericMonospace, 8, FontStyle.Bold),
        IncludeLabel = true
    };

    Image img = codigo.Encode(TYPE.CODE128, Contenido.Text, Color.Black, Color.White, 250, 70);
    pictureBox.Image = img;

    using (PrintDocument pd = new PrintDocument())
    {
        pd.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
        pd.PrinterSettings.PrinterName = "Datamax";
        pd.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);
        pd.DefaultPageSettings.PaperSize = new PaperSize("Custom", 403, 244);
        pd.Print();
    }

    // If the Barcode implements IDisposable interface...
    // codigo.Dispose();
}

private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
    copies--;
    pageNum++;

    // Don't dispose of the e.Graphics object.
    // You didn't create it then its not your call.
    var g = e.Graphics;

    using (var fnt = new Font("Courier New", 10, FontStyle.Bold))
    using (var f = new Font("Courier New", 8, FontStyle.Bold))
    {
        g.DrawString("SEMayr", fnt, Brushes.Black, 147, 10);
        g.DrawString("1er. Turno", fnt, Brushes.Black, 150, 40);
        g.DrawImage(pictureBox.Image, 98, 168, 201, 55);
        g.DrawString($"Pg {pageNum}", f, Brushes.Black, 215, 230);
        g.DrawString(textBox1.Text, fnt, Brushes.Black, 118, 70);

        e.HasMorePages = copies > 0;
    }
}

此外,考慮使用StringFormat類在矩形中繪制字符串。 請參閱Graphics.DrawString方法重載。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM