All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jpedal.examples.viewer.paper.PaperSizes Maven / Gradle / Ivy

There is a newer version: 20151002
Show newest version
/*
 * ===========================================
 * Java Pdf Extraction Decoding Access Library
 * ===========================================
 *
 * Project Info:  http://www.idrsolutions.com
 * Help section for developers at http://www.idrsolutions.com/support/
 *
 * (C) Copyright 1997-2015 IDRsolutions and Contributors.
 *
 * This file is part of JPedal/JPDF2HTML5
 *
     This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


 *
 * ---------------
 * PaperSizes.java
 * ---------------
 */
package org.jpedal.examples.viewer.paper;

import javax.print.DocFlavor;
import javax.print.PrintService;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.MediaPrintableArea;
import javax.print.attribute.standard.MediaSize;
import javax.print.attribute.standard.MediaSizeName;
import javax.print.attribute.standard.OrientationRequested;

import java.awt.print.PageFormat;
import java.util.*;

public class PaperSizes {

    Map paperDefinitions=new HashMap();
    ArrayList paperList = new ArrayList();

    private static final double mmToSubInch = 72 / 25.4;

    final Map paperNames=new HashMap();

    /**default for paper selection*/
    private int defaultPageIndex;
    private String defaultSize;

    private PrintService printService;

    @SuppressWarnings("UnusedDeclaration")
    public PaperSizes(final PrintService printService) {
    	defaultSize = null;
    	populateNameMap();
    	addCustomPaperSizes();
    	setPrintService(printService);
    }
    
    public PaperSizes(final String defaultSize){
        this.defaultSize = defaultSize;
        populateNameMap();
        addCustomPaperSizes();
    }

    public String[] getAvailablePaperSizes(){
        final Object[] objs = paperList.toArray();
        final String[] names = new String[objs.length];
        for (int i=0; i pX-0.5 && values[2] < pX+0.5) {
            values[2] = (float) pX;
        }
        if (values[3] > pY-0.5 && values[3] < pY+0.5) {
            values[3] = (float) pY;
        }

        //Check if printer thinks page is other way round - flip pagesize if so
        if (values[2] > pX ^ values[3] > pY) {
            final double temp = pX;
            pX = pY;
            pY = temp;
        }


        //Create and store as Paper object
        final MarginPaper paper = new MarginPaper();
        paper.setSize(pX*mmToSubInch, pY*mmToSubInch);
        paper.setMinImageableArea(values[0]* mmToSubInch,values[1]*mmToSubInch, values[2]*mmToSubInch, values[3]*mmToSubInch);

        paperDefinitions.put(printDescription,paper);
        paperList.add(printDescription);
    }

    /**
     * Returns the index of the default paper size
     * @return
     */
    public int getDefaultPageIndex() {
    	return defaultPageIndex;
        
    }

    /**
     * Returns the default orientation requested by the printer
     * @return int flag for the orientation
     */
    public int getDefaultPageOrientation() {
    	
    	//Set the pageformat orientation based on printer preference
        final OrientationRequested or = (OrientationRequested)printService.getDefaultAttributeValue(OrientationRequested.class);
        int orientation = PageFormat.PORTRAIT;
        if(or!=null){
        	switch (or.getValue()) {
        	case 4:
        		orientation = PageFormat.LANDSCAPE;
        		break;
        	case 5:
        		orientation = PageFormat.REVERSE_LANDSCAPE;
        		break;
			}
        }
        
    	return orientation;
    }
    
