Java 多页定向-Adobe Postscript工具

Java 多页定向-Adobe Postscript工具,java,adobe,postscript,Java,Adobe,Postscript,我正在深入研究AdobePostScript的工具,并试图找到一种生成具有多个方向的文档的方法 例如: 第1页的方向是纵向,第2页的方向是横向 下面我尝试创建一个新页面,然后将页面尺寸设置为与以前相反的尺寸,这样高度变为宽度,宽度变为高度-有效地创建了一个横向视图。但是,这不起作用,我想知道是否有办法做到这一点 OutputStream out = new java.io.FileOutputStream(outputFile); out = new java.io.Buffer

我正在深入研究AdobePostScript的工具,并试图找到一种生成具有多个方向的文档的方法

例如:

第1页的方向是纵向,第2页的方向是横向

下面我尝试创建一个新页面,然后将页面尺寸设置为与以前相反的尺寸,这样高度变为宽度,宽度变为高度-有效地创建了一个横向视图。但是,这不起作用,我想知道是否有办法做到这一点

    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);
    }
nextPage()
之后,使用
setupDocument()
代替
setViewportDimension()
,传入相同的
OutputStream
并交换宽度和高度:
g2d.setupDocument(out,pageHeightPT,pageWidthPT)

编辑

调用
setupDocument()
的问题在于它会重置页面计数并再次生成文件头。相反,您可以扩展
PSDocumentGraphics2D
并添加自己的
setDimension()
方法:

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;
    }
}
MyPSDocumentGraphics2D
中,
this.width
this.height
参考
AbstractPSDocumentGraphics2D
的受保护成员属性

您可以通过实例化
MyPSDocumentGraphics2D
然后替换
g2d.setViewportDimension(pageHeightPT、pageWidthPT)将其与示例联系起来带有
g2d.setDimension(页面高度pt、页面宽度pt)


当您尝试使用您提供的代码时会发生什么情况?
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);
}