年末将至,又到了一年一度的收集发票时间,平时零零碎碎的花钱都是不在意开发票,现在好了,到处找发票来报销,简直头大,
东拼西凑,终于搞定了全部发票,大伙多余的发票,麻烦艾特我一下啊,不限日期,能开发票的都可以,谢谢啦。文章后有彩蛋~
这么多的发票,一张一张打印,要浪费我多少时间,就想着合并这些PDF文件(发票)一起打印,节省时间。
还寻思这网上找一个软件来使使,都很(méi)委(līang)婉(xīn)的想收费,在线转PDF的还给我打水印,恰巧今天公司网速不行。
来吧,自己搞!程序猿没有什么不可能!
Spire.PDF
NPOI、DocX、Epplus……这些开源且免费的优秀组件,大家应该是耳熟能详的,而Spire是一款企业级的收费组件,Spire.PDF for .NET
是一个专业的PDF组件,用于在.NET应用程序中创建,编写,编辑,处理和阅读PDF文件,功能十分丰富…………
提取关键词:收费、稳定、高效
具体我不再过多的啰嗦了,详细我推荐看这篇《 高效而稳定的企业级.NET Office 组件Spire(.NET组件介绍之二)》
合并PDF
回到今天的问题,合并发票,合并PDF,然后一次性打印。其实吧,主要代码就两行:
1 var doc = PdfDocument.MergeFiles(allFiles.ToArray()); 2 doc.Save(DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf", FileFormat.PDF);
然后,你们输出之后会发现,很(méi)漂(līang)亮(xīn)的水印:
“Evaluation Warning:The document was created with Spire.PDF for .NET”
简直玷污我心爱的发票!!!!
解决
既然隐藏不了,那合并的第一页加入一个空页不就行了?!生成PDF后,然后从第二页开始打印或是删去第一页不就行了?!就这么干!
1 var first = new PdfDocument(); 2 first.AppendPage(); //加入一个空页 3 for (int idx = 0; idx < allFiles.Count; idx++) 4 { 5 var next = new PdfDocument(allFiles[idx]); 6 first.AppendPage(next); 7 } 8 first.SaveToFile(DateTime.Now.ToString("yyyyMMddHHmmss")+".pdf", FileFormat.PDF);
需要的下载去用吧。 PDFMerge
工具很简单,就不放到Git上去了,源码:
界面:
namespace 合并PDF { partial class mainForm { ////// 必需的设计器变量。 /// private System.ComponentModel.IContainer components = null; /// /// 清理所有正在使用的资源。 /// /// 如果应释放托管资源,为 true;否则为 false。 protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// /// 设计器支持所需的方法 - 不要修改 /// 使用代码编辑器修改此方法的内容。 /// private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.btnAdd = new System.Windows.Forms.Button(); this.btnMerge = new System.Windows.Forms.Button(); this.bgw = new System.ComponentModel.BackgroundWorker(); this.listView1 = new System.Windows.Forms.ListView(); this.columnHeader1 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.statusStrip1 = new System.Windows.Forms.StatusStrip(); this.progressBar = new System.Windows.Forms.ToolStripProgressBar(); this.tipStatu = new System.Windows.Forms.ToolStripStatusLabel(); this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components); this.btnRemove = new System.Windows.Forms.ToolStripMenuItem(); this.statusStrip1.SuspendLayout(); this.contextMenuStrip1.SuspendLayout(); this.SuspendLayout(); // // btnAdd // this.btnAdd.Location = new System.Drawing.Point(409, 12); this.btnAdd.Name = "btnAdd"; this.btnAdd.Size = new System.Drawing.Size(67, 24); this.btnAdd.TabIndex = 2; this.btnAdd.Text = "添加文件"; this.btnAdd.UseVisualStyleBackColor = true; this.btnAdd.Click += new System.EventHandler(this.btnSearch_Click); // // btnMerge // this.btnMerge.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.btnMerge.Location = new System.Drawing.Point(409, 43); this.btnMerge.Name = "btnMerge"; this.btnMerge.Size = new System.Drawing.Size(67, 24); this.btnMerge.TabIndex = 3; this.btnMerge.Text = "开始合并"; this.btnMerge.UseVisualStyleBackColor = true; this.btnMerge.Click += new System.EventHandler(this.btnMerge_Click); // // bgw // this.bgw.WorkerReportsProgress = true; this.bgw.DoWork += new System.ComponentModel.DoWorkEventHandler(this.bgw_DoWork); this.bgw.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(this.bgw_ProgressChanged); this.bgw.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.bgw_RunWorkerCompleted); // // listView1 // this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] { this.columnHeader1}); this.listView1.ContextMenuStrip = this.contextMenuStrip1; this.listView1.FullRowSelect = true; this.listView1.GridLines = true; this.listView1.Location = new System.Drawing.Point(12, 12); this.listView1.MultiSelect = false; this.listView1.Name = "listView1"; this.listView1.Size = new System.Drawing.Size(391, 190); this.listView1.TabIndex = 4; this.listView1.UseCompatibleStateImageBehavior = false; this.listView1.View = System.Windows.Forms.View.Details; // // columnHeader1 // this.columnHeader1.Text = "文件"; this.columnHeader1.Width = 350; // // statusStrip1 // this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.progressBar, this.tipStatu}); this.statusStrip1.Location = new System.Drawing.Point(0, 205); this.statusStrip1.Name = "statusStrip1"; this.statusStrip1.Size = new System.Drawing.Size(488, 22); this.statusStrip1.TabIndex = 5; this.statusStrip1.Text = "statusStrip1"; // // progressBar // this.progressBar.Name = "progressBar"; this.progressBar.Size = new System.Drawing.Size(100, 16); // // tipStatu // this.tipStatu.Name = "tipStatu"; this.tipStatu.Size = new System.Drawing.Size(27, 17); this.tipStatu.Text = "0/0"; // // contextMenuStrip1 // this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.btnRemove}); this.contextMenuStrip1.Name = "contextMenuStrip1"; this.contextMenuStrip1.Size = new System.Drawing.Size(101, 26); // // btnRemove // this.btnRemove.Name = "btnRemove"; this.btnRemove.Size = new System.Drawing.Size(180, 22); this.btnRemove.Text = "移除"; this.btnRemove.Click += new System.EventHandler(this.btnRemove_Click); // // mainForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(488, 227); this.Controls.Add(this.statusStrip1); this.Controls.Add(this.listView1); this.Controls.Add(this.btnMerge); this.Controls.Add(this.btnAdd); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; this.MaximizeBox = false; this.MaximumSize = new System.Drawing.Size(504, 266); this.MinimumSize = new System.Drawing.Size(504, 266); this.Name = "mainForm"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "PDF Merge"; this.statusStrip1.ResumeLayout(false); this.statusStrip1.PerformLayout(); this.contextMenuStrip1.ResumeLayout(false); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Button btnAdd; private System.Windows.Forms.Button btnMerge; private System.ComponentModel.BackgroundWorker bgw; private System.Windows.Forms.ListView listView1; private System.Windows.Forms.ColumnHeader columnHeader1; private System.Windows.Forms.StatusStrip statusStrip1; private System.Windows.Forms.ToolStripProgressBar progressBar; private System.Windows.Forms.ToolStripStatusLabel tipStatu; private System.Windows.Forms.ContextMenuStrip contextMenuStrip1; private System.Windows.Forms.ToolStripMenuItem btnRemove; } }
后台:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Windows.Forms; using Spire.Pdf; using System.Threading; namespace 合并PDF { public partial class mainForm : Form { public mainForm() { InitializeComponent(); } List<string> allFiles = new List<string>(); private void btnSearch_Click(object sender, EventArgs e) { OpenFileDialog openFile = new OpenFileDialog(); openFile.Filter = "PDF文件|*.pdf"; openFile.Multiselect = true; if (openFile.ShowDialog() == DialogResult.OK) { foreach (var fi in openFile.FileNames) { if (allFiles.Contains(fi)) continue; this.listView1.Items.Add(new ListViewItem(fi)); allFiles.Add(fi); } progressBar.Minimum = 0; progressBar.Maximum = allFiles.Count; } } private void btnMerge_Click(object sender, EventArgs e) { if (!bgw.IsBusy) { if (allFiles.Count<=1) { MessageBox.Show("请至少选择 2 个PDF文件"); return; } CtrlStatu(false); bgw.RunWorkerAsync(); } } private void CtrlStatu(bool b) { listView1.Enabled = b; btnMerge.Enabled = b; btnAdd.Enabled = b; } private void bgw_DoWork(object sender, DoWorkEventArgs e) { var first = new PdfDocument(); first.AppendPage(); //加入一个空页 for (int idx = 0; idx < allFiles.Count; idx++) { var next = new PdfDocument(allFiles[idx]); first.AppendPage(next); ReportInfo(idx + 1, allFiles.Count); Thread.Sleep(100); } first.SaveToFile(DateTime.Now.ToString("yyyyMMddHHmmss")+".pdf", FileFormat.PDF); } ////// 输出消息 /// public void ReportInfo(int idx,int count) { bgw.ReportProgress(1, idx + "/" + count); } private void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e) { var userState = e.UserState.ToString(); tipStatu.Text = userState; progressBar.Value = Convert.ToInt32(userState.Split('/')[0]); } private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { tipStatu.Text = "合并完成..."; CtrlStatu(true); } private void btnRemove_Click(object sender, EventArgs e) { if (this.listView1.SelectedItems.Count > 0) { int idx = this.listView1.SelectedItems[0].Index; allFiles.Remove(this.listView1.SelectedItems[0].Text); this.listView1.Items[idx].Remove(); } } } }