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