使用iText的api來生成pdf
要使用中文文字內容要特別增加中文字型
static{
// 設定中文字型
BaseFont bf = null;
try {
bf = BaseFont.createFont(
AsianFontMapper.ChineseTraditionalFont_MHei,
AsianFontMapper.ChineseTraditionalEncoding_H,
BaseFont.NOT_EMBEDDED);
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static Font chineseFont10 = new Font(bf, 10, Font.NORMAL);// 設定中文字型(BaseFont、字型大小、字型型態)
private static Font linkChineseFont10 = new Font(bf, 10, Font.UNDERLINE,BaseColor.BLUE);// 設定中文字型(BaseFont、字型大小、字型型態、顏色)
其中要讓pdf文件的點擊動作可以直接開程式的話可以採用
PdfAction的api。他一共有4個建構式這邊採用的是4個參數的。
原本第一個參數是指定應用程式,後面是使用該應用程式的參數。
但開發只需要讓系統使用預設應用程式去開啟該檔案。
還好這部分直接丟給pdf的話是交由pdf自行呼叫系統buding的應用程式。
File file = new File("c:\\test.doc");
PdfAction pdfAction = new PdfAction(file.getAbsolutePath() ,null,null,null);
不過遇到小插曲就是編碼的問題,網路上也沒查到別人相關的處理狀況。
好家在之前也有在tomcat與apacheftpserver遇過類似的狀況,
猜想應該又是預設ios8859-1編碼問題所以依樣畫葫蘆就順利過關囉!
String systemEncoding = System.getProperty("sun.jnu.encoding");
File file = new File("c:\\中文.doc");
PdfAction pdfAction = new PdfAction(new String(file.getAbsolutePath().getBytes(systemEncoding), "ISO8859-1") ,null,null,null);
在此作記錄
沒有留言:
張貼留言