Java tutorial
/* * Copyright (C) 2015-2016 NS Solutions Corporation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.htmlhifive.pitalium.sample.pdf; import java.awt.image.BufferedImage; import java.io.BufferedInputStream; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.rendering.ImageType; import org.apache.pdfbox.rendering.PDFRenderer; import org.junit.Assert; import org.junit.Test; import com.htmlhifive.pitalium.core.config.ExecMode; import com.htmlhifive.pitalium.core.config.PtlTestConfig; import com.htmlhifive.pitalium.image.model.DiffPoints; import com.htmlhifive.pitalium.image.model.ImageComparedResult; import com.htmlhifive.pitalium.image.util.ImageUtils; /** * Pitalim?PDF? <br/> * PDF????Apache PDFBox(https://pdfbox.apache.org/) ??? */ public class PDFReadTest { private static final String EXPORT_FILE_PATH = "./exports/pdf/"; /** * ?PDF?????????<br /> * ???????? * * @throws IOException */ @Test public void testCreateImageFromPDF() throws IOException { // PDF?????? if (PtlTestConfig.getInstance().getEnvironment().getExecMode() == ExecMode.SET_EXPECTED) { savePdfAsImages("sample_expected"); return; } int numberOfPages = savePdfAsImages("sample_actual"); if (numberOfPages < 0) { Assert.fail("PDF???????"); } // Pitalium?? // ?sample_expected.pdf???sample_actual.pdf? for (int i = 0; i < numberOfPages; i++) { BufferedImage leftImage = readImage(EXPORT_FILE_PATH + "sample_expected" + i + ".png"); BufferedImage rightImage = readImage(EXPORT_FILE_PATH + "sample_actual" + i + ".png"); if (leftImage == null || rightImage == null) { Assert.fail("??????"); } ImageComparedResult result = ImageUtils.compare(leftImage, null, rightImage, null, null); // ???? if (result.isFailed()) { DiffPoints diffPoints = (DiffPoints) result; final BufferedImage diffImage = ImageUtils.getDiffImage(leftImage, rightImage, diffPoints); saveExportImage(diffImage, "diffImage.png"); Assert.fail("PDF????"); } } } /** * PDF?????????<br/> * PDF?1?????1???<br/> * PDF??????Apache PDFBox? * * @param fileName ?PDF???.pdf??? * @return ?????PDF? */ private int savePdfAsImages(String fileName) { int numberOfPages = 0; try (BufferedInputStream fileToParse = new BufferedInputStream( getClass().getResourceAsStream(fileName + ".pdf")); PDDocument pdf = PDDocument.load(fileToParse)) { // 1=1????? final PDFRenderer pdfRenderer = new PDFRenderer(pdf); numberOfPages = pdf.getNumberOfPages(); for (int i = 0; i < numberOfPages; i++) { final BufferedImage image = pdfRenderer.renderImageWithDPI(i, 300, ImageType.RGB); if (!saveExportImage(image, fileName + i + ".png")) { return -1; } } } catch (IOException e) { return -1; } return numberOfPages; } /** * EXPORT_PATH???? * * @param image ??? * @param fileName ???? * @return ????????true?????????false */ private boolean saveExportImage(BufferedImage image, String fileName) { File exportDir = new File(EXPORT_FILE_PATH); if (!exportDir.exists()) { exportDir.mkdirs(); } try { return ImageIO.write(image, "png", new File(EXPORT_FILE_PATH + fileName)); } catch (IOException e) { return false; } } /** * ???? * * @param filePath ?? * @return ????BufferedImage */ private BufferedImage readImage(String filePath) { File image = new File(filePath); if (!image.exists()) { return null; } try { return ImageIO.read(image); } catch (IOException e) { return null; } } }