/** * This method makes a normalized URI from a given filename. * @param filename a given filename * @return a valid URI */ public static URI toNormalizedURI(String filename) { return toNormalizedURI(new File(filename)); }
/** * Creates a WmfImage from a file. * * @param fileName pah to the file * @throws MalformedURLException */ public WmfImageData(String fileName) throws MalformedURLException { this(UrlUtil.toURL(fileName)); }
private static byte[] readImageType(URL source) { InputStream stream = null; try { stream = UrlUtil.openStream(source); byte[] bytes = new byte[8]; stream.read(bytes); return bytes; } catch (java.io.IOException e) { throw new IOException(IOException.IoException, e); } finally { if (stream != null) { try { stream.close(); } catch (java.io.IOException ignored) { } } } }
/** * Check if the type of image located at the passed is supported by the {@link ImageDataFactory} * @param src location of the image resource * @return true if the image type is supported, false otherwise */ public boolean isImageTypeSupportedByImageDataFactory(String src) { try { URL url = uriResolver.resolveAgainstBaseUri(src); url = UrlUtil.getFinalURL(url); return ImageDataFactory.isSupportedType(url); } catch(Exception e){ return false; } }
private static byte[] readImageType(URL source) { InputStream stream = null; try { stream = UrlUtil.openStream(source); byte[] bytes = new byte[8]; stream.read(bytes); return bytes; } catch (java.io.IOException e) { throw new IOException(IOException.IoException, e); } finally { if (stream != null) { try { stream.close(); } catch (java.io.IOException ignored) { } } } }
protected PdfXObject tryResolveUrlImageSource(String src) { try { URL url = uriResolver.resolveAgainstBaseUri(src); url = UrlUtil.getFinalURL(url); String imageResolvedSrc = url.toExternalForm(); PdfXObject imageXObject = imageCache.getImage(imageResolvedSrc); if (imageXObject == null) { imageXObject = createImageByUrl(url); imageCache.putImage(imageResolvedSrc, imageXObject); } return imageXObject; } catch (Exception ignored) { } return null; }
/** * This method makes a normalized URI from a given filename. * @param filename a given filename * @return a valid URI */ public static URI toNormalizedURI(String filename) { return toNormalizedURI(new File(filename)); }
/** * Create an ImageData instance representing the image from the specified file. * @param filename filename of the file containing the image * @param recoverImage whether to recover from a image error (for TIFF-images) * @return The created ImageData object. * @throws MalformedURLException */ public static ImageData create(String filename, boolean recoverImage) throws MalformedURLException { return create(UrlUtil.toURL(filename), recoverImage); }
/** * Load data by URL. url must be not null. * Note, this method doesn't check if data or url is null. * @throws java.io.IOException */ void loadData() throws java.io.IOException { InputStream input = null; try { input = UrlUtil.openStream(url); ByteArrayOutputStream stream = new ByteArrayOutputStream(); StreamUtil.transferBytes(UrlUtil.openStream(url), stream); data = stream.toByteArray(); } finally { if (input != null) { input.close(); } } } }
private void printOutCmpDirectories() { System.out.println("Out file folder: file:///" + UrlUtil.toNormalizedURI(new File(outPdf).getParentFile()).getPath()); System.out.println("Cmp file folder: file:///" + UrlUtil.toNormalizedURI(new File(cmpPdf).getParentFile()).getPath()); }
/** * Create an ImageData instance representing the image from the specified file. * @param filename filename of the file containing the image * @param recoverImage whether to recover from a image error (for TIFF-images) * @return The created ImageData object. * @throws MalformedURLException */ public static ImageData create(String filename, boolean recoverImage) throws MalformedURLException { return create(UrlUtil.toURL(filename), recoverImage); }
/** * Load data by URL. url must be not null. * Note, this method doesn't check if data or url is null. * @throws java.io.IOException */ void loadData() throws java.io.IOException { InputStream input = null; try { input = UrlUtil.openStream(url); ByteArrayOutputStream stream = new ByteArrayOutputStream(); StreamUtil.transferBytes(UrlUtil.openStream(url), stream); data = stream.toByteArray(); } finally { if (input != null) { input.close(); } } } }
/** * Compares two documents visually. For the comparison two external tools are used: Ghostscript and ImageMagick. * For more info about needed configuration for visual comparison process see {@link CompareTool} class description. * <br> * During comparison for every page of two documents an image file will be created in the folder specified by * outPath absolute path. Then those page images will be compared and if there are any differences for some pages, * another image file will be created with marked differences on it. * <br> * It is possible to ignore certain areas of the document pages during visual comparison. This is useful for example * in case if documents should be the same except certain page area with date on it. In this case, in the folder * specified by the outPath, new pdf documents will be created with the black rectangles at the specified ignored * areas, and visual comparison will be performed on these new documents. * * @param outPdf the absolute path to the output file, which is to be compared to cmp-file. * @param cmpPdf the absolute path to the cmp-file, which is to be compared to output file. * @param outPath the absolute path to the folder, which will be used to store image files for visual comparison. * @param differenceImagePrefix file name prefix for image files with marked differences if there is any. * @param ignoredAreas a map with one-based page numbers as keys and lists of ignored rectangles as values. * @return string containing list of the pages that are visually different, or null if there are no visual differences. * @throws InterruptedException * @throws IOException */ public String compareVisually(String outPdf, String cmpPdf, String outPath, String differenceImagePrefix, Map<Integer, List<Rectangle>> ignoredAreas) throws InterruptedException, IOException { init(outPdf, cmpPdf); System.out.println("Out pdf: file:///" + UrlUtil.toNormalizedURI(outPdf).getPath()); System.out.println("Cmp pdf: file:///" + UrlUtil.toNormalizedURI(cmpPdf).getPath() + "\n"); return compareVisually(outPath, differenceImagePrefix, ignoredAreas); }
/** * Embed a file to a PdfDocument. * * @param doc * @param filePath * @param description * @param fileDisplay * @param mimeType * @param fileParameter * @param afRelationshipValue * @throws IOException */ public static PdfFileSpec createEmbeddedFileSpec(PdfDocument doc, String filePath, String description, String fileDisplay, PdfName mimeType, PdfDictionary fileParameter, PdfName afRelationshipValue) throws IOException { PdfStream stream = new PdfStream(doc, UrlUtil.toURL(filePath).openStream()); PdfDictionary params = new PdfDictionary(); if (fileParameter != null) { params.mergeDifferent(fileParameter); } if (!params.containsKey(PdfName.ModDate)) { params.put(PdfName.ModDate, new PdfDate().getPdfObject()); } stream.put(PdfName.Params, params); return createEmbeddedFileSpec(doc, stream, description, fileDisplay, mimeType, afRelationshipValue); }
System.out.println("Out pdf: file:///" + UrlUtil.toNormalizedURI(outPdf).getPath()); System.out.println("Cmp pdf: file:///" + UrlUtil.toNormalizedURI(cmpPdf).getPath() + "\n"); setPassword(outPass, cmpPass); return compareByContent(outPath, differenceImagePrefix, ignoredAreas);
if (equalPages != null && equalPages.contains(i)) continue; System.out.println("Comparing page " + Integer.toString(i + 1) + ": file:///" + UrlUtil.toNormalizedURI(imageFiles[i]).getPath() + " ..."); FileInputStream is1 = new FileInputStream(imageFiles[i].getAbsolutePath()); FileInputStream is2 = new FileInputStream(cmpImageFiles[i].getAbsolutePath()); String errorMessage = differentPages.replace("<filename>", UrlUtil.toNormalizedURI(outPdf).getPath()).replace("<pagenumber>", listDiffPagesAsString(diffPages)); if (!compareExecIsOk) { errorMessage += "\nYou can optionally specify path to ImageMagick compare tool (e.g. -DcompareExec=\"C:/Program Files/ImageMagick-6.5.4-2/compare.exe\") to visualize differences.";