2012-03-27 67 views
0

我正在使用Java Servlet中的iText。我正在從我的JSP文件創建PDF文件。我已經看到這是不可能的,所以我創建瞭解析字符串(我的HTML頁面)的新servlet。這裏的部分代碼:使用iText和org.w3c.dom.Document旋轉PDF文件

StringBuffer buffer = getHTMLinBuffer(consulenti, anUser); 
DocumentBuilder builder = DocumentBuilderFactory 
    .newInstance().newDocumentBuilder(); 
Document doc = builder.parse(new ByteArrayInputStream(buffer.toString().getBytes("UTF-8"))); 
ITextRenderer renderer = new ITextRenderer(); 
renderer.setDocument(doc, null); 
String fileName = "listaConsulenti.pdf"; 
String absolutePath = getServletContext().getRealPath("/"); 
String tempPath = absolutePath + "temp/"; 
File file = new File(tempPath, fileName); 
file.createNewFile(); 
OutputStream os = new FileOutputStream(file); 
renderer.layout(); 
renderer.createPDF(os); 
os.close(); 

此代碼有效。 現在我必須創建另一個需要A4頁面旋轉90°的PDF。使用org.w3c.dom.Document我找不到如何操作。 有可能使用其他文檔類,com.itextpdf.text.Document其中rotate()方法旋轉它,但使用此文檔我找不到如何做解析我的字符串(HTML代碼)...

提示?

回答

0

...我只是發現了,那iTextRendere不iText的一部分,但飛來源。 在this link我已經看到了一個橫向頁面的創建是簡單地通過添加一些CSS來源(X)HTML做:

<style type="text/css"> 
@page{ size: 11.69in 8.27in;} 
... 
</style> 
0

如果你需要的是在它的一側的文檔,可以使用以下命令:

private Document document = new Document(PageSize.A4.rotate()); 
+0

不,我不能。使用org.w3c.dom.Document是不可能的。 – Emaborsa 2012-03-28 15:52:02