请教关于pdf插入的问题

无为无不为? 无为无不为? 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
请教关于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");
}
}
}
...全文
评论 2
收藏 已收藏
分享
返回顶部
无为无不为?
C# 111,129 成员 642,538 内容