[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

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 资源,并将最开始处的注释取消
  1. using System;
  2. using System.Drawing;
  3. using System.Text;
  4. using Aspose.Pdf;
  5. using Aspose.Pdf.Text;
  6. namespace replacePDF
  7. {
  8.     class Program
  9.     {
  10.         //以下注释中的代码用于打包单文件版
  11.         //static Program() {
  12.         //    AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
  13.         //}
  14.         
  15.         //static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
  16.         //{
  17.         //    //获取加载失败的程序集的全名
  18.         //    var assName = new System.Reflection.AssemblyName(args.Name).FullName;
  19.         //    if (args.Name == "Aspose.Pdf, Version=10.1.0.0, Culture=neutral, PublicKeyToken=00725b1ceb58d0a9")
  20.         //    {
  21.         //        //读取资源
  22.         //        using (var stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("replacePDF.Aspose.Pdf.dll"))
  23.         //        {
  24.         //            var bytes = new byte[stream.Length];
  25.         //            stream.Read(bytes, 0, (int)stream.Length);
  26.         //            return System.Reflection.Assembly.Load(bytes);//加载资源文件中的dll,代替加载失败的程序集
  27.         //        }
  28.         //    }
  29.         //    throw new DllNotFoundException(assName);
  30.         //}
  31.         class textStyle
  32.         {
  33.             public bool hasFont = false;
  34.             public Aspose.Pdf.Text.Font Font;
  35.             public int Underline = -1;
  36.             public int Index;
  37.             public float Size = -1;
  38.             public float LineSpacing = -1;
  39.             public System.Drawing.Color Color = System.Drawing.Color.Empty;
  40.             public System.Drawing.Color BgColor = System.Drawing.Color.Empty;
  41.         }
  42.    
  43.         static void Usage()
  44.         {
  45.             Console.WriteLine(
  46.                 "replacePDF - Replace text in Pdf.         version 1.0  by CrLf [bathome.net]\r\n" +
  47.                 "\r\n" +
  48.                 "Usage: replacePDF.exe  textSrc textDst fileSrc fileDst [/option value]\r\n"+
  49.                 "  textSrc         The text before replace\r\n"+
  50.                 "  textDst         The text after replace\r\n"+
  51.                 "  fileSrc         Specifies the source file to replace\r\n"+
  52.                 "  fileDst         Specifies the file to save\r\n" +
  53.                 "  /index num      Replace on the NO.? Matchs\r\n" +
  54.                 "  /font fontName  Specifies the font name, such as \"Verdana\"\r\n"+
  55.                 "  /size fontSize  Specifies the font size, such as 12.5\r\n"+
  56.                 "  /color color    Specifies the text color, such as \"red\" or \"#ff0000\"\r\n"+
  57.                 "  /bgcolor color  Specifies the background color, such as \"red\" or \"#ff0000\"\r\n"+
  58.                 "  /underline bool Enable text underline, such as \"true\" or \"false\"\r\n"
  59.                 );
  60.             exit(0);
  61.         }
  62.         static int Replace(String textSrc, String textDst, String fileSrc, String fileDst, textStyle style)
  63.         {
  64.             int count_replace = 0,
  65.                 count_success = 0,
  66.                 count_fail = 0;
  67.             Document pdfDocument = new Document(fileSrc);
  68.             TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(textSrc);
  69.             //accept the absorber for all the pages
  70.             //textFragmentAbsorber.TextSearchOptions.;
  71.             
  72.             pdfDocument.Pages.Accept(textFragmentAbsorber);
  73.             //get the extracted text fragments
  74.             TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;
  75.             //loop through the fragments
  76.             
  77.             foreach (TextFragment textFragment in textFragmentCollection)
  78.             {
  79.                 try
  80.                 {
  81.                     count_replace ++;
  82.                     if (style.Index>0 && style.Index == count_replace) continue;
  83.                     //update text and other properties
  84.                     textFragment.Text = textDst;
  85.                     if (style.hasFont)          textFragment.TextState.Font = style.Font;
  86.                     if (style.Size >= 0)        textFragment.TextState.FontSize = style.Size;
  87.                     if (style.LineSpacing >= 0) textFragment.TextState.LineSpacing = style.LineSpacing;
  88.                     if (style.Color != System.Drawing.Color.Empty)   textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(style.Color);
  89.                     if (style.BgColor != System.Drawing.Color.Empty) textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(style.BgColor);
  90.                     if (style.Underline >= 0) textFragment.TextState.Underline = style.Underline > 0;
  91.                     count_success ++;
  92.                     Console.Error.WriteLine("* Replaced["+count_success+"]: Successfully on page "+textFragment.Page.Number);
  93.                 }
  94.                 catch (Exception e)
  95.                 {
  96.                     count_fail ++;
  97.                     Console.Error.WriteLine("* Error[" + count_fail + "]: " + e.Message);
  98.                 }
  99.             }
  100.             pdfDocument.Save(fileDst);
  101.             if (count_success != 0)
  102.             {
  103.                 Console.WriteLine("Success: \"" + fileSrc + "\"");
  104.             }
  105.             else
  106.             {
  107.                 Console.WriteLine("Fail: \"" + fileSrc + "\"");
  108.             }
  109.             return count_fail != 0 ? 1 : 0;
  110.         }
  111.         static void exit(int exitCode) {
  112.             Environment.Exit(exitCode);
  113.         }
  114.         static void Main(string[] args)
  115.         {
  116.             String  textSrc = "",
  117.                     textDst = "",
  118.                     fileSrc = "",
  119.                     fileDst = "";
  120.             textStyle style = new textStyle();
  121.             if (args.Length >= 4)
  122.             {
  123.                 textSrc = args[0];
  124.                 textDst = args[1];
  125.                 fileSrc = args[2];
  126.                 fileDst = args[3];
  127.                 for (int i = 4; i < args.Length; i += 2)
  128.                 {
  129.                     String option = args[i].ToLower();
  130.                     String value;
  131.                     
  132.                     try
  133.                     {
  134.                         switch (option)
  135.                         {
  136.                             case "/help":
  137.                             case "/?":
  138.                                 Usage();
  139.                                 break;
  140.                         }
  141.                         if (i + 1 >= args.Length)
  142.                         {
  143.                             throw new Exception("Error: Wrong value \"\".");
  144.                         }
  145.                         else
  146.                         {
  147.                             value = args[i + 1];
  148.                         }
  149.                     
  150.                         switch (option)
  151.                         {
  152.                             case "/font":
  153.                                 style.Font = FontRepository.FindFont(value);
  154.                                 style.hasFont = true;
  155.                                 break;
  156.                             case "/size":
  157.                                 if (!float.TryParse(value, out style.Size) || style.Size <= 0)
  158.                                 {
  159.                                     throw new Exception("Error: Wrong value \"" + value + "\".");
  160.                                 }
  161.                                 break;
  162.                                 
  163.                             case "/index":
  164.                                 if (!int.TryParse(value, out style.Index) || style.Index <= 0)
  165.                                 {
  166.                                     throw new Exception("Error: Wrong value \"" + value + "\".");
  167.                                 }
  168.                                 break;
  169.                             case "/height":
  170.                                 if (!float.TryParse(value, out style.LineSpacing) || style.LineSpacing <= 0)
  171.                                 {
  172.                                     throw new Exception("Error: Wrong value \"" + value + "\".");
  173.                                 }
  174.                                 break;
  175.                                 
  176.                             case "/color":
  177.                                 style.Color = System.Drawing.ColorTranslator.FromHtml(value);
  178.                                 break;
  179.                             case "/bgcolor":
  180.                                 style.BgColor = System.Drawing.ColorTranslator.FromHtml(value);
  181.                                 break;
  182.                             case "/underline":
  183.                                 if (value.ToLower() == "true")
  184.                                 {
  185.                                     style.Underline = 1;
  186.                                 }
  187.                                 else if (value.ToLower() == "false")
  188.                                 {
  189.                                     style.Underline = 0;
  190.                                 }
  191.                                 else
  192.                                 {
  193.                                     throw new Exception("Error: Wrong value \"" + value + "\".");
  194.                                 }
  195.                                 break;
  196.                                 
  197.                             default:
  198.                                 throw new Exception("Error: Unknow option \"" + option + "\".");
  199.                         }
  200.                     }
  201.                     catch (Exception e)
  202.                     {
  203.                         Console.Error.WriteLine("Error: " + e.Message);
  204.                         Environment.Exit(2);
  205.                     }
  206.                 }
  207.                 try
  208.                 {
  209.                     int exitCode = Replace(textSrc, textDst, fileSrc, fileDst, style);
  210.                     Environment.Exit(exitCode);
  211.                 }
  212.                 catch (Exception e)
  213.                 {
  214.                     Console.Error.WriteLine("Error: " + e.Message);
  215.                 }
  216.             }
  217.             else {
  218.                 Usage();
  219.             }
  220.         }
  221.     }
  222. }
复制代码
4

评分人数

本帖最后由 happy886rr 于 2017-10-15 01:06 编辑

回复 1# CrLf
这么久了,最近才发现大师的踪影。

TOP

回复 2# happy886rr


    :)

