2015-12-08 27 views
1

我正在研究Adobe Postscript的工具,并试图找到一种方法来生成具有多个方向的文档。多页面方向 - Adob​​e Postscript的工具

例子:

第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); 
    } 
+0

当您尝试使用您提供的代码,会发生什么? – liquidsystem

回答

1

nextPage()之后,而不是setViewportDimension()使用setupDocument(),通过在同一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; 
    } 
} 

MyPSDocumentGraphics2Dthis.widththis.height指的AbstractPSDocumentGraphics2D保护的成员属性。

您可以通过实例MyPSDocumentGraphics2D,然后用g2d.setDimension(pageHeightPT, pageWidthPT);更换g2d.setViewportDimension(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); 
}