2015-12-08 25 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); 
}