Search code examples
javaadobepostscript

Multiple Page Orientions - Tools for Adobe Postscript


I'm delving into Tools for Adobe Postscript, and I'm trying to find a way to generate a document with multiple orientations.

Example:

page 1's orientation is portrait, and page 2's orientation is landscape.

Below I attempt to create a new page and then set the page dimensions opposite of what they were before, so that height becomes width and width becomes height - effectively creating a landscape view. This does not work, however, and I was wondering if there is a way to do it at all.

    OutputStream out = new java.io.FileOutputStream(outputFile);
    out = new java.io.BufferedOutputStream(out);


    try {
        //Instantiate the EPSDocumentGraphics2D instance
        PSDocumentGraphics2D g2d = new PSDocumentGraphics2D(false);
        g2d.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext());

        //Set up the document size
        g2d.setupDocument(out, pageWidthPT, pageHeightPT);

        g2d.setFont(new Font(font, Font.PLAIN, fontSize));
        g2d.drawString("           !", 10, 10);

        g2d.nextPage();
        g2d.setViewportDimension(pageHeightPT, pageWidthPT);

        g2d.drawString("Hello World!", 10, 20);
        System.out.println("Creating the document");
        g2d.finish();//Cleanup
    } finally {
        IOUtils.closeQuietly(out);
    }

Solution

  • After nextPage(), instead of setViewportDimension() use setupDocument(), passing in the same OutputStream and swapping the width and height: g2d.setupDocument(out, pageHeightPT, pageWidthPT);

    EDIT

    The problem with calling setupDocument() is that it resets the page count and generates file headers again. Instead, you can extend PSDocumentGraphics2D and add your own setDimension() method:

    public class MyPSDocumentGraphics2D extends PSDocumentGraphics2D {
        public MyPSDocumentGraphics2D(PSDocumentGraphics2D psDocumentGraphics2D) {
            super(psDocumentGraphics2D);
        }
    
        public MyPSDocumentGraphics2D(boolean b, OutputStream outputStream, int i, int i1) throws IOException {
            super(b, outputStream, i, i1);
        }
    
        public MyPSDocumentGraphics2D(boolean b) {
            super(b);
        }
    
        public void setDimension(int width, int height) {
            this.width = width;
            this.height = height;
        }
    }
    

    In MyPSDocumentGraphics2D, this.width and this.height refer to protected member properties of AbstractPSDocumentGraphics2D.

    You can tie this in to your example by instantiating MyPSDocumentGraphics2D and then replacing g2d.setViewportDimension(pageHeightPT, pageWidthPT); with g2d.setDimension(pageHeightPT, pageWidthPT);:

    OutputStream out = new java.io.FileOutputStream(outputFile);
    out = new java.io.BufferedOutputStream(out);
    
    
    try {
        //Instantiate my extension of the EPSDocumentGraphics2D instance
        MyPSDocumentGraphics2D g2d = new MyPSDocumentGraphics2D(false);
        g2d.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext());
    
        //Set up the document size
        g2d.setupDocument(out, pageWidthPT, pageHeightPT);
    
        g2d.setFont(new Font(font, Font.PLAIN, fontSize));
        g2d.drawString("           !", 10, 10);
    
        g2d.nextPage();
        // change the page orientation
        g2d.setDimension(pageHeightPT, pageWidthPT);
    
        g2d.drawString("Hello World!", 10, 20);
        System.out.println("Creating the document");
        g2d.finish();//Cleanup
    } finally {
        IOUtils.closeQuietly(out);
    }