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