3

我正在用Java写一个程序(我正在使用Ubuntu)。我正在使用Jodconverter将文档转换为PDF。我必须将文档转换为横向模式,但我已经读过Jodconverter不支持方向更改。我也尝试过使用OpenOffice API,但我面临同样的问题。如何使用Java库将文档转换为横向模式?

有没有转换为横向的Java库?

回答

0

导出为PDF格式,然后使用像PDFbox这样的PDF库将页面旋转90度。

在所有页面上尝试PDPage.setRotation(int)PDDocument.getDocumentCatalog().getAllPages())。

+0

我试图PDFBOX LIB但它仅旋转页面....但我必须改变为横向页面的方向时,我们在横向模式下打印文档就像。 –

+0

要更改“方向”,您需要更改页面的边界(尝试“PDPage.setMediaBox()”;如果失败,请尝试接受矩形的其他方法)。 –

0

我找到了解决方案。我已经使用Java的开放式办公API将文档转换为格式pdf。这是相同的代码。

System.out.println("starting..."); 
       String oooExeFolder = "/usr/lib/openoffice/program"; 
       XComponentContext xContext = BootstrapSocketConnector.bootstrap(oooExeFolder); 

       XMultiComponentFactory xMCF = xContext.getServiceManager(); 

       Object oDesktop = xMCF.createInstanceWithContext("com.sun.star.frame.Desktop", xContext); 

       XComponentLoader xCLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, oDesktop); 
       System.out.println("loading "); 
       PropertyValue[] printerDesc = new PropertyValue[1]; 
       printerDesc[0] = new PropertyValue(); 
       printerDesc[0].Name = "PaperOrientation"; 
       printerDesc[0].Value = PaperOrientation.LANDSCAPE; 
       // Create a document 
       XComponent document = xCLoader.loadComponentFromURL(loadUrl, "_blank", 0, printerDesc); 
       // Following property will convert doc into requested orientation. 
       XPrintable xPrintable = (XPrintable) UnoRuntime.queryInterface(XPrintable.class, document); 
       xPrintable.setPrinter(printerDesc); 
       PropertyValue[] conversionProperties = new PropertyValue[3]; 
       conversionProperties[1] = new PropertyValue(); 
       conversionProperties[1].Name = "FilterName"; 
       conversionProperties[1].Value = "writer_pdf_Export";// 
       conversionProperties[0] = new PropertyValue(); 
       conversionProperties[0].Name = "Overwrite "; 
       conversionProperties[0].Value = new Boolean(true); 
       System.out.println("closing"); 
       XStorable xstorable = (XStorable) UnoRuntime.queryInterface(XStorable.class, document); 
       xstorable.storeToURL(storeUrl, conversionProperties); 
       System.out.println("closing"); 
       XCloseable xcloseable = (XCloseable) UnoRuntime.queryInterface(XCloseable.class, document); 
       xcloseable.close(false); 
0

尝试压倒一切的OfficeDocumentConverter

OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager) { 

      private Map<String, Object> createDefaultLoadProperties() { 
       Map<String, Object> loadProperties = new HashMap<String, Object>(); 
       loadProperties.put("Hidden", true); 
       loadProperties.put("ReadOnly", true); 
       loadProperties.put("UpdateDocMode", UpdateDocMode.QUIET_UPDATE); 
       return loadProperties; 
      } 

      @Override 
      public void convert(File inputFile, File outputFile, DocumentFormat outputFormat) throws OfficeException { 
       String inputExtension = FilenameUtils.getExtension(inputFile.getName()); 
       DocumentFormat inputFormat = getFormatRegistry().getFormatByExtension(inputExtension); 
       inputFormat.setLoadProperties(Collections.singletonMap("PaperOrientation", PaperOrientation.LANDSCAPE)); 
       StandardConversionTask conversionTask = new StandardConversionTask(inputFile, outputFile, outputFormat) { 

        @Override 
        protected void modifyDocument(XComponent document) throws OfficeException { 
         PropertyValue[] printerDesc = OfficeUtils.toUnoProperties(Collections.singletonMap("PaperOrientation", PaperOrientation.LANDSCAPE)); 
         XPrintable xPrintable = cast(XPrintable.class, document); 
         try { 
          xPrintable.setPrinter(printerDesc); 
         } catch (com.sun.star.lang.IllegalArgumentException e) { 
          logger.error(e.getMessage()); 
         } 
         super.modifyDocument(document); 
        } 
       }; 
       conversionTask.setDefaultLoadProperties(createDefaultLoadProperties()); 
       conversionTask.setInputFormat(inputFormat); 
       officeManager.execute(conversionTask); 
      } 

     };