如何解決OpenOffice轉excel爲pdf時出現折行?

問題:Java 操作  OpenOffice 將excel 轉換爲 pdf ,因多列出現折行;
原因:OpenOffice 默認輸出爲A4 大小,源excel 存在隊列,總寬度超出 A4 寬度,所以出現折行;
解決方法:自定義一個類,繼承OpenOfficeDocumentConverter 類 重寫方法,與 refreshDocument 方法,設置 OpenOffice 輸出 pdf 的寬度參數:
參考代碼如下:
public class ConverterDocument extends OpenOfficeDocumentConverter {


public ConverterDocument(OpenOfficeConnection connection) {
super(connection);
}




public final static Size A5, A4, A3;  
public final static Size B4, B5, B6;  
public final static Size KaoqinReport;  

static {  
    A5 = new Size(14800, 21000);  
    A4 = new Size(21000, 29700);  
    A3 = new Size(29700, 42000);  

    B4 = new Size(25000, 35300);  
    B5 = new Size(17600, 25000);  
    B6 = new Size(12500, 17600);  
      
    KaoqinReport = new Size(29700, 27940);  //最大限度  寬 1600000
}  


 
@Override  
protected void refreshDocument(XComponent document) {  
    super.refreshDocument(document);  

    // The default paper format and orientation is A4 and portrait. To  
    // change paper orientation  
    // re set page size  
    XPrintable xPrintable = (XPrintable) UnoRuntime.queryInterface(XPrintable.class, document);  
    PropertyValue[] printerDesc = new PropertyValue[2];  

    // Paper Orientation  
//  printerDesc[0] = new PropertyValue();  
//  printerDesc[0].Name = "PaperOrientation";  
//  printerDesc[0].Value = PaperOrientation.PORTRAIT;  

    // Paper Format  
    printerDesc[0] = new PropertyValue();  
    printerDesc[0].Name = "PaperFormat";  
    printerDesc[0].Value = PaperFormat.USER;  

    // Paper Size  
    printerDesc[1] = new PropertyValue();  
    printerDesc[1].Name = "PaperSize";  
    printerDesc[1].Value = KaoqinReport;  

    try {  
        xPrintable.setPrinter(printerDesc);  
    } catch (Exception e) {  
        e.printStackTrace();  
    }  




}


代碼參考大神!
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章