.Net C# 免费PDF合成软件

最近用到pdf合成,发现各种软件均收费啊,这个技术非常简单,别人写好的库一大把,这里用到了PDFsharp,项目地址Home of PDFsharp and MigraDoc Foundation

软件下载地址

https://download.csdn.net/download/g313105910/88014569

源码下载地址

https://download.csdn.net/download/g313105910/88014573

废话不多说,上WPF代码


    
        
            
            
        
        
            
        
        
using FolderBrowserEx;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;

namespace mergePDF
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void ButtonStart_Click(object sender, RoutedEventArgs e)
        {
            Info_Text.Text = string.Empty;
            CombinePdfFiles(Url_Text.Text);
        }

        public void CombinePdfFiles(string folderPath)
        {
            try
            {
                //读取文件夹中的文件夹
                int fileCount = 0;
                int pageCount = 0;
                PdfDocument outPdfFile = new PdfDocument();
                List fileNameList = new List();
                List fileError = new List();
                fileNameList.Add("开始合并");
                foreach (var filename in Directory.GetFiles(folderPath))
                {
                    if (System.IO.Path.GetExtension(filename) == ".pdf")
                    {
                        try
                        {
                            PdfDocument pdfFile = PdfReader.Open(filename, PdfDocumentOpenMode.Import);
                            fileCount++;
                            pageCount += pdfFile.PageCount;
                            int pageIndex = 0;
                            foreach (var pdfPage in pdfFile.Pages)
                            {
                                outPdfFile.AddPage(pdfPage);
                                pageIndex++;
                                fileNameList.Add(string.Format("文件名:{0}\t\t\t\t第{1}页\t文件共有{2}页", System.IO.Path.GetFileName(filename), pageIndex, pdfFile.PageCount));
                            }
                        }
                        catch (Exception ex)
                        {
                            fileError.Add(System.IO.Path.GetFileName(filename));
                            Info_Text.Text += ex.Message + "\r\n";
                        }
                    }
                }
                string combineResult = "";
                if (fileCount > 0)
                {
                    //导出pdf文件
                    string timeTag = DateTime.Now.ToString("MMdd_HH_mm_ss");
                    SavePdfFile(folderPath + "\\合并", timeTag + ".pdf", outPdfFile);
                    fileNameList.Add(string.Format("共 {0} 个文件,共 {1} 页。", fileCount, pageCount));
                    if (fileError.Count > 0)
                    {
                        fileNameList.Add("合并失败文件:");
                        foreach (string p in fileError)
                        {
                            fileNameList.Add(p);
                        }
                    }
                    SaveTxtFile(folderPath + "\\合并", timeTag + ".txt", fileNameList);
                }
                combineResult = string.Format("合并成功文件数量:{0}", fileCount);
                if (fileError.Count > 0)
                {
                    combineResult += "\r\n";
                    combineResult += string.Format("合并失败文件数量:{0}", fileError.Count);
                }
                foreach(var str in fileNameList)
                {
                    Info_Text.Text += str + "\r\n"; ;
                }
                Info_Text.Text += combineResult + "\r\n"; 
            }
            catch (Exception ex)
            {
                Info_Text.Text+=ex.Message + "\r\n";
            }
            Info_Text.ScrollToEnd();
        }
        private void SavePdfFile(string folderPath, string fileName, PdfDocument pdfFile)
        {
            if (!Directory.Exists(folderPath))
            {
                Directory.CreateDirectory(folderPath);
            }
            pdfFile.Save(folderPath + "\\" + fileName);
        }
        private void SaveTxtFile(string folderPath, string fileName, List content)
        {
            if (!Directory.Exists(folderPath))
            {
                Directory.CreateDirectory(folderPath);
            }
            string fileFullPath = folderPath + "\\" + fileName;
            if (!File.Exists(fileFullPath))
            {
                var newFile = File.CreateText(fileFullPath);
                newFile.Close();
            }
            using (StreamWriter sw = new StreamWriter(fileFullPath, true))
            {
                foreach (string line in content)
                {
                    sw.WriteLine(line);
                }
                sw.Close();
            }
        }

        private void ButtonUrl_Click(object sender, RoutedEventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
            folderBrowserDialog.Title = "Select a folder";
            //folderBrowserDialog.InitialFolder = @"C:\";
            folderBrowserDialog.AllowMultiSelect = false;
            if (folderBrowserDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Url_Text.Text = folderBrowserDialog.SelectedFolder;
            }
        }
    }
}

使用方法->打开软件

.Net C# 免费PDF合成软件_第1张图片

选择目录

.Net C# 免费PDF合成软件_第2张图片

合成成功

.Net C# 免费PDF合成软件_第3张图片

合并后的文件和日志

.Net C# 免费PDF合成软件_第4张图片

你可能感兴趣的:(pdf)