TOP

请教下大师,如何在pdf中插入一个空白页?这个dll能否实现?我用acrobat的dll,没有发现这样的操作,只能手动搞。

TOP

本帖最后由 CrLf 于 2017-10-15 14:10 编辑

回复 4# zhanglei1371


    acrobat 没这个功能?不太可能吧
    aspose 确实有 Pages.Insert,这里用 powershell 作个例子:
  1. [reflection.assembly]::LoadFile("z:\Aspose.Pdf.dll")
  2. $pdf=[aspose.pdf.document]"input.pdf"
  3. $pages = $pdf.Pages
  4. $page_old = $pages[1]
  5. $width = $page_old.Rect.Width
  6. $height = $page_old.Rect.Height
  7. $page_new = $pages.Insert(1)
  8. $page_new.SetPageSize($width, $height)
  9. $pdf.save("output.pdf")
复制代码
1

评分人数

    • yu2n: 感谢分享技术 + 1

TOP

谢谢,我研究下这个。不过acroba似乎却是没提供新建空白页的代码,请教个几个人都没解决。虽然里面有insert,只能插入现有文件,但无法插入空白页。除非新建个空的只有一页的pdf插进去

TOP

回复 6# zhanglei1371


    复制页面再删除元素呢?
    总觉得应该是有直接插入空页面的办法,这么基础的功能要是没有那也太奇怪了
    另外,powershell 的实现方式改了一下

TOP

返回列表