FileChannel channel = raf.getChannel(); ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); PDFFile pdffile = new PDFFile(buf); int numPgs = pdffile.getNumPages(); for (int i = 0; i < numPgs; i++) { PDFPage page = pdffile.getPage(i);
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" ));
public static int countPages(final byte[] pdfBytes) { final ByteBuffer buf = ByteBuffer.wrap(pdfBytes); PDFFile pdffile; try { pdffile = new PDFFile(buf); return pdffile.getNumPages(); } catch (final IOException e) { log.error("Unable ro read pdf file.", e); } return 0; }
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(); } }
pageObj = findPage(topPagesObj, 0, pagenum, resources); page = createPage(pagenum, pageObj); byte[] stream = getContents(pageObj); parser = new PDFParser(page, stream, resources);
/** * Get the page commands for a given page in a separate thread. * * @param pagenum the number of the page to get commands for */ public PDFPage getPage(int pagenum) { return getPage(pagenum, false); }
/** * Changes the displayed page, desyncing if we're not on the * same page as a presenter. * @param pagenum the page to display */ public void gotoPage(int pagenum) { if (pagenum < 0) { pagenum = 0; } else if (pagenum >= curFile.getNumPages()) { pagenum = curFile.getNumPages() - 1; } forceGotoPage(pagenum); }
obj= readObject(fileBuf, ref.getObjectNumber(), ref.getGeneration(), decrypter); if (obj == null) { obj = PDFObject.nullObj; dereference(entry.getStream(), getDefaultDecrypter()); if (stream == null || stream.getType() != PDFObject.STREAM || !"ObjStm".equals(stream.getDictRef("Type").getStringValue())) { throw new PDFParseException(entry.getStream().getObjectNumber() + readObject(streamBuf, -1, -1, IdentityDecrypter.getInstance()); readObject(streamBuf, -1, -1, IdentityDecrypter.getInstance()). getIntValue(); obj= readObject(streamBuf, ref.getObjectNumber(), ref.getGeneration(), PDFDecoder.isEncrypted(stream) ? IdentityDecrypter.getInstance() : getDefaultDecrypter()); if (obj == null) { obj = PDFObject.nullObj;
File f = new File(Environment.getExternalStorageDirectory().getPath()+"manual.pdf"); long len = f.length(); RandomAccessFile raf = new RandomAccessFile(f, "r"); FileChannel channel = raf.getChannel(); ByteBuffer bb = ByteBuffer.NEW(channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size())); PDFFile mPdfFile = new PDFFile(bb);
private void initialize(String fileName) throws IOException { jf.setTitle("PDFRenderer Font Toy - " + fileName); try { inputFile = new File(fileName); FileInputStream istr = new FileInputStream(inputFile); byte [] buf = new byte[(int) inputFile.length()]; int read = 0; int offset = 0; while (read >= 0) { read = istr.read(buf, offset, buf.length-offset); } istr.close(); ByteBuffer byteBuf = ByteBuffer.allocate(buf.length); byteBuf.put(buf); pdf = new PDFFile(byteBuf); } catch (Exception ex) { ex.printStackTrace(); } PDFObject root = pdf.getRoot(); PDFObject pagesObj = (PDFObject) root.getDictRef("Pages"); fonts = findFonts(pagesObj, new HashMap<String,PDFObject>()); gfont = new Font("Sans-serif", Font.PLAIN, 10); Object[] fontObjs = getFonts().toArray(); comboBox.setModel(new DefaultComboBoxModel(fontObjs)); fontSelected((PDFFont) fontObjs[0]); this.validate(); }
newfile = new PDFFile(buf); } catch (IOException ioe) { openError(path + " doesn't appear to be a PDF file." + outline = curFile.getOutline(); } catch (IOException ioe) {
/** * Create a PDF Page object by finding the relevant inherited * properties * * @param pageObj the PDF object for the page to be created */ private PDFPage createPage(int pagenum, PDFObject pageObj) throws IOException { int rotation = 0; Rectangle2D mediabox = null; // second choice, if no crop Rectangle2D cropbox = null; // first choice PDFObject mediaboxObj = getInheritedValue(pageObj, "MediaBox"); if (mediaboxObj != null) { mediabox = parseRect(mediaboxObj); } PDFObject cropboxObj = getInheritedValue(pageObj, "CropBox"); if (cropboxObj != null) { cropbox = parseRect(cropboxObj); } PDFObject rotateObj = getInheritedValue(pageObj, "Rotate"); if (rotateObj != null) { rotation = rotateObj.getIntValue(); } Rectangle2D bbox = ((cropbox == null) ? mediabox : cropbox); return new PDFPage(pagenum, bbox, rotation, cache); }
PDFFile pdfFile = new PDFFile(new File(file)); PdfReader pdfReader = pdfFile.getPdfReader(); try { ... } finally { pdfReader.close(); }
obj= readObject(ref.getID(), ref.getGeneration(), decrypter); return PDFObject.nullObj; PDFXref compRef = new PDFXref(compId, 0); PDFObject compObj = dereference(compRef, decrypter); int first = compObj.getDictionary().get("First").getIntValue(); int n = compObj.getDictionary().get("N").getIntValue(); readObject(-1, -1, true, IdentityDecrypter.getInstance()); readObject(-1, -1, true, IdentityDecrypter.getInstance()); PDFObject objNumPO= readObject(-1, -1, true, IdentityDecrypter.getInstance()); PDFObject offsetPO= readObject(-1, -1, true, IdentityDecrypter.getInstance()); int objNum = objNumPO.getIntValue(); int offset = offsetPO.getIntValue(); obj= readObject(objNum, 0, IdentityDecrypter.getInstance()); buf = oldBuf;
/** * Create a PDF Page object by finding the relevant inherited * properties * * @param pageObj the PDF object for the page to be created */ private PDFPage createPage(int pagenum, PDFObject pageObj) throws IOException { int rotation = 0; Rectangle2D mediabox = null; // second choice, if no crop Rectangle2D cropbox = null; // first choice PDFObject mediaboxObj = getInheritedValue(pageObj, "MediaBox"); if (mediaboxObj != null) { mediabox = parseNormalisedRectangle(mediaboxObj); } PDFObject cropboxObj = getInheritedValue(pageObj, "CropBox"); if (cropboxObj != null) { cropbox = parseNormalisedRectangle(cropboxObj); } PDFObject rotateObj = getInheritedValue(pageObj, "Rotate"); if (rotateObj != null) { rotation = rotateObj.getIntValue(); } Rectangle2D bbox = ((cropbox == null) ? mediabox : cropbox); return new PDFPage(pagenum, bbox, rotation, cache); }
public PDFDecrypter getDecrypter() { // PDFObjects without owners are always created as part of // content instructions. Such objects will never have encryption // applied to them, as the stream they're contained by is the // unit of encryption. So, if someone asks for the decrypter for // one of these in-stream objects, no decryption should // ever be applied. This can be seen with inline images. return owner != null ? owner.getDefaultDecrypter() : IdentityDecrypter.getInstance(); }
/** * Find a property value in a page that may be inherited. If the value * is not defined in the page itself, follow the page's "parent" links * until the value is found or the top of the tree is reached. * * @param pageObj the object representing the page * @param propName the name of the property we are looking for */ private PDFObject getInheritedValue(PDFObject pageObj, String propName) throws IOException { // see if we have the property PDFObject propObj = pageObj.getDictRef(propName); if (propObj != null) { return propObj; } // recursively see if any of our parent have it PDFObject parentObj = pageObj.getDictRef("Parent"); if (parentObj != null) { return getInheritedValue(parentObj, propName); } // no luck return null; }
/** * Make sure that this object is dereferenced. Use the cache of * an indirect object to cache the dereferenced value, if possible. */ public PDFObject dereference() throws IOException { if (type == INDIRECT) { PDFObject obj = null; if (cache != null) { obj = (PDFObject) cache.get(); } if (obj == null || obj.value == null) { if (owner == null) { System.out.println("Bad seed (owner==null)! Object=" + this); } obj = owner.dereference((PDFXref)value, getDecrypter()); cache = new SoftReference<PDFObject>(obj); } return obj; } else { // not indirect, no need to dereference return this; } }
return findPage(kids[i], start, getPage, resources);