Exemplo n.º 1
0
  /**
   * Creates a page based on the template selected in the wizard **Note: this function may swap the
   * width and height if the template prefers different page orientation.
   *
   * @return a page
   */
  protected Page makePage(Rectangle pageSize, Document doc, Template template) {

    Map mapOnlyRasterLayers = null;
    Map mapNoRasterLayers = null;

    // **Note: the iText API doesn't render rasters at a high enough resolution if
    // they are written to the PDF via graphics2d.  To work around this problem, I
    // create two copies of the map: one with only the raster layers, and one with
    // everything else.
    // The "everything else" map gets drawn by a graphics2d.  The other layer must be
    // rasterized and inserted into the PDF via iText's API.

    // make one copy of the map with no raster layers
    mapNoRasterLayers = (Map) ApplicationGIS.copyMap(map);
    List<Layer> layersNoRasters = mapNoRasterLayers.getLayersInternal();
    List<Layer> toRemove = new ArrayList<Layer>();
    for (Layer layer : layersNoRasters) {
      for (IGeoResource resource : layer.getGeoResources()) {
        if (resource.canResolve(GridCoverageReader.class)) {
          toRemove.add(layer);
        }
      }
    }
    layersNoRasters.removeAll(toRemove);

    // adjust scale
    double currentViewportScaleDenom = map.getViewportModel().getScaleDenominator();
    if (currentViewportScaleDenom == -1)
      throw new IllegalStateException(
          "no scale denominator is available from the viewport model"); //$NON-NLS-1$

    if (page1.getScaleOption() == PrintWizardPage1.CUSTOM_MAP_SCALE) {
      float customScale = page1.getCustomScale();
      template.setMapScaleHint(customScale);
    } else if (page1.getScaleOption() == PrintWizardPage1.CURRENT_MAP_SCALE) {
      template.setMapScaleHint(currentViewportScaleDenom);
    } else if (page1.getScaleOption() == PrintWizardPage1.ZOOM_TO_SELECTION) {
      template.setZoomToSelectionHint(true);
      template.setMapScaleHint(currentViewportScaleDenom);
    }

    // 3. make the page itself
    Page page = ModelFactory.eINSTANCE.createPage();
    page.setSize(new Dimension((int) pageSize.getWidth(), (int) pageSize.getHeight()));

    // page name stuff not required, because this page will just get discarded
    MessageFormat formatter =
        new MessageFormat(Messages.CreatePageAction_newPageName, Locale.getDefault());
    if (page.getName() == null || page.getName().length() == 0) {
      page.setName(formatter.format(new Object[] {mapNoRasterLayers.getName()}));
    }

    template.init(page, mapNoRasterLayers);

    if (page1.getRasterEnabled()) {
      // make another copy with only raster layers
      mapOnlyRasterLayers = (Map) ApplicationGIS.copyMap(map);
      List<Layer> layersOnlyRasters = mapOnlyRasterLayers.getLayersInternal();
      List<Layer> toRemove2 = new ArrayList<Layer>();
      for (Layer layer : layersOnlyRasters) {
        for (IGeoResource resource : layer.getGeoResources()) {
          if (!resource.canResolve(GridCoverageReader.class)) {
            toRemove2.add(layer);
          }
        }
      }
      layersOnlyRasters.removeAll(toRemove2);

      // set bounds to match the other map
      SetViewportBBoxCommand cmdBbox =
          new SetViewportBBoxCommand(mapNoRasterLayers.getViewportModel().getBounds());
      mapOnlyRasterLayers.sendCommandSync(cmdBbox);

      if (layersNoRasters.size() > 0) {
        writeRasterLayersOnlyToDocument(
            mapOnlyRasterLayers,
            template.getMapBounds(),
            doc,
            page.getSize(), /*currentViewportScaleDenom*/
            mapNoRasterLayers.getViewportModel().getScaleDenominator());
      }
    }

    // copy the boxes from the template into the page
    Iterator<Box> iter = template.iterator();
    while (iter.hasNext()) {
      page.getBoxes().add(iter.next());
    }
    return page;

    // TODO Throw some sort of exception if the page can't be created

  }
Exemplo n.º 2
0
  @Override
  public boolean performFinish() {

    // create the document
    Rectangle suggestedPageSize = getITextPageSize(page1.getPageSize());
    Rectangle pageSize = rotatePageIfNecessary(suggestedPageSize); // rotate if we need landscape

    Document document = new Document(pageSize);

    try {

      // Basic setup of the Document, and get instance of the iText Graphics2D
      //   to pass along to uDig's standard "printing" code.
      String outputFile =
          page1.getDestinationDir()
              + System.getProperty("file.separator")
              + //$NON-NLS-1$
              page1.getOutputFile();
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outputFile));
      document.open();
      Graphics2D graphics = null;
      Template template = getTemplate();

      int i = 0;
      int numPages = 1;
      do {

        // sets the active page
        template.setActivePage(i);

        PdfContentByte cb = writer.getDirectContent();

        Page page = makePage(pageSize, document, template);

        graphics = cb.createGraphics(pageSize.getWidth(), pageSize.getHeight());

        // instantiate a PrinterEngine (pass in the Page instance)
        PrintingEngine engine = new PrintingEngine(page);

        // make page format
        PageFormat pageFormat = new PageFormat();
        pageFormat.setOrientation(PageFormat.PORTRAIT);
        java.awt.print.Paper awtPaper = new java.awt.print.Paper();
        awtPaper.setSize(pageSize.getWidth() * 3, pageSize.getHeight() * 3);
        awtPaper.setImageableArea(0, 0, pageSize.getWidth(), pageSize.getHeight());
        pageFormat.setPaper(awtPaper);

        // run PrinterEngine's print function
        engine.print(graphics, pageFormat, 0);

        graphics.dispose();
        document.newPage();
        if (i == 0) {
          numPages = template.getNumPages();
        }
        i++;

      } while (i < numPages);

      // cleanup
      document.close();
      writer.close();
    } catch (DocumentException e) {
      e.printStackTrace();
      return false;
    } catch (IOException e) {
      e.printStackTrace();
      return false;
    } catch (PrinterException e) {
      e.printStackTrace();
    }
    return true;
  }