    /**
     * Fills the name map from standardised to "pretty" names
     */
    private void populateNameMap() {
        paperNames.put("iso-a0", "A0");
        paperNames.put("iso-a1", "A1");
        paperNames.put("iso-a2", "A2");
        paperNames.put("iso-a3", "A3");
        paperNames.put("iso-a4", "A4");
        paperNames.put("iso-a5", "A5");
        paperNames.put("iso-a6", "A6");
        paperNames.put("iso-a7", "A7");
        paperNames.put("iso-a8", "A8");
        paperNames.put("iso-a9", "A9");
        paperNames.put("iso-a10", "A10");
        paperNames.put("iso-b0", "B0");
        paperNames.put("iso-b1", "B1");
        paperNames.put("iso-b2", "B2");
        paperNames.put("iso-b3", "B3");
        paperNames.put("iso-b4", "B4");
        paperNames.put("iso-b5", "B5");
        paperNames.put("iso-b6", "B6");
        paperNames.put("iso-b7", "B7");
        paperNames.put("iso-b8", "B8");
        paperNames.put("iso-b9", "B9");
        paperNames.put("iso-b10", "B10");
        paperNames.put("na-letter", "North American Letter");
        paperNames.put("na-legal", "North American Legal");
        paperNames.put("na-8x10", "North American 8x10 inch");
        paperNames.put("na-5x7", "North American 5x7 inch");
        paperNames.put("executive", "Executive");
        paperNames.put("folio", "Folio");
        paperNames.put("invoice", "Invoice");
        paperNames.put("tabloid", "Tabloid");
        paperNames.put("ledger", "Ledger");
        paperNames.put("quarto", "Quarto");
        paperNames.put("iso-c0", "C0");
        paperNames.put("iso-c1", "C1");
        paperNames.put("iso-c2", "C2");
        paperNames.put("iso-c3", "C3");
        paperNames.put("iso-c4", "C4");
        paperNames.put("iso-c5", "C5");
        paperNames.put("iso-c6", "C6");
        paperNames.put("iso-designated-long", "ISO Designated Long size");
        paperNames.put("na-10x13-envelope", "North American 10x13 inch");
        paperNames.put("na-9x12-envelope", "North American 9x12 inch");
        paperNames.put("na-number-10-envelope", "North American number 10 business envelope");
        paperNames.put("na-7x9-envelope", "North American 7x9 inch envelope");
        paperNames.put("na-9x11-envelope", "North American 9x11 inch envelope");
        paperNames.put("na-10x14-envelope", "North American 10x14 inch envelope");
        paperNames.put("na-number-9-envelope", "North American number 9 business envelope");
        paperNames.put("na-6x9-envelope", "North American 6x9 inch envelope");
        paperNames.put("na-10x15-envelope", "North American 10x15 inch envelope");
        paperNames.put("monarch-envelope", "Monarch envelope");
        paperNames.put("jis-b0", "Japanese B0");
        paperNames.put("jis-b1", "Japanese B1");
        paperNames.put("jis-b2", "Japanese B2");
        paperNames.put("jis-b3", "Japanese B3");
        paperNames.put("jis-b4", "Japanese B4");
        paperNames.put("jis-b5", "Japanese B5");
        paperNames.put("jis-b6", "Japanese B6");
        paperNames.put("jis-b7", "Japanese B7");
        paperNames.put("jis-b8", "Japanese B8");
        paperNames.put("jis-b9", "Japanese B9");
        paperNames.put("jis-b10", "Japanese B10");
        paperNames.put("a", "Engineering ANSI A");
        paperNames.put("b", "Engineering ANSI B");
        paperNames.put("c", "Engineering ANSI C");
        paperNames.put("d", "Engineering ANSI D");
        paperNames.put("e", "Engineering ANSI E");
        paperNames.put("arch-a", "Architectural A");
        paperNames.put("arch-b", "Architectural B");
        paperNames.put("arch-c", "Architectural C");
        paperNames.put("arch-d", "Architectural D");
        paperNames.put("arch-e", "Architectural E");
        paperNames.put("japanese-postcard", "Japanese Postcard");
        paperNames.put("oufuko-postcard", "Oufuko Postcard");
        paperNames.put("italian-envelope", "Italian Envelope");
        paperNames.put("personal-envelope", "Personal Envelope");
        paperNames.put("na-number-11-envelope", "North American Number 11 Envelope");
        paperNames.put("na-number-12-envelope", "North American Number 12 Envelope");
        paperNames.put("na-number-14-envelope", "North American Number 14 Envelope");
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy