标题: replacePDF 替换PDF文字[v1.0] [打印本页]
作者: CrLf 时间: 2017-10-15 00:07 标题: replacePDF 替换PDF文字[v1.0]
本帖最后由 CrLf 于 2017-10-15 00:37 编辑
下载地址:http://www.bathome.net/s/tool/?down=replacePDF
附上中文说明:
replacePDF - 替换PDF文字 version 1.0 by CrLf [bathome.net]
Usage: replacePDF.exe textSrc textDst fileSrc fileDst [/option value]
textSrc 替换前的文字
textDst 替换后的文字
fileSrc 来源的文件
fileDst 保存的文件
/index num 只替换第 N 个匹配项
/font fontName 指定替换后的字体名,例如 "Verdana"
/size fontSize 指定替换后的字号,例如 12.5
/color color 指定文字颜色,例如 "red" 或 "#ff0000"
/bgcolor color 指定背景颜色,例如 "red" 或 "#ff0000"
/underline bool 指定是否加上下划线,例如 "true" 或 "false"
以下为 C# 源码,基于 .Net2.0,依赖 Aspose.Pdf.dll(2015年的10.1版),若要打包单文件版请自行引入 dll 资源,并将最开始处的注释取消- using System;
- using System.Drawing;
- using System.Text;
- using Aspose.Pdf;
- using Aspose.Pdf.Text;
-
- namespace replacePDF
- {
-
- class Program
- {
- //以下注释中的代码用于打包单文件版
-
- //static Program() {
- // AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
- //}
-
- //static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
- //{
- // //获取加载失败的程序集的全名
- // var assName = new System.Reflection.AssemblyName(args.Name).FullName;
- // if (args.Name == "Aspose.Pdf, Version=10.1.0.0, Culture=neutral, PublicKeyToken=00725b1ceb58d0a9")
- // {
- // //读取资源
- // using (var stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("replacePDF.Aspose.Pdf.dll"))
- // {
- // var bytes = new byte[stream.Length];
- // stream.Read(bytes, 0, (int)stream.Length);
- // return System.Reflection.Assembly.Load(bytes);//加载资源文件中的dll,代替加载失败的程序集
- // }
- // }
- // throw new DllNotFoundException(assName);
- //}
-
- class textStyle
- {
- public bool hasFont = false;
- public Aspose.Pdf.Text.Font Font;
-
- public int Underline = -1;
- public int Index;
-
- public float Size = -1;
- public float LineSpacing = -1;
-
- public System.Drawing.Color Color = System.Drawing.Color.Empty;
- public System.Drawing.Color BgColor = System.Drawing.Color.Empty;
- }
-
- static void Usage()
- {
- Console.WriteLine(
- "replacePDF - Replace text in Pdf. version 1.0 by CrLf [bathome.net]\r\n" +
- "\r\n" +
- "Usage: replacePDF.exe textSrc textDst fileSrc fileDst [/option value]\r\n"+
- " textSrc The text before replace\r\n"+
- " textDst The text after replace\r\n"+
- " fileSrc Specifies the source file to replace\r\n"+
- " fileDst Specifies the file to save\r\n" +
- " /index num Replace on the NO.? Matchs\r\n" +
- " /font fontName Specifies the font name, such as \"Verdana\"\r\n"+
- " /size fontSize Specifies the font size, such as 12.5\r\n"+
- " /color color Specifies the text color, such as \"red\" or \"#ff0000\"\r\n"+
- " /bgcolor color Specifies the background color, such as \"red\" or \"#ff0000\"\r\n"+
- " /underline bool Enable text underline, such as \"true\" or \"false\"\r\n"
- );
- exit(0);
- }
-
- static int Replace(String textSrc, String textDst, String fileSrc, String fileDst, textStyle style)
- {
- int count_replace = 0,
- count_success = 0,
- count_fail = 0;
-
- Document pdfDocument = new Document(fileSrc);
-
- TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(textSrc);
- //accept the absorber for all the pages
-
- //textFragmentAbsorber.TextSearchOptions.;
-
- pdfDocument.Pages.Accept(textFragmentAbsorber);
- //get the extracted text fragments
-
- TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;
- //loop through the fragments
-
- foreach (TextFragment textFragment in textFragmentCollection)
- {
- try
- {
- count_replace ++;
-
- if (style.Index>0 && style.Index == count_replace) continue;
-
- //update text and other properties
-
- textFragment.Text = textDst;
-
- if (style.hasFont) textFragment.TextState.Font = style.Font;
- if (style.Size >= 0) textFragment.TextState.FontSize = style.Size;
- if (style.LineSpacing >= 0) textFragment.TextState.LineSpacing = style.LineSpacing;
-
- if (style.Color != System.Drawing.Color.Empty) textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(style.Color);
- if (style.BgColor != System.Drawing.Color.Empty) textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(style.BgColor);
-
- if (style.Underline >= 0) textFragment.TextState.Underline = style.Underline > 0;
-
- count_success ++;
-
- Console.Error.WriteLine("* Replaced["+count_success+"]: Successfully on page "+textFragment.Page.Number);
- }
- catch (Exception e)
- {
- count_fail ++;
- Console.Error.WriteLine("* Error[" + count_fail + "]: " + e.Message);
- }
- }
-
- pdfDocument.Save(fileDst);
-
- if (count_success != 0)
- {
- Console.WriteLine("Success: \"" + fileSrc + "\"");
- }
- else
- {
- Console.WriteLine("Fail: \"" + fileSrc + "\"");
- }
-
- return count_fail != 0 ? 1 : 0;
- }
-
- static void exit(int exitCode) {
- Environment.Exit(exitCode);
- }
-
- static void Main(string[] args)
- {
- String textSrc = "",
- textDst = "",
- fileSrc = "",
- fileDst = "";
-
- textStyle style = new textStyle();
-
- if (args.Length >= 4)
- {
- textSrc = args[0];
- textDst = args[1];
-
- fileSrc = args[2];
- fileDst = args[3];
-
- for (int i = 4; i < args.Length; i += 2)
- {
- String option = args[i].ToLower();
- String value;
-
- try
- {
- switch (option)
- {
- case "/help":
-
- case "/?":
- Usage();
- break;
- }
-
- if (i + 1 >= args.Length)
- {
- throw new Exception("Error: Wrong value \"\".");
- }
- else
- {
- value = args[i + 1];
- }
-
-
- switch (option)
- {
- case "/font":
- style.Font = FontRepository.FindFont(value);
- style.hasFont = true;
- break;
-
- case "/size":
- if (!float.TryParse(value, out style.Size) || style.Size <= 0)
- {
- throw new Exception("Error: Wrong value \"" + value + "\".");
- }
- break;
-
- case "/index":
- if (!int.TryParse(value, out style.Index) || style.Index <= 0)
- {
- throw new Exception("Error: Wrong value \"" + value + "\".");
- }
- break;
-
- case "/height":
- if (!float.TryParse(value, out style.LineSpacing) || style.LineSpacing <= 0)
- {
- throw new Exception("Error: Wrong value \"" + value + "\".");
- }
- break;
-
- case "/color":
- style.Color = System.Drawing.ColorTranslator.FromHtml(value);
- break;
-
- case "/bgcolor":
- style.BgColor = System.Drawing.ColorTranslator.FromHtml(value);
- break;
-
- case "/underline":
- if (value.ToLower() == "true")
- {
- style.Underline = 1;
- }
- else if (value.ToLower() == "false")
- {
- style.Underline = 0;
- }
- else
- {
- throw new Exception("Error: Wrong value \"" + value + "\".");
- }
- break;
-
- default:
- throw new Exception("Error: Unknow option \"" + option + "\".");
- }
- }
- catch (Exception e)
- {
- Console.Error.WriteLine("Error: " + e.Message);
- Environment.Exit(2);
- }
- }
-
- try
- {
- int exitCode = Replace(textSrc, textDst, fileSrc, fileDst, style);
- Environment.Exit(exitCode);
- }
- catch (Exception e)
- {
- Console.Error.WriteLine("Error: " + e.Message);
- }
- }
- else {
- Usage();
- }
-
- }
- }
- }
复制代码
作者: happy886rr 时间: 2017-10-15 00:57
本帖最后由 happy886rr 于 2017-10-15 01:06 编辑
回复 1# CrLf
这么久了,最近才发现大师的踪影。
作者: CrLf 时间: 2017-10-15 01:57
回复 2# happy886rr
:)
作者: zhanglei1371 时间: 2017-10-15 10:29
请教下大师,如何在pdf中插入一个空白页?这个dll能否实现?我用acrobat的dll,没有发现这样的操作,只能手动搞。
作者: CrLf 时间: 2017-10-15 12:29
本帖最后由 CrLf 于 2017-10-15 14:10 编辑
回复 4# zhanglei1371
acrobat 没这个功能?不太可能吧
aspose 确实有 Pages.Insert,这里用 powershell 作个例子:- [reflection.assembly]::LoadFile("z:\Aspose.Pdf.dll")
- $pdf=[aspose.pdf.document]"input.pdf"
- $pages = $pdf.Pages
-
- $page_old = $pages[1]
- $width = $page_old.Rect.Width
- $height = $page_old.Rect.Height
-
- $page_new = $pages.Insert(1)
- $page_new.SetPageSize($width, $height)
-
- $pdf.save("output.pdf")
复制代码
作者: zhanglei1371 时间: 2017-10-15 13:52
谢谢,我研究下这个。不过acroba似乎却是没提供新建空白页的代码,请教个几个人都没解决。虽然里面有insert,只能插入现有文件,但无法插入空白页。除非新建个空的只有一页的pdf插进去
作者: CrLf 时间: 2017-10-15 14:13
回复 6# zhanglei1371
复制页面再删除元素呢?
总觉得应该是有直接插入空页面的办法,这么基础的功能要是没有那也太奇怪了
另外,powershell 的实现方式改了一下
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |