List of usage examples for org.apache.pdfbox.pdmodel.common PDRectangle PDRectangle
public PDRectangle(float width, float height)
From source file:correccioncolorpdfs.CorrectorColorUI.java
private void transformarPDF() { try {/*from w w w. ja v a 2s. c om*/ //@see http://stackoverflow.com/questions/18189314/convert-a-pdf-file-to-image String rutaPDFOriginal = rutaPDF + nombrePDF; // Pdf files are read from this folder //String destinationDir = "D:\\Desarrollo\\pruebas\\reportes_negros\\imagenes\\"; // converted images from pdf document are saved here File pdfOriginal = new File(rutaPDFOriginal); // File destinationFile = new File(destinationDir); // if (!destinationFile.exists()) { // destinationFile.mkdir(); // System.out.println("Folder Created -> " + destinationFile.getAbsolutePath()); // } if (pdfOriginal.exists()) { //System.out.println("Images copied to Folder: " + destinationFile.getName()); PDDocument document = PDDocument.load(rutaPDFOriginal); //Documento Fondo Blanco PDDocument documentoCool = new PDDocument(); List<PDPage> list = document.getDocumentCatalog().getAllPages(); System.out.println("Total files to be converted -> " + list.size()); String nombrePDFOriginal = pdfOriginal.getName().replace(".pdf", ""); int pageNumber = 1; for (PDPage page : list) { BufferedImage image = page.convertToImage(); //Inviertiendo colores //@see http://stackoverflow.com/questions/8662349/convert-negative-image-to-positive for (int x = 0; x < image.getWidth(); x++) { for (int y = 0; y < image.getHeight(); y++) { int rgba = image.getRGB(x, y); //Hexa a reemplazar e9e9e1 R=233|G=233|B=225 Color col = new Color(rgba, true); col = new Color(255 - col.getRed(), 255 - col.getGreen(), 255 - col.getBlue()); //Si color es igual al invertido - cambiarlo a blanco if (col.getRGB() == -1447455) { col = new Color(255, 255, 255); } //System.out.println("col.getR = " + col.getRGB()); image.setRGB(x, y, col.getRGB()); } } // File outputfile = new File(destinationDir + fileName + "_" + pageNumber + ".png"); // System.out.println("Image Created -> " + outputfile.getName()); // ImageIO.write(image, "png", outputfile); pageNumber++; //Crear pagina nueva para el PDF Convertido float width = image.getWidth(); float height = image.getHeight(); PDPage paginaSinFondo = new PDPage(new PDRectangle(width, height)); documentoCool.addPage(paginaSinFondo); PDXObjectImage img = new PDJpeg(documentoCool, image); PDPageContentStream contentStream = new PDPageContentStream(documentoCool, paginaSinFondo); contentStream.drawImage(img, 0, 0); contentStream.close(); } document.close(); rutaPDFImprimible = rutaPDF + nombrePDFOriginal + "_imprimible.pdf"; documentoCool.save(rutaPDFImprimible); documentoCool.close(); estadoConversion(true); } else { JOptionPane.showMessageDialog(this, "No se logr identificar la ruta del archivo, por favor verifique que el archivo si existe o no halla sido movido durante el proceso.", "Ruta de archivo no encontrada", JOptionPane.WARNING_MESSAGE); } } catch (IOException | COSVisitorException | HeadlessException e) { estadoConversion(false); JOptionPane.showMessageDialog(this, e.getMessage(), "Error durante el proceso de conversin", JOptionPane.ERROR_MESSAGE); } }
From source file:costumetrade.common.util.PdfUtils.java
License:Open Source License
public static PDDocument createImagePdf(File file) { PDPageContentStream contentStream = null; try {//from www.ja v a2 s .c om BufferedImage bimg = ImageIO.read(file); PDDocument document = new PDDocument(); PDPage page = new PDPage(new PDRectangle(bimg.getWidth(), bimg.getHeight())); document.addPage(page); PDImageXObject image = PDImageXObject.createFromFileByExtension(file, document); contentStream = new PDPageContentStream(document, page); contentStream.drawImage(image, 0, 0); return document; } catch (IOException e) { throw new RuntimeException("?PDF", e); } finally { IOUtils.closeQuietly(contentStream); } }
From source file:de.redsix.pdfcompare.CompareResult.java
License:Apache License
protected void addPageToDocument(final PDDocument document, final ImageWithDimension diffImage) throws IOException { PDPage page = new PDPage(new PDRectangle(diffImage.width, diffImage.height)); document.addPage(page);/*w w w . j ava 2 s .co m*/ final PDImageXObject diffXImage = LosslessFactory.createFromImage(document, diffImage.bufferedImage); /* final PDImageXObject actualXObject = LosslessFactory.createFromImage(document, actualImage.bufferedImage); final PDImageXObject expectedXObject = LosslessFactory.createFromImage(document, expectedImage.bufferedImage); */ try (PDPageContentStream contentStream = new PDPageContentStream(document, page)) { int x = (int) page.getCropBox().getWidth(); //System.out.println("X value "+x); int y = (int) page.getCropBox().getHeight(); //System.out.println("Y value "+y); contentStream.drawImage(diffXImage, 0, 0, x, y); /* contentStream.setLineWidth(0.5F); contentStream.moveTo(x, 2); contentStream.lineTo(x, y); contentStream.drawImage(expectedXObject, x+2, 0, x, y); contentStream.moveTo(x+x, 2); contentStream.lineTo(x+x, y+y); contentStream.drawImage(expectedXObject, x+x+2, 0, x, y); contentStream.stroke();*/ } }
From source file:de.redsix.pdfcompare.CompareResult.java
License:Apache License
protected void addPageToDocument(final PDDocument document, final ImageWithDimension actualImage, final ImageWithDimension expectedImage) throws IOException { PDPage page = new PDPage(new PDRectangle(actualImage.width, actualImage.height)); document.addPage(page);//ww w . ja v a2 s . co m //final PDImageXObject diffXImage = LosslessFactory.createFromImage(document, diffImage.bufferedImage); final PDImageXObject actualXObject = LosslessFactory.createFromImage(document, actualImage.bufferedImage); final PDImageXObject expectedXObject = LosslessFactory.createFromImage(document, expectedImage.bufferedImage); try (PDPageContentStream contentStream = new PDPageContentStream(document, page)) { int x = (int) page.getCropBox().getWidth() / 2; //System.out.println("X value "+x); int y = (int) page.getCropBox().getHeight(); //System.out.println("Y value "+y); contentStream.drawImage(actualXObject, 0, 0, x, y); contentStream.setLineWidth(0.5F); contentStream.moveTo(x, 2); contentStream.lineTo(x, y); contentStream.drawImage(expectedXObject, x + 2, 0, x, y); /*contentStream.moveTo(x+x, 2); contentStream.lineTo(x+x, y+y); contentStream.drawImage(expectedXObject, x+x+2, 0, x, y);*/ contentStream.stroke(); } }
From source file:de.redsix.pdfcompare.CompareResult.java
License:Apache License
protected void addPageToDocument(final PDDocument document, final ImageWithDimension diffImage, final ImageWithDimension actualImage, final ImageWithDimension expectedImage) throws IOException { PDPage page = new PDPage(new PDRectangle(diffImage.width, diffImage.height)); document.addPage(page);/*from ww w . j av a 2s .c om*/ final PDImageXObject diffXImage = LosslessFactory.createFromImage(document, diffImage.bufferedImage); final PDImageXObject actualXObject = LosslessFactory.createFromImage(document, actualImage.bufferedImage); final PDImageXObject expectedXObject = LosslessFactory.createFromImage(document, expectedImage.bufferedImage); try (PDPageContentStream contentStream = new PDPageContentStream(document, page)) { int x = (int) page.getCropBox().getWidth() / 3; System.out.println("X value " + x); int y = (int) page.getCropBox().getHeight(); System.out.println("Y value " + y); contentStream.drawImage(diffXImage, 0, 0, x, y); contentStream.setLineWidth(0.5F); contentStream.moveTo(x, 2); contentStream.lineTo(x, y); contentStream.drawImage(actualXObject, x + 2, 0, x, y); contentStream.moveTo(x + x, 2); contentStream.lineTo(x + x, y + y); contentStream.drawImage(expectedXObject, x + x + 2, 0, x, y); contentStream.stroke(); } }
From source file:de.rototor.pdfbox.graphics2d.PdfBoxGraphics2D.java
License:Apache License
/** * Create a PDfBox Graphics2D. This size is used for the BBox of the XForm. So * everything drawn outside the rectangle (0x0)-(pixelWidth,pixelHeight) will be * clipped./*from www.java2 s . c o m*/ * <p> * Note: pixelWidth and pixelHeight only define the size of the coordinate space * within this Graphics2D. They do not affect how big the XForm is finally * displayed in the PDF. * * @param document * The document the graphics should be used to create a XForm in. * @param pixelWidth * the width in pixel of the drawing area. * @param pixelHeight * the height in pixel of the drawing area. * @throws IOException * if something goes wrong with writing into the content stream of * the {@link PDDocument}. */ public PdfBoxGraphics2D(PDDocument document, int pixelWidth, int pixelHeight) throws IOException { this(document, new PDRectangle(pixelWidth, pixelHeight)); }
From source file:de.rototor.pdfbox.graphics2d.PdfBoxGraphics2D.java
License:Apache License
/** * Create a PDfBox Graphics2D. This size is used for the BBox of the XForm. So * everything drawn outside the rectangle (0x0)-(pixelWidth,pixelHeight) will be * clipped./*from ww w. j av a 2 s . c o m*/ * <p> * Note: pixelWidth and pixelHeight only define the size of the coordinate space * within this Graphics2D. They do not affect how big the XForm is finally * displayed in the PDF. * * @param document * The document the graphics should be used to create a XForm in. * @param pixelWidth * the width in pixel of the drawing area. * @param pixelHeight * the height in pixel of the drawing area. * @throws IOException * if something goes wrong with writing into the content stream of * the {@link PDDocument}. */ public PdfBoxGraphics2D(PDDocument document, float pixelWidth, float pixelHeight) throws IOException { this(document, new PDRectangle(pixelWidth, pixelHeight)); }
From source file:fr.amap.commons.javafx.chart.ChartViewer.java
/** * A handler for the export to PDF option in the context menu. *//* w w w . j a v a 2 s . c o m*/ private void handleExportToPDF() { FileChooser fileChooser = new FileChooser(); fileChooser.setSelectedExtensionFilter( new FileChooser.ExtensionFilter("Portable Document Format (PDF)", "pdf")); fileChooser.setTitle("Export to PDF"); File file = fileChooser.showSaveDialog(stage); if (file != null) { try { CanvasPositionsAndSize canvasPositionAndSize = getCanvasPositionAndSize(); PDDocument doc = new PDDocument(); PDPage page = new PDPage(new PDRectangle((float) canvasPositionAndSize.totalWidth, (float) canvasPositionAndSize.totalHeight)); doc.addPage(page); BufferedImage image = new BufferedImage((int) canvasPositionAndSize.totalWidth, (int) canvasPositionAndSize.totalHeight, BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = image.createGraphics(); int index = 0; for (ChartCanvas canvas : chartCanvasList) { Rectangle2D rectangle2D = canvasPositionAndSize.positionsAndSizes.get(index); ((Drawable) canvas.chart).draw(g2, new Rectangle((int) rectangle2D.getX(), (int) rectangle2D.getY(), (int) rectangle2D.getWidth(), (int) rectangle2D.getHeight())); index++; } PDPageContentStream contentStream = new PDPageContentStream(doc, page, true, false); PDXObjectImage pdImage = new PDPixelMap(doc, image); contentStream.drawImage(pdImage, 0, 0); PDPageContentStream cos = new PDPageContentStream(doc, page); cos.drawXObject(pdImage, 0, 0, pdImage.getWidth(), pdImage.getHeight()); cos.close(); doc.save(file); } catch (IOException | COSVisitorException ex) { Logger.getLogger(ChartViewer.class.getName()).log(Level.SEVERE, null, ex); } /*ExportUtils.writeAsPDF(this.chart, (int)canvas.getWidth(), (int)canvas.getHeight(), file);*/ /*ExportUtils.writeAsPDF(this.chart, (int)canvas.getWidth(), (int)canvas.getHeight(), file);*/ } }
From source file:info.informationsea.venn.graphics.VennDrawPDF.java
License:Open Source License
public static <T> void draw(VennFigure<T> vennFigure, PDDocument doc) throws IOException { // based on https://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/CreatePDFA.java?revision=1703059&view=markup PDFont font = PDType0Font.load(doc, VennDrawPDF.class.getResourceAsStream("../fx/mplus-1p-regular.ttf")); Rectangle2D drawRect = vennFigure.drawRect(str -> stringBoundingBox(font, str, FONT_SIZE)); PDRectangle pageSize = new PDRectangle((float) drawRect.getWidth() + MARGIN * 2, (float) drawRect.getHeight() + MARGIN * 2); PDPage page = new PDPage(pageSize); doc.addPage(page);/*from w w w . j av a2 s .co m*/ VennDrawPDF.draw(vennFigure, doc, page, font, pageSize); // PDF/A1b support /* // add XMP metadata XMPMetadata xmp = XMPMetadata.createXMPMetadata(); try { DublinCoreSchema dc = xmp.createAndAddDublinCoreSchema(); dc.setTitle("Venn Diagram"); dc.setSource("VennDraw "+ VersionResolver.getVersion()); dc.setDescription("Venn Diagram"); PDFAIdentificationSchema id = xmp.createAndAddPFAIdentificationSchema(); id.setPart(1); id.setConformance("B"); XmpSerializer serializer = new XmpSerializer(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); serializer.serialize(xmp, baos, true); PDMetadata metadata = new PDMetadata(doc); metadata.importXMPMetadata(baos.toByteArray()); doc.getDocumentCatalog().setMetadata(metadata); } catch(BadFieldValueException|TransformerException e) { throw new RuntimeException(e); } // sRGB output intent InputStream colorProfile = VennDrawPDF.class.getResourceAsStream( "sRGB Color Space Profile.icm"); PDOutputIntent intent = new PDOutputIntent(doc, colorProfile); intent.setInfo("sRGB IEC61966-2.1"); intent.setOutputCondition("sRGB IEC61966-2.1"); intent.setOutputConditionIdentifier("sRGB IEC61966-2.1"); intent.setRegistryName("http://www.color.org"); doc.getDocumentCatalog().addOutputIntent(intent); */ }
From source file:jp.qpg.PDFPrinter.java
License:Apache License
/** * @param page page size/*from w w w. ja va2 s . co m*/ * @param isLandscape true: landscape, false: portrait * @return this */ public PDFPrinter setPageSize(PDRectangle page, boolean isLandscape) { Objects.requireNonNull(page); pageSize = isLandscape ? new PDRectangle(page.getHeight(), page.getWidth()) : page; return this; }