Example.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright (C) 2022 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. /**
  23. * Render a page from a document at given zoom and rotation.
  24. *
  25. * To build this example in a source tree:
  26. * make -C platform/java examples
  27. *
  28. * To render page 2 from a document (at 100% without rotation)
  29. * and output the result as PNM to stdout, run:
  30. * java -classpath build/java/debug -Djava.library.path=build/java/debug \
  31. * example.Example document.pdf 2
  32. *
  33. * For 150% size with 90 degree rotation, pass two more arguments:
  34. * java -classpath build/java/debug -Djava.library.path=build/java/debug \
  35. * example.Example document.pdf 2 150 90
  36. */
  37. package example;
  38. /* Import all MuPDF java classes. */
  39. import com.artifex.mupdf.fitz.*;
  40. class Example
  41. {
  42. public static void main(String args[])
  43. {
  44. /* Parse arguments. */
  45. if (args.length < 2)
  46. {
  47. System.err.println("usage: Example input-file page-number [ zoom [ rotate] ]");
  48. System.err.println("\tinput-file: path of PDF, XPS, CBZ or EPUB document to open");
  49. System.err.println("\tPage numbering starts from one.");
  50. System.err.println("\tZoom level is in percent (100 percent is 72 dpi).");
  51. System.err.println("\tRotation is in degrees clockwise.");
  52. return;
  53. }
  54. String filename = args[0];
  55. int pageNumber = Integer.parseInt(args[1]) - 1;
  56. float zoom = 100;
  57. if (args.length >= 3)
  58. zoom = Float.parseFloat(args[2]);
  59. float rotate = 0;
  60. if (args.length >= 4)
  61. rotate = Float.parseFloat(args[3]);
  62. /* Open document using path. */
  63. Document doc;
  64. try {
  65. doc = Document.openDocument(filename);
  66. } catch (RuntimeException e) {
  67. System.err.println("Error opening document: " + e);
  68. return;
  69. }
  70. /* Count the number of pages. */
  71. int pageCount;
  72. try {
  73. pageCount = doc.countPages();
  74. } catch (RuntimeException e) {
  75. System.err.println("Error counting number of pages: " + e);
  76. return;
  77. }
  78. /* Check that the desired page is in range. */
  79. if (pageNumber < 0 || pageNumber >= pageCount)
  80. {
  81. System.err.println("Page number out of range: " + pageNumber + " (page count " + pageCount + ")");
  82. return;
  83. }
  84. /* Load the desired page from the document. */
  85. Page page;
  86. try {
  87. page = doc.loadPage(pageNumber);
  88. } catch (RuntimeException e) {
  89. System.err.println("Error loading page " + (pageNumber + 1) + ": " + e);
  90. return;
  91. }
  92. /* Create a transformation matrix based on
  93. * zoom factor and rotation supplied by user.
  94. */
  95. Matrix ctm = Matrix.Scale(zoom / 100);
  96. ctm.concat(Matrix.Rotate(rotate));
  97. /* Render page to an RGB pixmap without transparency. */
  98. Pixmap pixmap = null;
  99. try {
  100. pixmap = page.toPixmap(ctm, ColorSpace.DeviceRGB, false, true);
  101. } catch (RuntimeException e) {
  102. System.err.println("Error loading page " + (pageNumber + 1) + ": " + e);
  103. return;
  104. }
  105. /* Output RGB pixmap to PNM image. */
  106. System.out.println("P3");
  107. System.out.println(pixmap.getWidth() + " " + pixmap.getHeight());
  108. System.out.println("255");
  109. for (int y = 0; y < pixmap.getHeight(); y++)
  110. {
  111. for (int x = 0; x < pixmap.getWidth(); x++)
  112. {
  113. int r = ((int) pixmap.getSample(x, y, 0)) & 0xff;
  114. int g = ((int) pixmap.getSample(x, y, 1)) & 0xff;
  115. int b = ((int) pixmap.getSample(x, y, 2)) & 0xff;
  116. if (x == 0)
  117. System.out.format("%3d %3d %3d", r, g, b);
  118. else
  119. System.out.format(" %3d %3d %3d", r, g, b);
  120. }
  121. System.out.println("");
  122. }
  123. }
  124. }