PDFTrace.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // Copyright (C) 2004-2025 Artifex Software, Inc.
  2. //
  3. // This file is part of MuPDF.
  4. //
  5. // MuPDF is free software: you can redistribute it and/or modify it under the
  6. // terms of the GNU Affero General Public License as published by the Free
  7. // Software Foundation, either version 3 of the License, or (at your option)
  8. // any later version.
  9. //
  10. // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
  11. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. // details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
  17. //
  18. // Alternative licensing terms are available from the licensor.
  19. // For commercial licensing, see <https://www.artifex.com/> or contact
  20. // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  21. // CA 94129, USA, for further information.
  22. package example;
  23. import com.artifex.mupdf.fitz.*;
  24. public class PDFTrace extends PDFProcessor
  25. {
  26. protected String formatFloatArray(float[] array, String suffix) {
  27. StringBuilder sb = new StringBuilder("[ ");
  28. int n = array.length;
  29. for (int i = 0; i < n; i++)
  30. sb.append(array[i] + " ");
  31. sb.append(']');
  32. if (suffix != null)
  33. sb.append(suffix);
  34. return sb.toString();
  35. }
  36. protected String formatString(String s, String suffix) {
  37. StringBuilder sb = new StringBuilder("(");
  38. int n = s.length();
  39. for (int i = 0; i < n; i++)
  40. {
  41. char c = s.charAt(i);
  42. if (c == '(' || c == ')' || c == '\\')
  43. sb.append('\\');
  44. sb.append(c);
  45. }
  46. sb.append(')');
  47. if (suffix != null)
  48. sb.append(suffix);
  49. return sb.toString();
  50. }
  51. protected String formatByteArray(byte[] array, String suffix) {
  52. StringBuilder sb = new StringBuilder("<");
  53. int n = array.length;
  54. for (int i = 0; i < n; i++)
  55. sb.append(String.format("%02x", array[i]));
  56. sb.append('>');
  57. if (suffix != null)
  58. sb.append(suffix);
  59. return sb.toString();
  60. }
  61. protected String formatObjectArray(Object[] array, String suffix) {
  62. StringBuilder sb = new StringBuilder('[');
  63. int n = array.length;
  64. for (int i = 0; i < n; i++)
  65. {
  66. if (array[i] instanceof Float)
  67. sb.append(((Float) array[i]).toString() + " ");
  68. else if (array[i] instanceof String)
  69. sb.append(formatString((String) array[i], " "));
  70. else
  71. sb.append(formatByteArray((byte[]) array[i], " "));
  72. }
  73. sb.append("]");
  74. if (suffix != null)
  75. sb.append(suffix);
  76. return sb.toString();
  77. }
  78. public void pushResources(PDFObject resources) { }
  79. public void popResources() { }
  80. public void op_gs(String name, PDFObject extgstate) { }
  81. public void op_w(float lineWidth) { System.out.println(lineWidth+ " w"); }
  82. public void op_j(float lineJoin) { System.out.println(lineJoin + " j"); }
  83. public void op_J(float lineCap) { System.out.println(lineCap + " J"); }
  84. public void op_M(float miterLimit) { System.out.println(miterLimit + " M"); }
  85. public void op_d(float[] array, float phase) { System.out.print("[ "); for (int i = 0; i < array.length; i++) System.out.print(array[i] + " "); System.out.println("] " + phase + " d"); }
  86. public void op_ri(String intent) { System.out.println(intent + " ri"); }
  87. public void op_i(float flatness) { System.out.println(flatness + "i"); }
  88. public void op_q() { System.out.println("q"); }
  89. public void op_Q() { System.out.println("Q"); }
  90. public void op_cm(float a, float b, float c, float d, float e, float f) { System.out.println(a + " " + b + " " + c + " " + d + " " + e + " " + f + " cm"); }
  91. public void op_m(float x, float y) { System.out.println(x + " " + y + " m"); }
  92. public void op_l(float x, float y) { System.out.println(x + " " + y + " l"); }
  93. public void op_c(float x1, float y1, float x2, float y2, float x3, float y3) { System.out.println(x1 + " " + y1 + " " + x2 + " " + y2 + " " + x3 + " " + y3 + " c"); }
  94. public void op_v(float x2, float y2, float x3, float y3) { System.out.println(x2 + " " + y2 + " " + x3 + " " + y3 + " v"); }
  95. public void op_y(float x1, float y1, float x3, float y3) { System.out.println(x1 + " " + y1 + " " + x3 + " " + y3 + " f"); }
  96. public void op_h() { System.out.println("h"); }
  97. public void op_re(float x, float y, float w, float h) { System.out.println(x + " " + y + " " + w + " " + h + " re"); }
  98. public void op_S() { System.out.println("S"); }
  99. public void op_s() { System.out.println("s"); }
  100. public void op_F() { System.out.println("F"); }
  101. public void op_f() { System.out.println("f"); }
  102. public void op_fstar() { System.out.println("f*"); }
  103. public void op_B() { System.out.println("B"); }
  104. public void op_Bstar() { System.out.println("B*"); }
  105. public void op_b() { System.out.println("b"); }
  106. public void op_bstar() { System.out.println("b*"); }
  107. public void op_n() { System.out.println("n"); }
  108. public void op_W() { System.out.println("W"); }
  109. public void op_Wstar() { System.out.println("W*"); }
  110. public void op_BT() { System.out.println("BT"); }
  111. public void op_ET() { System.out.println("ET"); }
  112. public void op_Tc(float charspace) { System.out.println(charspace + " Tc"); }
  113. public void op_Tw(float wordspace) { System.out.println(wordspace + " Tw"); }
  114. public void op_Tz(float scale) { System.out.println(scale + " Tz"); }
  115. public void op_TL(float leading) { System.out.println(leading + " TL"); }
  116. public void op_Tf(String name, float size) { System.out.println("/" + name + " " + size + " Tf"); }
  117. public void op_Tr(float render) { System.out.println(render + " Tr"); }
  118. public void op_Ts(float rise) { System.out.println(rise + " Ts"); }
  119. public void op_Td(float tx, float ty) { System.out.println(tx + " " + ty + " Td"); }
  120. public void op_TD(float tx, float ty) { System.out.println(tx + " " + ty + " TD"); }
  121. public void op_Tm(float a, float b, float c, float d, float e, float f) { System.out.println(a + " " + b + " " + c + " " + d + " " + e + " " + f + " Tm"); }
  122. public void op_Tstar() { System.out.println("T*"); }
  123. public void op_TJ(Object[] array) { System.out.println(formatObjectArray(array, " TJ")); }
  124. public void op_Tj(byte[] array) { System.out.println(formatByteArray(array, " Tj")); }
  125. public void op_Tj(String string) { System.out.println(formatString(string, " Tj")); }
  126. public void op_squote(String text) { System.out.println(formatString(text, " '")); }
  127. public void op_squote(byte[] array) { System.out.println(formatByteArray(array, " '")); }
  128. public void op_dquote(float aw, float ac, String text) { System.out.println(aw + " " + ac + " " + formatString(text, " \"")); }
  129. public void op_dquote(float aw, float ac, byte[] array) { System.out.println(aw + " " + ac + " " + " " + formatByteArray(array, " \"")); }
  130. public void op_d0(float wx, float wy) { System.out.println(wx + " " + wy + " d0"); }
  131. public void op_d1(float wx, float wy, float llx, float lly, float urx, float ury) { System.out.println(wx + " " + wy + " " + llx + " " + lly + " " + urx + " " + ury + " d1"); }
  132. public void op_CS(String name, ColorSpace cs) { System.out.println("/" + name + " " + (cs != null ? cs.toString() : "null") + " CS"); }
  133. public void op_cs(String name, ColorSpace cs) { System.out.println("/" + name + " " + (cs != null ? cs.toString() : "null") + " cs"); }
  134. public void op_SC_color(float[] color) { for (int i = 0; i < color.length; i++) System.out.print(color[i] + " "); System.out.println("SC%color"); }
  135. public void op_sc_color(float[] color) { for (int i = 0; i < color.length; i++) System.out.print(color[i] + " "); System.out.println("sc%color"); }
  136. public void op_SC_pattern(String name, int idx, float[] color) { System.out.print("/" + name + " " + idx + " "); for (int i = 0; i < color.length; i++) System.out.print(color[i] + " "); System.out.println("SC%pattern"); }
  137. public void op_sc_pattern(String name, int idx, float[] color) { System.out.print("/" + name + " " + idx + " "); for (int i = 0; i < color.length; i++) System.out.print(color[i] + " "); System.out.println("sc%pattern"); }
  138. public void op_SC_shade(String name) { System.out.println("/" + name + " SC%shade"); }
  139. public void op_sc_shade(String name) { System.out.println("/" + name + " sc%shade"); }
  140. public void op_G(float g) { System.out.println(g + " G"); }
  141. public void op_g(float g) { System.out.println(g + " g"); }
  142. public void op_RG(float r, float g, float b) { System.out.println(r + " " + g + " " + b + " RG"); }
  143. public void op_rg(float r, float g, float b) { System.out.println(r + " " + g + " " + b + " rg"); }
  144. public void op_K(float c, float m, float y, float k) { System.out.println(c + " " + m + " " + y + " " + k + " K"); }
  145. public void op_k(float c, float m, float y, float k) { System.out.println(c + " " + m + " " + y + " " + k + " k"); }
  146. public void op_BI(Image image) { System.out.println("% BI ... ID ... EI"); }
  147. public void op_sh(String name, Shade shade) { System.out.println("/" + name + " sh"); }
  148. public void op_Do_image(String name, Image image) { System.out.println("/" + name + " Do%image"); }
  149. public void op_Do_form(String name, PDFObject form, PDFObject pageResources) { System.out.println("/" + name + " Do%form"); }
  150. public void op_MP(String tag) { System.out.println(tag + " MP"); }
  151. public void op_DP(String tag, PDFObject raw) { System.out.println(tag + " " + raw.toString() + " DP"); }
  152. public void op_BMC(String tag) { System.out.println("/" + tag + " BMC"); }
  153. public void op_BDC(String tag, PDFObject raw) { System.out.println("/" + tag + " " + raw.toString(true) + " BDC"); }
  154. public void op_EMC() { System.out.println("EMC"); }
  155. public void op_BX() { System.out.println("BX"); }
  156. public void op_EX() { System.out.println("EX"); }
  157. public static void main(String[] args) {
  158. if (args.length < 1)
  159. {
  160. System.err.println("Usage: PDFTrace pdffile [pages...]");
  161. return;
  162. }
  163. String filename = args[0];
  164. Document doc = Document.openDocument(args[0]);
  165. if (!doc.isPDF())
  166. System.err.println(args[0] + ": only PDF documents can be traced");
  167. PDFDocument pdf = doc.asPDF();
  168. PDFProcessor proc = new PDFTrace();
  169. if (args.length == 1)
  170. {
  171. int pageno = 1;
  172. PDFPage page = (PDFPage) pdf.loadPage(pageno - 1);
  173. System.out.println("--------PAGE " + pageno + " BEGIN--------");
  174. page.process(proc);
  175. System.out.println("--------PAGE " + pageno + " END----------");
  176. }
  177. else
  178. {
  179. for (int i = 1; i < args.length; i++)
  180. {
  181. int pageno = Integer.parseInt(args[i]);
  182. PDFPage page = (PDFPage) pdf.loadPage(pageno - 1);
  183. System.out.println("--------PAGE " + pageno + " BEGIN--------");
  184. page.process(proc);
  185. System.out.println("--------PAGE " + pageno + " END----------");
  186. }
  187. }
  188. }
  189. }