请教关于pdf插入的问题

无为无不为? 2017-06-19 03:33:56
请教关于pdf插入的问题
目标:
文件夹1中有1101、1102、1103等文件夹,文件夹里面有对应名称的1101、1102、1103等pdf文件,有5页到10页不等。
文件夹2中有1101、1102、1103等pdf文件,只有一页,这一页正在1目录下对应文件名的pdf第三页所缺少页。
文件夹1内容: 文件夹2内容:




思路:借助第三方软件Spire.PDF
1、 遍历获取文件夹1中所有pdf文件(完整路径并放在listbox1中),遍历文件夹2中所有pdf文件,放在listbox2中。
2、 判断如果说listbox2中路径及文件名完全等于listbox1的路径及文件名,采用pdf的复制插入操作,循环次数以2文件夹中pdf个数为依据,2中没有的pdf就不用管。
不知道这么想是否正确?请老师指教为谢!
代码实现了思路1功能,但到第二步代码写不出来,还请各位老师教教学生,谢谢!
附窗体及代码,也在网上找到一部分教程,无法参透。



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.IO;

namespace wenjianjia
{
public partial class MergePDF : Form
{
public MergePDF()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog FBDialog = new FolderBrowserDialog();
if (FBDialog.ShowDialog() == DialogResult.OK)
{
string strPath = FBDialog.SelectedPath;
if (strPath.EndsWith("\\"))
textBox1.Text = strPath;
else
textBox1.Text = strPath + "\\";
}
DirectoryInfo theFolder = new DirectoryInfo(textBox1.Text);
DirectoryInfo[] dirInfo = theFolder.GetDirectories();
foreach (DirectoryInfo NextFolder in dirInfo)
{
FileInfo[] fileInfo = NextFolder.GetFiles("*.pdf");
foreach (FileInfo NextFile in fileInfo)
{
this.listBox1.Items.Add(theFolder+NextFile.Name);
}
}
}

private void button2_Click(object sender, EventArgs e)
{
FolderBrowserDialog FYDialog = new FolderBrowserDialog();
if (FYDialog.ShowDialog() == DialogResult.OK)
{
string stPath = FYDialog.SelectedPath;
if (stPath.EndsWith("\\"))
textBox2.Text = stPath;
else
textBox2.Text = stPath + "\\";
}
DirectoryInfo eFolder = new DirectoryInfo(textBox2.Text);
DirectoryInfo[] rInfo = eFolder.GetDirectories();
foreach (DirectoryInfo NextFolder in rInfo)
{
FileInfo[] eInfo = NextFolder.GetFiles("*.pdf");
foreach (FileInfo tFile in eInfo)
{
this.listBox2.Items.Add(eFolder+tFile.Name);
}
}
}
}
}
网上百度到的pdf代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;

namespace 复制PDF页面到另一个PDF文档
{
class Program
{
static void Main(string[] args)
{
PdfDocument doc1 = newPdfDocument();
doc1.LoadFromFile("童话故事.pdf");

PdfDocument doc2 = newPdfDocument();
doc2.LoadFromFile("各种点心的做法.pdf");

PdfPageBase page = doc1.Pages[0];
SizeF size = page.Size;
PdfTemplate template = page.CreateTemplate();

doc2.Pages.Insert(1, size, new PdfMargins(0, 0));
doc2.Pages[1].Canvas.DrawTemplate(template, newPointF(0, 0));

doc2.SaveToFile("复制.pdf");
System.Diagnostics.Process.Start("复制.pdf");
}
}
}
...全文
291 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
无为无不为? 2017-07-05
  • 打赏
  • 举报
回复
[quote=引用 1 楼 Eiceblue 的回复:] 文件名存放在listbox中不太方便比较,你可以使用存在集合的方式。请参考以下代码,希望对你有帮助。
//获取文件夹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)
            {
                if (Files2.ContainsKey(name))
                {

                    PdfDocument pdf1 = new PdfDocument();
                    pdf1.LoadFromFile(Files1[name]);
                    PdfDocument pdf2 = new PdfDocument();
                    pdf2.LoadFromFile(Files2[name]);
                    //获取文件夹2中pdf的第一页插入到对应pdf的第三页
                    PdfPageBase page = pdf2.Pages[0];
                    SizeF size = page.Size;
                    PdfTemplate template = page.CreateTemplate();
                    pdf1.Pages.Insert(1, size, new PdfMargins(0, 0));
                    pdf1.Pages[2].Canvas.DrawTemplate(template, new PointF(0, 0));
                    pdf1.SaveToFile(Files1[name]);
                }

            }
[/quote谢谢老师指导,对不起,我点错了一下,
Eiceblue 2017-06-27
  • 打赏
  • 举报
回复
文件名存放在listbox中不太方便比较,你可以使用存在集合的方式。请参考以下代码,希望对你有帮助。
//获取文件夹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)
            {
                if (Files2.ContainsKey(name))
                {

                    PdfDocument pdf1 = new PdfDocument();
                    pdf1.LoadFromFile(Files1[name]);
                    PdfDocument pdf2 = new PdfDocument();
                    pdf2.LoadFromFile(Files2[name]);
                    //获取文件夹2中pdf的第一页插入到对应pdf的第三页
                    PdfPageBase page = pdf2.Pages[0];
                    SizeF size = page.Size;
                    PdfTemplate template = page.CreateTemplate();
                    pdf1.Pages.Insert(1, size, new PdfMargins(0, 0));
                    pdf1.Pages[2].Canvas.DrawTemplate(template, new PointF(0, 0));
                    pdf1.SaveToFile(Files1[name]);
                }

            }

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