我正在深入研究Adobe 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 :(得分: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;
}
}
在MyPSDocumentGraphics2D
中,this.width
和this.height
引用AbstractPSDocumentGraphics2D
的受保护成员资格。
您可以通过实例化MyPSDocumentGraphics2D
,然后将g2d.setViewportDimension(pageHeightPT, pageWidthPT);
替换为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);
}