File pdfFile = new File("/path/to/pdf.pdf"); RandomAccessFile raf = new RandomAccessFile(pdfFile, "r"); FileChannel channel = raf.getChannel(); ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); PDFFile pdf = new PDFFile(buf); PDFPage page = pdf.getPage(0); // create the image Rectangle rect = new Rectangle(0, 0, (int) page.getBBox().getWidth(), (int) page.getBBox().getHeight()); BufferedImage bufferedImage = new BufferedImage(rect.width, rect.height, BufferedImage.TYPE_INT_RGB); Image image = page.getImage(rect.width, rect.height, // width & height rect, // clip rect null, // null for the ImageObserver true, // fill background with white true // block until drawing is done ); Graphics2D bufImageGraphics = bufferedImage.createGraphics(); bufImageGraphics.drawImage(image, 0, 0, null); ImageIO.write(bufferedImage, format, new File( "/path/to/image.jpg" ));
File file = new File("test.pdf"); RandomAccessFile raf = new RandomAccessFile(file, "r"); FileChannel channel = raf.getChannel(); ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); PDFFile pdffile = new PDFFile(buf); // draw the first page to an image PDFPage page = pdffile.getPage(0); //get the width and height for the doc at the default zoom Rectangle rect = new Rectangle(0,0, (int)page.getBBox().getWidth(), (int)page.getBBox().getHeight()); //generate the image Image img = page.getImage( rect.width, rect.height, //width & height rect, // clip rect null, // null for the ImageObserver true, // fill background with white true // block until drawing is done );
File file = new File("test.pdf"); RandomAccessFile raf = new RandomAccessFile(file, "r"); FileChannel channel = raf.getChannel(); ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); PDFFile pdffile = new PDFFile(buf); // draw the first page to an image PDFPage page = pdffile.getPage(0); //get the width and height for the doc at the default zoom Rectangle rect = new Rectangle(0,0, (int)page.getBBox().getWidth(), (int)page.getBBox().getHeight()); //generate the image Image img = page.getImage( rect.width, rect.height, //width & height rect, // clip rect null, // null for the ImageObserver true, // fill background with white true // block until drawing is done );
byte[] bytes; try { File file = new File("/storage/extSdCard/Test.pdf"); FileInputStream is = new FileInputStream(file); // Get the size of the file long length = file.length(); bytes = new byte[(int) length]; int offset = 0; int numRead = 0; while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) { offset += numRead; } ByteBuffer buffer = ByteBuffer.NEW(bytes); String data = Base64.encodeToString(bytes, Base64.DEFAULT); PDFFile pdf_file = new PDFFile(buffer); PDFPage page = pdf_file.getPage(2, true); RectF rect = new RectF(0, 0, (int) page.getBBox().width(), (int) page.getBBox().height()); Bitmap image = page.getImage((int)rect.width(), (int)rect.height(), rect); FileOutputStream os = new FileOutputStream("/storage/extSdCard/pdf.jpg"); image.compress(Bitmap.CompressFormat.JPEG, 80, os); //((ImageView) findViewById(R.id.testView)).setImageBitmap(image); } catch (Exception e) { e.printStackTrace(); }
PrinterJob job = PrinterJob.createPrinterJob(); PDFFile pdfFile = ... ; if (job != null) { boolean success = true ; for (int pageNumber = 1; pageNumber <= pdfFile.getNumPages() ; pageNumber++) { PDFPage page = pdfFile.getPage(pageNumber, true); Rectangle2D bounds = page.getBBox(); int width = (int) bounds.getWidth(); int height = (int) bounds.getHeight(); java.awt.Image img = page.getImage(width, height, bounds, null, true, true); BufferedImage bImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); bImg.createGraphics().drawImage(img, 0, 0, null); javafx.scene.image.Image fxImg = SwingFXUtils.toFXImage(bImg, null); ImageView imageView = new ImageView(fxImg); success = success & job.printPage(imageView); } if (success) { job.endJob(); } }
clip = getBBox(); } else if (getRotation() == 90 || getRotation() == 270) { int tmp = width;
clip = getBBox(); } else if (getRotation() == 90 || getRotation() == 270) { int tmp = width;