- VisualStudio2022插件的安装及使用-编程手把手系列文章
- pprof-在现网场景怎么用
- C#实现的下拉多选框,下拉多选树,多级节点
- 【学习笔记】基础数据结构:猫树
由于项目需要编写大量的材料,以及各种签字表格、文书等,最后以PDF作为材料交付的文档格式,过程文档时有变化或补充,故此处理PDF文档已经成为日常工作的一部分。 网上有各种PDF处理工具,总是感觉用得不跟手。最后回顾自己的需求总结为以下几项: 1.可以便捷、快速的对多份PDF进行合并。 2.可以从源PDF选取指定页码进行合并。 3.可以从单个PDF提取特定页码(拆分PDF)。 4.对多个PDF分组,合并作为最终PDF的导航书签,可快速定位。 5.统一合成后PDF页面尺寸,如统一为A4幅面。 6.操作尽量简便,支持文件拖放,不需要花巧的东西.
首先,我们看看最终成品: ①.可以批量添加多个PDF到列表框中,也可以资料管理器将文件批量拖进来实现添加。 ②.[可选]定义分组标题对文件进行分组,也作为合并后PDF的书签。 ③.将列表中PDF批量合并到一个文件中。如果只有PDF,而且定义了页码范围,则转换为拆分功能。 ④.显示PDF总页数,如果只需提取部分内容,可以定义页码范围。 ⑤.可以更改合并后PDF页面的尺寸,统一为A4、B4或A5幅面.
搜索发现github有个开源的PdfBinder1.2(https://github.com/schourode/pdfbinder)比较接近想要的效果,本着能省即省、成本最低、能效更高的原则,直接以此为基础进行扩展,开发自身所需的功能.
这个比较简单,点击按钮后弹出选择对话框,将选择的文件逐一加到ListBox中.
private void addFileButton_Click(object sender, EventArgs e)
{
if (addFileDialog.ShowDialog() == DialogResult.OK)
{
foreach (string file in addFileDialog.FileNames)
{
AddInputFile(file);
}
UpdateUI();
}
}
其中AddInputFile函数单独编写是为了在拖放事件中复用.
public void AddInputFile(string file)
{
int Pages = 0;
switch (Combiner.TestSourceFile(file, out Pages))
{
case Combiner.SourceTestResult.Unreadable:
MessageBox.Show(string.Format(resources.GetString("Error.Unreadable.Text"), file), resources.GetString("Error.Unreadable.Title"), MessageBoxButtons.OK, MessageBoxIcon.Error);
break;
case Combiner.SourceTestResult.Protected:
MessageBox.Show(string.Format(resources.GetString("Error.Protected.Text"), file), resources.GetString("Error.Protected.Title"), MessageBoxButtons.OK, MessageBoxIcon.Hand);
break;
case Combiner.SourceTestResult.Ok:
FileListBox.Items.Add(new PdfInfo() { Fullname = file, Filename = Path.GetFileName(file), Ranges = "", TotalPages = Pages });
break;
}
}
这里对PDF文件有效性进行了检查,而且添加到ListBox的是PdfInfo对象,它还记录了总页数、提取的页面范围等信息。 文件拖放的实现:
private void FileListBox_DragEnter(object sender, DragEventArgs e)
{
e.Effect = e.Data.GetDataPresent(DataFormats.FileDrop, false) ? DragDropEffects.All : DragDropEffects.None;
}
private void FileListBox_DragDrop(object sender, DragEventArgs e)
{
var fileNames = (string[])e.Data.GetData(DataFormats.FileDrop);
Array.Sort(fileNames);
foreach (var file in fileNames)
{
AddInputFile(file);
}
UpdateUI();
}
using BookmarkName = System.String;
private void addBookmarkButton_Click(object sender, EventArgs e)
{
//未添加文件不处理
if (FileListBox.SelectedIndex < 0) return;
//如果选择的书签(组名),读取名称供修改
BookmarkName bookmark = "";
if (FileListBox.SelectedItem is BookmarkName)
bookmark = (BookmarkName)FileListBox.SelectedItem;
else
{
//如果选择的是文件,提取文件名作默认值
bookmark = ((PdfInfo)FileListBox.SelectedItem).Filename;
if (bookmark.Contains("."))
bookmark = bookmark.Substring(0, bookmark.LastIndexOf("."));
}
//如果输入有效,添加书签(组名)
BookmarkName newName = Interaction.InputBox(resources.GetString("SetBookmark.Prompt"), resources.GetString("SetBookmark.Title"), bookmark);
if (newName != "")
{
if (FileListBox.SelectedItem is BookmarkName)
FileListBox.Items[FileListBox.SelectedIndex] = newName;
else
{
FileListBox.Items.Insert(FileListBox.SelectedIndex, newName);
BookmarkCounter++;
}
}
}
没有定义页码范围表示整个PDF进行合并。定义了页面范围,合并时只提取相应的页面进行合并。 页码范围的格式与常见的打印功能的页码定义相一致,如:1,2,3,6-9。 这个操作放在右键弹出菜单中实现.
private void mnuSetPageRange_Click(object sender, EventArgs e)
{
PdfInfo item = ((PdfInfo)FileListBox.SelectedItem);
string range = Interaction.InputBox(resources.GetString("SetPageRange.Prompt"), resources.GetString("SetPageRange.Title"), item.Ranges);
//内容未变更的不用处理
if (range != item.Ranges)
{
if (range == "")
{
((PdfInfo)FileListBox.Items[FileListBox.SelectedIndex]).Ranges = "";
return;
}
//针对逗号和空格做处理
string[] arr = range.Replace(",", ",").Replace(" ", "").Split(',');
range = "";
for (int i = 0; i < arr.Length; i++)
{
//用正则表达式判断有效性
if ("" == arr[i]) continue;
if (Regex.IsMatch(arr[i], @"^\d+$") || Regex.IsMatch(arr[i], @"^\d+-\d+$"))
range += ("" == range ? "" : ",") + arr[i];
else
{
MessageBox.Show(resources.GetString("Error.RangeValid"));
return;
}
}
//输入有效,更新
((PdfInfo)FileListBox.Items[FileListBox.SelectedIndex]).Ranges = range;
UpdateUI();
}
}
为了在ListBox中显示书签、总页数和提取页码范围,需要接管ListBox的绘制事件.
private void FileListBox_DrawItem(object sender, DrawItemEventArgs e)
{
...
StringFormat Formater = new StringFormat();
Formater.Alignment = StringAlignment.Near;
Formater.LineAlignment = StringAlignment.Center;
Formater.Trimming = StringTrimming.EllipsisPath;
Formater.FormatFlags = StringFormatFlags.NoWrap;
//绘制书签(分组名)
if (FileListBox.Items[e.Index] is BookmarkName)
{
//绘书签(分组名)图标
e.Graphics.DrawImage(addBookmarkButton.Image, e.Bounds.X, e.Bounds.Y + ((e.Bounds.Height - addBookmarkButton.Image.Height) /2));
//绘书签(分组名)
e.Graphics.DrawString((BookmarkName)FileListBox.Items[e.Index], e.Font, Brushes.Black
, new Rectangle(e.Bounds.X + addBookmarkButton.Image.Width, e.Bounds.Y, e.Bounds.Width - RIGHT_MARGIN, e.Bounds.Height), Formater);
return;
}
//绘制PDF文件名
PdfInfo item = (PdfInfo)FileListBox.Items[e.Index];
e.Graphics.DrawString(showNameButton.Checked ? item.Fullname : item.Filename, e.Font, Brushes.Black
, new Rectangle(e.Bounds.X + (BookmarkCounter > 0 ? (int)(addBookmarkButton.Image.Width * 1.5) : 0), e.Bounds.Y, e.Bounds.Width - RIGHT_MARGIN, e.Bounds.Height), Formater);
//绘制页码
Formater.Alignment = StringAlignment.Far;
e.Graphics.DrawString((item.Ranges == "" ? "" : item.Ranges + " | ")
+ string.Format(item.TotalPages>1 ? resources.GetString("Pages"): resources.GetString("Page"), item.TotalPages)
, e.Font, Brushes.Gray, e.Bounds, Formater);
}
默认是原始尺寸(不做调整),可根据需要选择为A4、A5、B4.
private void OnPageSizeChanged(object sender, EventArgs e)
{
PageSizeButton.Tag = ((ToolStripMenuItem)sender).Tag;
mnuPageSize_Original.Checked = sender == mnuPageSize_Original;
mnuPageSize_A4.Checked = sender == mnuPageSize_A4;
mnuPageSize_A5.Checked = sender == mnuPageSize_A5;
mnuPageSize_B4.Checked = sender == mnuPageSize_B4;
if (mnuPageSize_Original.Checked)
PageSizeButton.Text = resources.GetString("PageSizeButton.Text");
else
PageSizeButton.Text = resources.GetString("PageSizeButton.Text") + ":" + ((ToolStripMenuItem)sender).Text;
}
这个比较长,有兴趣的可以到https://github.com/kacarton/PDFBinder2下载源码自己看,以下摘录核心部分.
private void combineButton_Click(object sender, EventArgs e)
{
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
using (var combiner = new Combiner(saveFileDialog.FileName, (PDFBinder.PageSize)PageSizeButton.Tag))
{
progressBar.Visible = true;
this.Enabled = false;
for (int i = 0; i < FileListBox.Items.Count; i++)
{
if (FileListBox.Items[i] is BookmarkName)
combiner.AddBookmark((string)FileListBox.Items[i]);
else
combiner.AddFile(((PdfInfo)FileListBox.Items[i]).Fullname, ((PdfInfo)FileListBox.Items[i]).Ranges);
//刷新进度
progressBar.Value = (int)(((i + 1) / (double)FileListBox.Items.Count) * 100);
}
this.Enabled = true;
progressBar.Visible = false;
}
System.Diagnostics.Process.Start(saveFileDialog.FileName);
}
}
class Combiner : IDisposable
{
public void AddFile(string fileName, string range)
{
var reader = new PdfReader(fileName);
....
_document.NewPage();
//添加书签
if (!string.IsNullOrEmpty(this.BookMarkName))
{
Chapter _chapter = new Chapter("", 1);
_chapter.BookmarkTitle = this.BookMarkName;
_chapter.BookmarkOpen = true;
_document.Add(_chapter);
this.BookMarkName = null;
}
if (_newPageSize == PageSize.Original)
{
var page = _pdfCopy.GetImportedPage(reader, i);
_pdfCopy.AddPage(page);
}
else
{
var page = _writer.GetImportedPage(reader, i);
_document.Add(iTextSharp.text.Image.GetInstance(page));
}
reader.Close();
}
}
UI同步、文件移除、上移、下移、排序、多语言支持这些比较简单就不展开了.
源码已发布在github上,网址:PDFBinder2 https://github.com/kacarton/PDFBinder2,欢迎交流.
最后此篇关于用C#写个PDF批量合并工具简化日常工作的文章就讲到这里了,如果你想了解更多关于用C#写个PDF批量合并工具简化日常工作的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我的代码有一些问题。我正在尝试遍历包含许多 PDF 的 Drive 文件夹,然后将它们合并为一个文件。当我使用我的代码时,它只是为 Drive 文件夹中的最后一个 PDF 创建一个 PDF,而不是按预
我从 PDF Specification 获取了 PDF 规范中的最小 PDF 示例。 ,将其复制到记事本,将文件重命名为扩展名为 .pdf。 我可以用其他 PDF 查看器(PDF-XChange、S
感谢您在以下方面的帮助: 我有 2 个部分可访问的 PDF(包含标签),我想使用一些命令行工具(如 PDFtk 或 Ghostscript,或任何 Perl 模块)将它们连接起来: 我已经尝试使用 P
我想使用 ghostscript 将矢量 pdf 转换为光栅 pdf(即光栅化矢量 pdf)。但是即使我添加了解析参数 -r300,我也找不到合适的参数来执行此操作。 我使用的代码是-dSAFER -
我无法在 FAQ 中找到这个功能是否存在于 API 中,尽管它在书中提到作为潜在可用的东西。有没有人有任何实现此功能的经验? 最佳答案 在 This thread (日期为 2007 年 6 月)Pa
我要放文件sample.pdf在我的网站上,并希望使用 pdf.js 显示它.我想要的是显示我自己的文件,如 demo ,带有工具栏,放大/缩小等。到目前为止,我还不能这样做。 我确实检查了 hell
我知道这可能不是严格意义上的编程问题(也许是,我不知道)但我在尝试转换常规 pdf(带有超链接、书签、图像、嵌入字体等)时遇到了严重问题.) 转换为 PDF/A-1 格式。 当我用 pdfaPilot
这是 PDF.js 网站 https://github.com/mozilla/pdf.js 我正在搜索和阅读很多文章,大多数编码都是将 pdf 导入 pdf.js 并在浏览器上显示,我不明白是不是
谁能建议我如何将扫描图像转换为可搜索图像或如何将扫描 pdf 转换为可搜索 pdf? 很长一段时间以来,我一直陷入这种情况。 我已经在 ubuntu 中尝试过 pdfocr 应用程序,但没有成功。 最
作为我对客户端/服务器 pdf 签名研究的一部分,我测试了 itext pdf 延迟签名示例。不幸的是,我生成的 pdf 即合并空签名 pdf 和哈希值的输出显示无效签名。 我的代码片段如下 cla
我想将一个 PDF 页面插入到另一个已缩放的 PDF 页面中。我想使用 iTextSharp 来实现此目的。 我有一个矢量绘图,可以导出为单页 PDF 文件。我想将此文件添加到其他 PDF 文档的页面
作为我对客户端/服务器 pdf 签名研究的一部分,我测试了 itext pdf 延迟签名示例。不幸的是,我生成的 pdf 即合并空签名 pdf 和哈希值的输出显示无效签名。 我的代码片段如下 cla
我想为 Kindle 转换电子书。我尝试使用 Calibre 将具有复杂格式样式和图像的基于两种语言的基于文本的大型 PDF 电子书转换为适用于 Kindle 的 AZW3 电子书,并且还尝试了亚马逊
我在 Google Chrome 中显示 pdf 时遇到问题。问题是 Chrome 将 pdf 的某些页面显示为黑色。 启用 Chrome PDF 查看器时会发生这种情况。如果我禁用此插件并使用 Ad
我确信这个问题无处不在,尽管我似乎找不到答案。我希望我的 PDF 文档在 PDF 阅读器中显示时没有空白页,但随后在封面后打印空白页,这样打印出来的文档在右侧甚至左侧都有奇数页。还有其他人遇到过这个问
我需要自动裁剪 pdf 文件(去除白边)。到目前为止,我尝试了两种并不完美的工具: pdf裁剪 问题:它不会裁剪某些 pdf。 pdf-crop-margins 问题:有时它裁剪得太多(精细的细节)。
This PDF由几个源文件组成。其中五个是包含 alpha channel 的 PNG。一种是没有 alpha channel 的 PNG。最后一 block 是带有透明效果的 Photoshop
我的团队将内部 wiki 页面用于各种内容。这些页面是使用 MediaWiki 创建的。我想知道是否有任何方法可以将 wiki 页面转换为 PDF 格式。我必须用它来将用户文档转换为 PDF 格式,以
我希望能够从我可能在数据库或 xml 或任何其他结构化形式中拥有的数据生成高度图形化(也包含大量文本内容)的 PDF 文件。 目前,我们的平面设计师在将内容作为 MS Word 文档后,在 Photo
我正在寻找可以帮助我找到重复 PDF 的实用程序。问题:我有 1000 个 PDF 文件。有些是重复的。由于不同的文件名和文件大小的微小差异,它们不容易被检测到。是否有实用程序/算法/库可以帮助我找到
我是一名优秀的程序员,十分优秀!