| using System; |
| using System.Drawing; |
| using System.Text; |
| using Aspose.Pdf; |
| using Aspose.Pdf.Text; |
| |
| namespace replacePDF |
| { |
| |
| class Program |
| { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| 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); |
| |
| |
| |
| |
| pdfDocument.Pages.Accept(textFragmentAbsorber); |
| |
| |
| TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments; |
| |
| |
| foreach (TextFragment textFragment in textFragmentCollection) |
| { |
| try |
| { |
| count_replace ++; |
| |
| if (style.Index>0 && style.Index == count_replace) continue; |
| |
| |
| |
| 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(); |
| } |
| |
| } |
| } |
| }COPY |