using System; using System.Drawing; using System.Windows.Forms; using System.Drawing.Printing; using Spire.Pdf; namespace InvoicePrint { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnUpload_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); //设置对话框标题 openFileDialog.Title = "选择要打印的发票"; //设置文件类型 openFileDialog.Filter = "PDF文件|*.pdf|PDF文件|*.PDF"; //默认加载目录 openFileDialog.InitialDirectory = @"C:\"; //记忆之前打开的对话框 openFileDialog.RestoreDirectory = true; //多选 openFileDialog.Multiselect = true; if (openFileDialog.ShowDialog() == DialogResult.OK) { for (int i = 0; i < openFileDialog.SafeFileNames.Length; i++) { //openFileDialog.SafeFileNames[i] 文件名扩展名 //openFileDialog.FileNames[i] 全路径及文件名 //lbFileList.Items.Add(openFileDialog.SafeFileNames[i]); lbFileList.Items.Add(openFileDialog.FileNames[i]); } } } private void btnExit_Click(object sender, EventArgs e) { Close(); } private void btnPrint_Click(object sender, EventArgs e) { pdfPrint(lbFileList.Items[0].ToString()); } void pdfPrint(string pdfFileName) { //加载需要打印的PDF文档 PdfDocument doc=new Spire.Pdf.PdfDocument(); doc.LoadFromFile(pdfFileName); //获取原文档第一页的纸张大小,这里的单位是Point SizeF size = doc.Pages[0].Size; //实例化PaperSize对象,设置其宽、高 //需要特别注意的是这里涉及到单位的转换,PaperSize的宽高参数默认单位是百英寸 PaperSize paper = new PaperSize("Custom", (int)size.Width / 210 * 100, (int)size.Height / 72 * 100); paper.RawKind = (int)PaperKind.Custom; //设置打印的纸张大小为原来文档的大小 doc.PrintSettings.PaperSize = paper; //需要选择FitSize打印模式 doc.PrintSettings.SelectSinglePageLayout(PdfSinglePageScalingMode.FitSize, true); //打印 doc.Print(); } } }
标签:c#,doc,void,打印,PaperSize,openFileDialog,using,发票 From: https://www.cnblogs.com/lrzy/p/17430740.html