protected void updateTexts() { mLine1 = "PdfViewer: " + mText; float fileTime = fileMillis * 0.001f; float pageRenderTime = pageRenderMillis * 0.001f; float pageParseTime = pageParseMillis * 0.001f; mLine2 = "render page=" + format(pageRenderTime, 2) + ", parse page=" + format(pageParseTime, 2) + ", parse file=" + format(fileTime, 2); int maxCmds = PDFPage.getParsedCommands(); int curCmd = PDFPage.getLastRenderedCommand() + 1; mLine3 = "PDF-Commands: " + curCmd + "/" + maxCmds; mLine1View.setText(mLine1); mLine2View.setText(mLine2); mLine3View.setText(mLine3); if (mPdfPage != null) { if (mBtPage != null) mBtPage.setText(mPdfPage.getPageNumber() + "/" + mPdfFile.getNumPages()); if (mBtPage2 != null) mBtPage2.setText(mPdfPage.getPageNumber() + "/" + mPdfFile.getNumPages()); } }
private void showPage(int page, float zoom) throws Exception { long startTime = System.currentTimeMillis(); long middleTime = startTime; try { // free memory from previous page mGraphView.setPageBitmap(null); mGraphView.updateImage(); mPdfPage = mPdfFile.getPage(page, true); int num = mPdfPage.getPageNumber(); int maxNum = mPdfFile.getNumPages(); float wi = mPdfPage.getWidth(); float hei = mPdfPage.getHeight(); String pageInfo = new File(pdffilename).getName() + " - " + num + "/" + maxNum + ": " + wi + "x" + hei; mGraphView.showText(pageInfo); Log.i(TAG, pageInfo); RectF clip = null; middleTime = System.currentTimeMillis(); Bitmap bi = mPdfPage.getImage((int) (wi * zoom), (int) (hei * zoom), clip, true, true); mGraphView.setPageBitmap(bi); mGraphView.updateImage(); } catch (Throwable e) { Log.e(TAG, e.getMessage(), e); mGraphView.showText("Exception: " + e.getMessage()); } long stopTime = System.currentTimeMillis(); mGraphView.pageParseMillis = middleTime - startTime; mGraphView.pageRenderMillis = stopTime - middleTime; }
public void pdfRead() { try { String INPUTFILE = "example_2.pdf"; File file = new File(INPUTFILE); 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 ); frame.getContentPane().add(new JLabel(new ImageIcon(img))); frame.pack(); frame.setSize(600, 700); } catch (Exception e) { System.out.println(e); } }
public static void imageConverter() { System.out.println("…ffene Datei..."); File file = new File("./PDFFile/01 Access2010 EinfŸhrung.pdf"); RandomAccessFile raf; try { 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); // konvertiere erste seite in ein bild int num = pdfFile.getNumPages(); for (int i = 0; i < num; i++) { PDFPage page = pdfFile.getPage(i); // ermittle Hoehe und Breite int width = (int) page.getBBox().getWidth(); int height = (int) page.getBBox().getHeight(); // AWT Rectangle rect = new Rectangle(0, 0, width, height); int rotation = page.getRotation(); Rectangle rectl = rect; if (rotation == 90 || rotation == 270) { rectl = new Rectangle(0, 0, rect.height, rect.width); } // generiere Bild BufferedImage img = (BufferedImage) page.getImage( rect.width, rect.height, // Breite Hoehe rectl, null, true, // Fuelle Hintergrund weiss aus true); ImageIO.write(img, "png", new File("./PDFImages/" + i + ".png")); System.out.println("Bild {" + i + " }erzeugt"); img.flush(); } System.out.println("Bilder erzeugt!"); } // Catcher catch (FileNotFoundException e1) { System.err.println(e1.getLocalizedMessage()); } catch (IOException e2) { System.err.println(e2.getLocalizedMessage()); } }
/** Add commands for this glyph to a page */ public Point2D addCommands(PDFPage cmds, AffineTransform transform, int mode) { if (shape != null) { GeneralPath outline = (GeneralPath) shape.createTransformedShape(transform); cmds.addCommand(new PDFShapeCmd(outline, mode)); } else if (page != null) { cmds.addCommands(page, transform); } return advance; }
public Rectangle2D convertPDF2ImageCoord(Rectangle2D r) { if (currentImage == null) return null; int imwid = currentImage.getWidth(null); int imhgt = currentImage.getHeight(null); AffineTransform t = currentPage.getInitialTransform(imwid, imhgt, prevClip); Rectangle2D tr = t.createTransformedShape(r).getBounds2D(); tr.setFrame(tr.getX(), tr.getY(), tr.getWidth(), tr.getHeight()); return tr; }
private void showPage(int page, float zoom) throws Exception { // long startTime = System.currentTimeMillis(); // long middleTime = startTime; try { // free memory from previous page mGraphView.setPageBitmap(null); mGraphView.updateImage(); // Only load the page if it's a different page (i.e. not just changing the zoom level) if (mPdfPage == null || mPdfPage.getPageNumber() != page) { mPdfPage = mPdfFile.getPage(page, true); } // int num = mPdfPage.getPageNumber(); // int maxNum = mPdfFile.getNumPages(); float width = mPdfPage.getWidth(); float height = mPdfPage.getHeight(); // String pageInfo= new File(pdffilename).getName() + " - " + num +"/"+maxNum+ ": " + width + // "x" + height; // mGraphView.showText(pageInfo); // Log.i(TAG, pageInfo); RectF clip = null; // middleTime = System.currentTimeMillis(); Bitmap bi = mPdfPage.getImage((int) (width * zoom), (int) (height * zoom), clip, true, true); mGraphView.setPageBitmap(bi); mGraphView.updateImage(); writeTempFileToDisc(bi); // bi.recycle();//ADDED FOR EFFICIENCY File dir = Environment.getExternalStorageDirectory(); File file = new File(dir, IMAGE_PATH + mPage + ".jpg"); mImageUri = Uri.fromFile(file); System.out.println("Displaying " + mImageUri.getPath()); mAllShareService.start(mImageUri.getPath()); if (progress != null) progress.dismiss(); } catch (Throwable e) { Log.e(TAG, e.getMessage(), e); mGraphView.showText("Exception: " + e.getMessage()); } // long stopTime = System.currentTimeMillis(); // mGraphView.pageParseMillis = middleTime-startTime; // mGraphView.pageRenderMillis = stopTime-middleTime; }
public Rectangle2D convertImage2PDFCoord(Rectangle2D r) { if (currentImage == null) return null; int imwid = currentImage.getWidth(null); int imhgt = currentImage.getHeight(null); AffineTransform t; try { t = currentPage.getInitialTransform(imwid, imhgt, prevClip).createInverse(); r.setFrame(r.getX(), r.getY(), 1, 1); Rectangle2D tr = t.createTransformedShape(r).getBounds2D(); tr.setFrame(tr.getX(), tr.getY(), tr.getWidth(), tr.getHeight()); return tr; } catch (NoninvertibleTransformException e) { return null; } }
public void write() { int navg = 8; // int nshift=3; bookend = 8; numPgs = pdffile.getNumPages(); files = new File[numPgs]; int[] pixelsi = null; long[] sum = null; long[][] hist = null; BufferedImage bimage = null; // BufferedImage simage = null; // float data[] = { 0.0625f, 0.125f, 0.0625f, 0.125f, 0.25f, 0.125f, // 0.0625f, 0.125f, 0.0625f }; // Kernel kernel = new Kernel(3, 3, data); // ConvolveOp convolve = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null); done(0); for (int i = 0; i < numPgs; i++) { if (i > MAXPAGE) break; PDFPage page = getPage(i); if (i == 0) { w = (int) page.getBBox().getWidth(); h = (int) page.getBBox().getHeight(); rect = new Rectangle(0, 0, w, h); // w /= 2; h /=2; bimage = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR); // simage = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR); pixelsi = new int[h * w]; sum = new long[h * w]; hist = new long[navg][h * w]; for (int j = 0; j < h * w; j++) { sum[j] = 0; } page1.countDown(); } // generate page image Image image = pdffile.getPage(i).getImage(w, h, rect, null, true, true); // force complete loading image = new ImageIcon(image).getImage(); // Copy image to buffered image Graphics g = bimage.createGraphics(); // Paint the image onto the buffered image g.drawImage(image, 0, 0, null); g.dispose(); // extract pixels into array bimage.getRGB(0, 0, w, h, pixelsi, 0, w); // Accumulate rolling averages int im = i % navg; int ii = i - navg / 2; // middle of averaging range for (int j = 0; j < h * w; j++) { int p = pixelsi[j], q = 0; // Expand packed 8x3 pixel to 16x3 long r = 0, r2 = 0; r |= (p & 0xff); r <<= 16; p >>= 8; r |= (p & 0xff); r <<= 16; p >>= 8; r |= (p & 0xff); r = ~r; sum[j] += r; // rolling sum if (i >= navg) { // we have enough to average sum[j] -= hist[im][j]; // roll off the old hist[im][j] = r; r2 = (3 * sum[j] / navg + r) / 4; r = ~r2; } else { hist[im][j] = r; } // Repack averaged pixel q |= (r & 0xff); q <<= 8; r >>= 16; q |= (r & 0xff); q <<= 8; r >>= 16; q |= (r & 0xff); // Average over number of images frames with a non-background pixel in this location. // Note that we sum in complement space, so background is zero. // if(i>=navg) // nfg[j] -= fghist[im][j]; // nfg[j] += (fghist[im][j] = (q==-1) ? 0 : 1); // // // If all pixels in history were background, this is easy... // if(nfg[j]==0) // q = -1; // // else { // // if(i>=navg) sum[k]-=hist[im][k]; // hist[im][k] = ~(p&0xff); // sum[k] += hist[im][k]; // if(i>=navg) // q += ~(((sum[k]/nfg[j])*3 + hist[iim][k])>>2); // else if(i>=navg/2) // q += ~(sum[k]/nfg[j] + hist[i-navg/2][k])>>1; // else // q += ~(sum[k]/nfg[j]); // k++; q<<=8; p>>=8; // // if(i>=navg) sum[k]-=hist[im][k]; // hist[im][k] = ~(p&0xff); // sum[k] += hist[im][k]; // if(i>=navg) // q += ~(((sum[k]/nfg[j])*3 + hist[iim][k])>>2); // else if(i>=navg/2) // q += ~((sum[k]/nfg[j] + hist[i-navg/2][k])>>1); // else // q += ~(sum[k]/nfg[j]); // k++; q<<=8; p>>=8; // // if(i>=navg) sum[k]-=hist[im][k]; // hist[im][k] = ~(p&0xff); // sum[k] += hist[im][k]; // if(i>=navg) // q += ~(((sum[k]/nfg[j])*3 + hist[iim][k])>>2); // else if(i>=navg/2) // q += ~((sum[k]/nfg[j] + hist[i-navg/2][k])>>1); // else // q += ~(sum[k]/nfg[j]); // k++; // } pixelsi[j] = q; } bimage = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR); bimage.setRGB(0, 0, w, h, pixelsi, 0, w); // save it as a file if (i >= navg) { try { ImageIO.write(bimage, "png", imageFile(0, ii + 1)); } catch (Exception e) { throw new RuntimeException(e.getMessage()); } } done(ii + 1); // System.err.println("Page " + i + " " + (System.currentTimeMillis()-t0)/1000.); if (terminated) { System.err.println("Prematurely terminated"); for (File f : files) f.delete(); tmpdir.delete(); break; } } }
public static void main(String[] args) { try { String sourceDir = "d:\\Desarrollo\\git\\AyudaDigitalizacion\\AyudaDigitalizacion\\ocr.pdf"; // PDF file must // be placed in // DataGet // folder String destinationDir = "d:\\Desarrollo\\git\\AyudaDigitalizacion\\AyudaDigitalizacion\\"; // Converted PDF page // saved in this folder File sourceFile = new File(sourceDir); File destinationFile = new File(destinationDir); String fileName = sourceFile.getName().replace(".pdf", ""); if (sourceFile.exists()) { if (!destinationFile.exists()) { destinationFile.mkdir(); System.out.println("Folder created in: " + destinationFile.getCanonicalPath()); } RandomAccessFile raf = new RandomAccessFile(sourceFile, "r"); FileChannel channel = raf.getChannel(); ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); PDFFile pdf = new PDFFile(buf); int pageNumber = 1; // which PDF page to be convert PDFPage page = pdf.getPage(pageNumber); // image dimensions int width = 1200; int height = 1400; // imagenes rectificadas width = (int) page.getBBox().getWidth(); height = (int) page.getBBox().getHeight(); // create the image Rectangle rect = new Rectangle(0, 0, (int) page.getBBox().getWidth(), (int) page.getBBox().getHeight()); BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // width & height, // clip rect, // null for the ImageObserver, // fill background with // white, // block until drawing is done Image image = page.getImage(width, height, rect, null, true, true); Graphics2D bufImageGraphics = bufferedImage.createGraphics(); bufImageGraphics.drawImage(image, 0, 0, null); File imageFile = new File( destinationDir + fileName + "_" + pageNumber + ".jpg"); // change file format here. Ex: .png, .jpg, .jpeg, .gif, .bmp ImageIO.write(bufferedImage, "png", imageFile); System.out.println( imageFile.getName() + " File created in: " + destinationFile.getCanonicalPath()); } else { System.err.println(sourceFile.getName() + " File not exists"); } } catch (Exception e) { e.printStackTrace(); } }
/** * Stop the generation of any previous page, and draw the new one. * * @param page the PDFPage to draw. */ public void showPage(PDFPage page) { // stop drawing the previous page if (currentPage != null && prevSize != null) { currentPage.stop(prevSize.width, prevSize.height, prevClip); } // set up the new page currentPage = page; if (page == null) { // no page currentImage = null; clip = null; currentXform = null; canvas.repaint(); } else { // Reset highlight highlight = null; boolean resize = false; int newW = Math.round(zoomFactor * page.getWidth()); int newH = Math.round(zoomFactor * page.getHeight()); // setSize(Math.round(zoomFactor*page.getWidth()), Math.round(zoomFactor*page.getHeight())); Point sz = getSize(); if (sz.x != newW || sz.y != newH) { sz.x = newW; sz.y = newH; resize = true; } if (sz.x + sz.y == 0) { // no image to draw. return; } // calculate the clipping rectangle in page space from the // desired clip in screen space. Rectangle2D useClip = clip; if (clip != null && currentXform != null) { useClip = currentXform.createTransformedShape(clip).getBounds2D(); } Dimension pageSize = page.getUnstretchedSize(sz.x, sz.y, useClip); ImageInfo info = new ImageInfo(pageSize.width, pageSize.height, useClip, null); currentImage = new RefImage(pageSize.width, pageSize.height, BufferedImage.TYPE_INT_ARGB); Rectangle rect = new Rectangle(0, 0, pageSize.width, pageSize.height); PDFRenderer r = new PDFRenderer( page, ((BufferedImage) currentImage).createGraphics(), rect, useClip, Color.WHITE); page.renderers.put(info, new WeakReference<PDFRenderer>(r)); // get the new image /*currentImage = page.getImage(pageSize.width, pageSize.height, useClip, this, true, false);*/ // calculate the transform from screen to page space currentXform = page.getInitialTransform(pageSize.width, pageSize.height, useClip); try { currentXform = currentXform.createInverse(); } catch (NoninvertibleTransformException nte) { System.out.println("Error inverting page transform!"); nte.printStackTrace(); } prevClip = useClip; prevSize = pageSize; r.go(true); if (r.getStatus() != Watchable.COMPLETED) return; if (resize) // Resize triggers repaint setSize( Math.round(zoomFactor * page.getWidth()), Math.round(zoomFactor * page.getHeight())); else { EventQueue.invokeLater( new Runnable() { @Override public void run() { // canvas.repaint(); } }); } } }
public void highlight(double x, double y, double x2, double y2) { Rectangle2D r = new Double(x, currentPage.getHeight() - y2, x2 - x, y2 - y); highlight = convertPDF2ImageCoord(r); }