.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代码

cs 复制代码
<Window x:Class="mergePDF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:mergePDF"
        mc:Ignorable="d"
        Title="PDF合成器,有顺序要求请用数字给文件命名,从小到大排序" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="10*"/>
            <RowDefinition Height="33*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Button Content="合成" HorizontalAlignment="Left" Margin="737,0,0,0" VerticalAlignment="Center" Click="ButtonStart_Click" Height="19" Width="28"/>
        <Button Content="选择目录" HorizontalAlignment="Left" Margin="675,0,0,0" VerticalAlignment="Center" Click="ButtonUrl_Click" Height="19" Width="52"/>
        <TextBox x:Name="Url_Text" HorizontalAlignment="Left" Margin="60,0,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Center" Width="600" Height="17"/>
        <Label Content="Url:" HorizontalAlignment="Left" Margin="25,0,0,0" VerticalAlignment="Center"/>
        <TextBox x:Name="Info_Text" TextWrapping="Wrap" VerticalScrollBarVisibility="Visible"  Text="" Grid.Row="1"/>
    </Grid>
</Window>
cs 复制代码
using FolderBrowserEx;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;

namespace mergePDF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    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<string> fileNameList = new List<string>();
                List<string> fileError = new List<string>();
                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<string> 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;
            }
        }
    }
}

使用方法->打开软件

选择目录

合成成功

合并后的文件和日志

相关推荐
Dovis(誓平步青云)6 小时前
多个链接不等于多份证据,新闻核验看板怎样合并来源
java·服务器·前端·javascript·人工智能·pdf·电脑
web打印社区10 小时前
Windows 网页静默打印设置步骤:客户端、防火墙与联调清单
开发语言·前端·javascript·chrome·pdf·ecmascript
开开心心就好11 小时前
图片白底怎么去掉?抠图工具抠完背景透明
java·服务器·开发语言·pdf·ocr·散列表·启发式算法
web打印社区11 小时前
JS 静默打印怎么做:纯前端为什么不行,以及最小可跑通写法
开发语言·前端·javascript·websocket·网络协议·http·pdf
web打印社区11 小时前
HTML5 静默打印:前端页面怎么调用打印机且尽量不弹窗
前端·javascript·vue.js·pdf·html·html5
晚来的棠棠11 小时前
27考研数学超全资料PDF
考研·pdf
正经教主14 小时前
【FDE系列】阶段3:Day 63:文档解析 — 把真实 PDF 手册变成可用文本
人工智能·pdf·fde
码林拾遗WJ1 天前
学术 PDF 翻译工作流:从乱码到双语对照的实战记录
pdf
web打印社区1 天前
政务办事大厅:叫号小票、办理回执网页怎么静默出纸
前端·javascript·vue.js·pdf·html·政务
是枚小菜鸡儿吖1 天前
TraceBack:基于TextIn xParse,统一解析PDF/Word/Excel/扫描件/截图,自动交叉核验维修报告真伪,带原文出处,让造假无处遁形
pdf·word·excel