| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1878 人关注过本帖
标题:请教关于C#合并pdf文件的问题
只看楼主 加入收藏
top_xjy
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2017-12-14
收藏
 问题点数:0 回复次数:0 
请教关于C#合并pdf文件的问题
我有目录文件夹1、目录文件夹2,文件夹1和2里面都有多个同名的pdf文件,我要把文件夹2里面的pdf插入到文件夹1里面的同名pdf里面指定页,如:第3页位置。
初学C#还望高手指点为谢
图片附件: 游客没有浏览图片的权限,请 登录注册

已经学着搞了一串代码,但是运行不成功,请知道的老师指点一下!
程序代码:
using System;
using System.Collections.Generic;
using using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Spire.Pdf;
using Spire.Pdf.Graphics;
using namespace wenjianjia
{
    public partial class PDFMerge : Form
    {
        public PDFMerge()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //获取文件夹1中的所有pdf
            FolderBrowserDialog FBDialog = new FolderBrowserDialog();
            string folder1Path = "";
            if (FBDialog.ShowDialog() == DialogResult.OK)
            {
                string strPath = FBDialog.SelectedPath;
                if (strPath.EndsWith("\\"))
                    folder1Path = strPath;
                else
                    folder1Path = strPath + "\\";
            }
            //遍历文件夹1获取具体文件
            //files集合存储文件名和对应的具体位置
            Dictionary<string, string> Files1 = new Dictionary<string, string>();
            DirectoryInfo theFolder = new DirectoryInfo(folder1Path);
            DirectoryInfo[] dirInfo = theFolder.GetDirectories();
            foreach (DirectoryInfo NextFolder in dirInfo)
            {
                FileInfo[] fileInfo = NextFolder.GetFiles("*.pdf");
                foreach (FileInfo NextFile in fileInfo)
                {
                    Files1.Add(NextFile.Name, NextFile.FullName);
                }
            }

            //遍历文件夹2,同样的操作,其实这里可以提取一个方法出来要简便些
            FBDialog = new FolderBrowserDialog();
            string folder2Path = "";
            if (FBDialog.ShowDialog() == DialogResult.OK)
            {
                string strPath = FBDialog.SelectedPath;
                if (strPath.EndsWith("\\"))
                    folder2Path = strPath;
                else
                    folder2Path = strPath + "\\";
            }
            Dictionary<string, string> Files2 = new Dictionary<string, string>();
            theFolder = new DirectoryInfo(folder2Path);

            FileInfo[] fileInfo1 = theFolder.GetFiles("*.pdf");
            foreach (FileInfo NextFile in fileInfo1)
            {
                Files2.Add(NextFile.Name, NextFile.FullName);
            }

            //遍历Files2,如果Files1中有相同的key,也就是文件name相同,将文件夹2中的pdf的第一页插入到文件夹1中对应pdf的第三页
            foreach (string name in Files1.Keys)
            {
                PdfDocument pdf1 = new PdfDocument();
                pdf1.LoadFromFile(Files1[name]);
                PdfDocument pdf2 = new PdfDocument();
                pdf2.LoadFromFile(Files2[name]);
                if (Files2.ContainsKey(name))
                {
                    //获取文件夹2中pdf的第一页插入到对应pdf的第三页
                    PdfPageBase page = pdf1.Pages[0];
                    SizeF size = page.Size;
                    PdfTemplate template = page.CreateTemplate();
                    pdf2.Pages.Insert(2, size, new PdfMargins(0, 0));
                    pdf2.Pages[2].Canvas.DrawTemplate(template, new PointF(0, 0));
                    pdf2.Pages.Add(size, new PdfMargins(0, 0)); 
                    pdf2.SaveToFile(Files1[name]);
                    System.Diagnostics.Process.Start(Files1[name]);
                }

            }
        }
    }
}

搜索更多相关主题的帖子: pdf 文件夹 using new string 
2017-12-14 16:14
快速回复:请教关于C#合并pdf文件的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018788 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved