|
| 1 | +package org.codedoggy; |
| 2 | + |
| 3 | +import com.aspose.words.Document; |
| 4 | +import com.aspose.words.HeaderFooter; |
| 5 | +import com.aspose.words.NodeCollection; |
| 6 | +import com.aspose.words.NodeType; |
| 7 | +import com.aspose.words.PageInfo; |
| 8 | +import com.aspose.words.Section; |
| 9 | +import com.aspose.words.Shape; |
| 10 | +import java.awt.image.BufferedImage; |
| 11 | +import java.io.File; |
| 12 | +import java.io.FileInputStream; |
| 13 | +import java.io.IOException; |
| 14 | +import java.io.InputStreamReader; |
| 15 | +import java.io.OutputStream; |
| 16 | +import java.io.OutputStreamWriter; |
| 17 | +import java.lang.reflect.Modifier; |
| 18 | +import java.nio.charset.StandardCharsets; |
| 19 | +import java.nio.file.Files; |
| 20 | +import java.util.LinkedHashMap; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.Optional; |
| 23 | +import java.util.Properties; |
| 24 | +import javax.imageio.ImageIO; |
| 25 | +import javax.print.PrintService; |
| 26 | +import javax.print.attribute.AttributeSet; |
| 27 | +import javax.print.attribute.HashAttributeSet; |
| 28 | +import javax.print.attribute.standard.Chromaticity; |
| 29 | +import javax.print.attribute.standard.Copies; |
| 30 | +import javax.print.attribute.standard.Media; |
| 31 | +import javax.print.attribute.standard.PrinterName; |
| 32 | + |
| 33 | +class AppController |
| 34 | +{ |
| 35 | + |
| 36 | + private static final String CONFIG_FILE_NAME = "config.properties"; |
| 37 | + |
| 38 | + private static final String TEMP_DIRECTORY = "temp"; |
| 39 | + |
| 40 | + private static final String configComment = "[forceModel]: [color] color mode, [no-color] black-white mode\n"; |
| 41 | + |
| 42 | + private String rootPath; |
| 43 | + |
| 44 | + private Properties properties; |
| 45 | + |
| 46 | + private MainInterface mainInterface; |
| 47 | + |
| 48 | + static { |
| 49 | + try { |
| 50 | + Class<?> aClass = Class.forName("com.aspose.words.zzXyu"); |
| 51 | + java.lang.reflect.Field zzYAC = aClass.getDeclaredField("zzZXG"); |
| 52 | + zzYAC.setAccessible(true); |
| 53 | + |
| 54 | + java.lang.reflect.Field modifiersField = zzYAC.getClass().getDeclaredField("modifiers"); |
| 55 | + modifiersField.setAccessible(true); |
| 56 | + modifiersField.setInt(zzYAC, zzYAC.getModifiers() & ~Modifier.FINAL); |
| 57 | + zzYAC.set(null, new byte[] { 76, 73, 67, 69, 78, 83, 69, 68 }); |
| 58 | + } catch (IllegalAccessException | ClassNotFoundException | NoSuchFieldException e) { |
| 59 | + e.printStackTrace(); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + AppController(MainInterface mainInterface) |
| 64 | + { |
| 65 | + this.mainInterface = mainInterface; |
| 66 | + // read root path |
| 67 | + this.rootPath = System.getProperty("user.dir"); |
| 68 | + |
| 69 | + this.properties = new Properties(); |
| 70 | + } |
| 71 | + |
| 72 | + void smartPrint(String filePath, PrintService printService, Media portraitPaperBox, Media landscapePaperBox, int copies) throws Exception |
| 73 | + { |
| 74 | + // load the file |
| 75 | + Document doc = new Document(filePath); |
| 76 | + int pageCount = doc.getPageCount(); |
| 77 | + Map<Document, AttributeSet> printJobs = new LinkedHashMap<>(); |
| 78 | + for (int page = 0; page < pageCount; page++) { |
| 79 | + // get printer properties |
| 80 | + HashAttributeSet attributeSet = new HashAttributeSet(); |
| 81 | + Document extractedPage = doc.extractPages(page, 1); |
| 82 | + PageInfo pageInfo = extractedPage.getPageInfo(0); |
| 83 | + boolean isLandscape = pageInfo.getLandscape(); |
| 84 | + if (!isLandscape) { |
| 85 | + // remove the header and footer |
| 86 | + for (Section section : extractedPage.getSections()) { |
| 87 | + for (HeaderFooter headerFooter : section.getHeadersFooters()) { |
| 88 | + NodeCollection<Shape> shapes = headerFooter.getChildNodes(NodeType.SHAPE, true); |
| 89 | + for (Shape shape : shapes) { |
| 90 | + if (shape.hasImage()) { |
| 91 | + shape.remove(); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + String tempFilePath = getTempFilePath() + File.separator + doc.hashCode() + "_page_" + (page + 1) + ".docx"; |
| 99 | + extractedPage.save(tempFilePath); |
| 100 | + Document extractedDoc = new Document(tempFilePath); |
| 101 | + boolean isColor = isLandscape || isColorImage(extractedDoc); |
| 102 | + // 检查配置 |
| 103 | + String forceModel = (String) properties.get("forceModel"); |
| 104 | + if (forceModel != null) { |
| 105 | + // 强制模式 |
| 106 | + if (forceModel.equals("color")) { |
| 107 | + isColor = true; |
| 108 | + } |
| 109 | + if (forceModel.equals("no-color")) { |
| 110 | + isColor = false; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + mainInterface.printMessage( |
| 115 | + "document page " + (page + 1) + (isLandscape ? " landscape " : " portrait ") |
| 116 | + + ", " + (isColor ? "color mode" : "black-white mode") |
| 117 | + + "\n"); |
| 118 | + |
| 119 | + attributeSet.add(new PrinterName(printService.getName(), null)); |
| 120 | + |
| 121 | + attributeSet.add(new Copies(1)); |
| 122 | + |
| 123 | + Optional.ofNullable(isLandscape ? landscapePaperBox : portraitPaperBox).ifPresent(attributeSet::add); |
| 124 | + |
| 125 | + attributeSet.add(isColor ? Chromaticity.COLOR : Chromaticity.MONOCHROME); |
| 126 | + |
| 127 | + // extractedDoc.print(attributeSet); |
| 128 | + printJobs.put(extractedDoc, attributeSet); |
| 129 | + } |
| 130 | + |
| 131 | + mainInterface.printMessage("Processing completed, send the file to printer...\n"); |
| 132 | + for (int i = 0; i < copies; i++) { |
| 133 | + for (Map.Entry<Document, AttributeSet> entry : printJobs.entrySet()) { |
| 134 | + entry.getKey().print(entry.getValue()); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + private String getTempFilePath() |
| 140 | + { |
| 141 | + return rootPath + File.separator + TEMP_DIRECTORY; |
| 142 | + } |
| 143 | + |
| 144 | + private boolean isColorImage(Document document) throws Exception |
| 145 | + { |
| 146 | + String tempPngFilePath = getTempFilePath() + File.separator + document.hashCode() + ".png"; |
| 147 | + document.save(tempPngFilePath); |
| 148 | + |
| 149 | + BufferedImage bufferedImage = ImageIO.read(new File(tempPngFilePath)); |
| 150 | + |
| 151 | + int[][][] result = null; |
| 152 | + int threshold = 20; |
| 153 | + |
| 154 | + int height = bufferedImage.getHeight(); |
| 155 | + int width = bufferedImage.getWidth(); |
| 156 | + result = new int[height][width][3]; |
| 157 | + for (int i = 0; i < height; i++) { |
| 158 | + for (int j = 0; j < width; j++) { |
| 159 | + int rgb = bufferedImage.getRGB(j, i);//visiting sequence for image: column, row |
| 160 | + result[i][j][0] = (rgb & 0xff0000) >> 16; |
| 161 | + result[i][j][1] = (rgb & 0xff00) >> 8; |
| 162 | + result[i][j][2] = (rgb & 0xff); |
| 163 | + |
| 164 | + if (Math.abs(result[i][j][0] - result[i][j][1]) <= threshold && Math.abs(result[i][j][1] - result[i][j][2]) <= threshold |
| 165 | + && Math.abs(result[i][j][2] - result[i][j][0]) <= threshold) { |
| 166 | + |
| 167 | + } else { |
| 168 | + return true; |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + return false; |
| 174 | + } |
| 175 | + |
| 176 | + Properties readConfig() throws IOException |
| 177 | + { |
| 178 | + String configPath = rootPath + File.separator + CONFIG_FILE_NAME; |
| 179 | + File configFile = new File(configPath); |
| 180 | + if (!configFile.exists()) { |
| 181 | + configFile.createNewFile(); |
| 182 | + } |
| 183 | + |
| 184 | + FileInputStream input = new FileInputStream(configFile); |
| 185 | + properties.load(new InputStreamReader(input, StandardCharsets.UTF_8)); |
| 186 | + return properties; |
| 187 | + } |
| 188 | + |
| 189 | + void saveConfig() throws IOException |
| 190 | + { |
| 191 | + String configPath = rootPath + File.separator + CONFIG_FILE_NAME; |
| 192 | + File configFile = new File(configPath); |
| 193 | + if (!configFile.exists()) { |
| 194 | + configFile.createNewFile(); |
| 195 | + } |
| 196 | + |
| 197 | + OutputStream os = Files.newOutputStream(configFile.toPath()); |
| 198 | + OutputStreamWriter osw = new OutputStreamWriter(os, StandardCharsets.UTF_8); |
| 199 | + properties.store(osw, configComment); |
| 200 | + } |
| 201 | + |
| 202 | + void cleanTemp() |
| 203 | + { |
| 204 | + String tempPath = getTempFilePath(); |
| 205 | + File tempDir = new File(tempPath); |
| 206 | + if (tempDir.exists()) { |
| 207 | + File[] files = tempDir.listFiles(); |
| 208 | + for (File file : files) { |
| 209 | + file.delete(); |
| 210 | + } |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + void savePrinterState(PrintService printService, Media portraitPaperBox, Media landscapePaperBox) |
| 215 | + { |
| 216 | + properties.put("printService", printService.getName()); |
| 217 | + properties.put("portraitPaperBox", String.valueOf(portraitPaperBox)); |
| 218 | + properties.put("landscapePaperBox", String.valueOf(landscapePaperBox)); |
| 219 | + } |
| 220 | +} |
0 commit comments