gpt4 book ai didi

c# - 需要帮助打印列表中的多页

转载 作者:行者123 更新时间:2023-11-30 17:16:31 25 4
gpt4 key购买 nike

我很难弄清楚 c# 打印多页。我的应用程序创建了一个包含 1 到 10 个元素的对象类型列表。每个对象包含 2 个字符串属性:docTypeNumber 和 docTypeDescription。一个名为 flightnumber 的变量也被传递到类构造函数中。每个实例都是一种文档类型,必须作为单独的条形码表打印,其中包含文档类型编号、说明和航类号。大多数多页打印示例是一个文档“溢出”到多个页面上,而不是一个文档由多个单独的页面组成。我的问题是如何实现这一目标。

我是否需要创建一个覆盖多个页面的大型文档?我是否必须创建 PrintDocument 类的多个实例?

如有任何帮助,我们将不胜感激。

这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;

namespace BarcodeTest
{
class BarcodePrinter
{
public BarcodePrinter(List<DocumentType> type, string flightnumber)
{
docType = type;
flightNumber = flightnumber;
}

//Attributes
private List<DocumentType> docType = new List<DocumentType>();
private string flightNumber;

//helper variables
string docTypeNumber;
string docTypeDescription;
int pageNumber = 1;
int numberOfPages;
private static Font barcodeFont = new Font("3 of 9 Barcode", 24);
private static Font printFont = new Font("Arial", 24);

public void Print()
{
numberOfPages = docType.Count;

PrintDocument pd = new PrintDocument();

foreach (DocumentType type in docType)
{
docTypeNumber = type.DocumentTypeNumber;
docTypeDescription = type.DocumentDescription;

pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
}//end foreach

#if DEBUG
PrintPreviewDialog printPreview = new PrintPreviewDialog();
printPreview.Document = pd;
printPreview.Show();
#else
pd.Print();
#endif
}// end Print() method

public void pd_PrintPage(Object sender, PrintPageEventArgs e)
{
Graphics g = e.Graphics;
//e.Graphics.PageUnit = GraphicsUnit.Point;
e.Graphics.PageUnit = GraphicsUnit.Inch;

StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;

Brush br = new SolidBrush(Color.Black);
RectangleF rec1 = new RectangleF(1.9375f, 0f, 4, 1);
RectangleF rec2 = new RectangleF(1.9375f, .5f, 4, 1);
RectangleF rec3 = new RectangleF(1.9375f, 1f, 4, 1);
RectangleF rec4 = new RectangleF(1.9375f, 2, 4, 1);
RectangleF rec5 = new RectangleF(1.9375f, 2.5f, 4, 1);
g.DrawString("Air - " + docTypeDescription, printFont, br, rec1, stringFormat);

g.DrawString("*" + docTypeNumber + "*", barcodeFont, br, rec2, stringFormat);
g.DrawString(docTypeNumber, printFont, br, rec3, stringFormat);


g.DrawString("*" + flightNumber + "*", barcodeFont, br, rec4, stringFormat);
g.DrawString(flightNumber, printFont, br, rec5, stringFormat);


if (pageNumber < numberOfPages)
{
e.HasMorePages = true;

}
else
e.HasMorePages = false;
pageNumber++;

}//end pd_PrintPage Method


}//end BarcodePrinter Class
}//end namespace

最佳答案

我想通了。我需要在打印页面处理程序中遍历我的列表。我通过对每一页进行计数来做到这一点。我根据列表中的项目数知道有多少页。这是我的工作代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;

namespace BarcodeTest
{
class BarcodePrinter
{
public BarcodePrinter(List<DocumentType> type, string number)
{
docType = type;
flightNumber = number;
}

//Attributes
private List<DocumentType> docType = new List<DocumentType>();
private string flightNumber;

//helper variables
string docTypeNumber;
string docTypeDescription;
int pageNumber = 1;
int numberOfPages;
Font barcodeFont = new Font("3 of 9 Barcode", 36);
Font printFont = new Font("Arial", 24);
int i = 0;





public void Print()
{

numberOfPages = docType.Count; //# of List elements = # of pages


PrintDocument pd = new PrintDocument();

pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);



#if DEBUG
PrintPreviewDialog printPreview = new PrintPreviewDialog();
printPreview.Document = pd;
printPreview.Show();
#else
pd.Print();
#endif


}// end Print() method


public void pd_PrintPage(Object sender, PrintPageEventArgs e)
{

docTypeNumber = docType[i].DocumentTypeNumber; // This is a get/set Property
docTypeDescription = docType[i].DocumentDescription; // This is a get/set Property

StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;


Graphics g = e.Graphics;
e.Graphics.PageUnit = GraphicsUnit.Inch;

Brush br = new SolidBrush(Color.Black);
RectangleF rec1 = new RectangleF(.9375f, 0, 6, 1);
RectangleF rec2 = new RectangleF(1.9375f, .5f, 4, 1);
RectangleF rec3 = new RectangleF(1.9375f, 1f, 4, 1);
RectangleF rec4 = new RectangleF(.9375f, 2, 6, 1);
RectangleF rec5 = new RectangleF(1.9375f, 2.5f, 4, 1);
g.DrawString("Air - " + docTypeDescription, printFont, br, rec1, stringFormat);
// '*' Must Preceed and Follow Information for a bar code to be scannable
g.DrawString("*" + docTypeNumber + "*", barcodeFont, br, rec2, stringFormat);
g.DrawString(docTypeNumber, printFont, br, rec3, stringFormat);

// '*' Must Preceed and Follow Information for a bar code to be scannable
g.DrawString("*" + flightNumber + "*", barcodeFont, br, rec4, stringFormat);
g.DrawString(flightNumber, printFont, br, rec5, stringFormat);



if (pageNumber < numberOfPages)
{
e.HasMorePages = true;
i++;
pageNumber++;

}
else
{
e.HasMorePages = false;
}

}//end pd_PrintPage Method


}//end BarcodePrinter Class
}//end namespace

关于c# - 需要帮助打印列表中的多页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7212324/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com