健康一贴灵,专注医药行业管理信息化

c# 打印电子发票

需要通过NUGET 安装 spire.pdf和FreeSpire.PDF

源码下载

应用下载

 

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Printing;
using Spire.Pdf;
using System.Diagnostics;

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:\Users\";
            //记忆之前打开的对话框
            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)
        {
            for (int i = 0; i < lbFileList.Items.Count; i++)
            {
                pdfPrint(lbFileList.Items[i].ToString());
            }
            MessageBox.Show("打印完成!", "提示");
            

        }
        /// <summary>
        /// 打印PDF文件到自定义纸张
        /// 
        /// </summary>
        /// <param name="pdfFileName">需要打印的PDF文件名称</param>
        void pdfPrint(string pdfFileName)
        {
            try
            {
                //加载需要打印的PDF文档
                PdfDocument doc = new Spire.Pdf.PdfDocument();
                doc.LoadFromFile(pdfFileName);

                //获取原文档第一页的纸张大小,这里的单位是Point

                SizeF size = doc.Pages[0].Size;

                //实例化PaperSize对象,设置其宽、高

                //需要特别注意的是这里涉及到单位的转换,PaperSize的宽高参数默认单位是百英寸 
                int paperWidth = Convert.ToInt32(txtWidth.Text);
                int paperHeight = Convert.ToInt32(txtHeight.Text);

                PaperSize paper = new PaperSize("Custom", (int)size.Width / 72 * paperWidth, (int)size.Height / 72 * paperHeight);

                paper.RawKind = (int)PaperKind.Custom;

                //设置打印的纸张大小为原来文档的大小

                doc.PrintSettings.PaperSize = paper;

                //需要选择FitSize打印模式
                doc.PrintSettings.SelectSinglePageLayout(Spire.Pdf.Print.PdfSinglePageScalingMode.FitSize, true);
                //打印

                doc.Print();
            }catch (Exception e)
            {
                MessageBox.Show("打印出错", "出错原因:"+e.Message);
                return;
            }

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox1.Checked)
            {
                txtWidth.ReadOnly = false;
                txtHeight.ReadOnly = false;
            }
            else
            {
                txtWidth.ReadOnly = true;
                txtHeight.ReadOnly = true;
            }

        }

        private void btnClean_Click(object sender, EventArgs e)
        {
            lbFileList.Items.Clear();
        }

        private void lbFileList_DoubleClick(object sender, EventArgs e)
        {
            //用系统的PDF 默认方式打开PDF 文件
            Process myProcess = new Process();
            if (lbFileList.Items.Count == 0) return;
            try
            {
                myProcess.StartInfo.FileName = lbFileList.SelectedItem.ToString();
                myProcess.StartInfo.Verb = "Open";
                myProcess.StartInfo.CreateNoWindow = true;
                myProcess.Start();
            }
            catch(Exception es)
            {
                MessageBox.Show("提示","出错信息:" + es.Message);
                return;
            }
        }

        private void lbFileList_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }

}

 

posted @ 2023-05-25 11:46  一贴灵  阅读(332)  评论(2编辑  收藏  举报
学以致用,效率第一