Package de.intarsys.cwt.environment

Examples of de.intarsys.cwt.environment.IGraphicsContext


  private PDPageTree pageTree = null;

  public BufferedImage getPageImage(File inputFile, String password, int page, int rotation)
      throws ThumbnailCreationException {
    BufferedImage retVal = null;
    IGraphicsContext graphics = null;
    PDDocument pdfDoc = null;
    if (inputFile != null && inputFile.exists() && inputFile.isFile()) {
      try {
        pdfDoc = openDoc(inputFile, password);
        PDPage pdPage = pdfDoc.getPageTree().getPageAt(page - 1);
        Rectangle2D rect = pdPage.getCropBox().toNormalizedRectangle();

        retVal = new BufferedImage((int) rect.getWidth(), (int) rect.getHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = (Graphics2D) retVal.getGraphics();
        graphics = new CwtAwtGraphicsContext(g2);
        // setup user space
        AffineTransform imgTransform = graphics.getTransform();
        imgTransform.scale(1, -1);
        imgTransform.translate(-rect.getMinX(), -rect.getMaxY());
        graphics.setTransform(imgTransform);
        graphics.setBackgroundColor(Color.WHITE);
        graphics.fill(rect);
        CSContent content = pdPage.getContentStream();
        if (content != null) {
          JPodRenderer renderer = new JPodRenderer(null, graphics);
          renderer.process(content, pdPage.getResources());
        }
        if (pdfDoc != null) {
          pdfDoc.close();
        }
        int totalRotation = (rotation + pdPage.getRotate()) % 360;
        if (totalRotation != 0) {
          Image rotated = ImageUtility.rotateImage(retVal, totalRotation);
          retVal = new BufferedImage(rotated.getWidth(null), rotated.getHeight(null),
              BufferedImage.TYPE_INT_RGB);
          Graphics g = retVal.getGraphics();
          g.drawImage(rotated, 0, 0, null);
        }
      } catch (Throwable t) {
        throw new ThumbnailCreationException(t);
      } finally {
        if (graphics != null) {
          graphics.dispose();
        }
      }
    } else {
      throw new ThumbnailCreationException(GettextResource.gettext(Configuration.getInstance()
          .getI18nResourceBundle(), "Input file doesn't exists or is a directory"));
View Full Code Here



  public Boolean call() {
    Boolean retVal = Boolean.FALSE;
    if(!IdManager.getInstance().isCancelledExecution(id)){
      IGraphicsContext graphics = null;
      try{
        Rectangle2D rect = pdPage.getCropBox().toNormalizedRectangle();
        double rectHeight = rect.getHeight();
        double recWidth = rect.getWidth();       
        double resizePercentage = getResizePercentage(rectHeight, recWidth);
       
        int height = Math.round(((int) rect.getHeight())*(float)resizePercentage);
        int width = Math.round(((int) rect.getWidth())*(float)resizePercentage);
        BufferedImage imageInstance = new BufferedImage((int)recWidth, (int)rectHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = (Graphics2D) imageInstance.getGraphics();
        graphics = new CwtAwtGraphicsContext(g2);
        // setup user space
        AffineTransform imgTransform = graphics.getTransform();
        imgTransform.scale(1, -1);
        imgTransform.translate(-rect.getMinX(), -rect.getMaxY());
        graphics.setTransform(imgTransform);       
        graphics.setBackgroundColor(Color.WHITE);
        graphics.fill(rect);
        CSContent content = pdPage.getContentStream();
        if (content != null) {
          JPodRenderer renderer = new JPodRenderer(null,graphics);
          renderer.process(content, pdPage.getResources());
        }
              //  pageItem.setThumbnail(scaledInstance);
        BufferedImage scaledInstance = ImageUtility.getScaledInstance(imageInstance, width, height);
                pageItem.setPaperFormat(recWidth, rectHeight, JPodThumbnailCreator.JPOD_RESOLUTION);
                if(pdPage.getRotate()!=0){
                  pageItem.setOriginalRotation(Rotation.getRotation(pdPage.getRotate()));
                }
                if(pageItem.isRotated()){
                  pageItem.setThumbnail(ImageUtility.rotateImage(scaledInstance, pageItem.getCompleteRotation()))
            }else{
            pageItem.setThumbnail(scaledInstance);

            }
                retVal = Boolean.TRUE;
            }catch (Throwable t) {
              pageItem.setThumbnail(ImageUtility.getErrorImage());
            log.error(GettextResource.gettext(Configuration.getInstance().getI18nResourceBundle(),"Unable to generate thumbnail"),t);
          }finally{
            if(graphics!=null){
              graphics.dispose();
            }
            pdPage = null;
          }
            ((VisualListModel)panel.getThumbnailList().getModel()).elementChanged(pageItem);
    }
View Full Code Here

TOP

Related Classes of de.intarsys.cwt.environment.IGraphicsContext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.