如何使用Java库将文档转换为横向模式?

时间:2011-09-19 06:55:24

标签: java pdf-generation openoffice.org jodconverter openoffice-api

我正在用Java编写程序(我使用的是Ubuntu)。我正在使用Jodconverter将文档转换为PDF。我必须将文档转换为横向模式,但我已经读过Jodconverter不支持方向更改。我也尝试过使用OpenOffice API,但我遇到了同样的问题。

是否有任何Java库可以转换为横向?

4 个答案:

答案 0 :(得分:4)

关于将Jodconverter与Open Office文档一起使用的类似问题:

http://groups.google.com/group/jodconverter/browse_thread/thread/dc96df64c7d60ada/c1692fee92513b7a

简短的回答:你做不到。页面方向是该属性 文档(菜单格式> Calc中的页面),而不是PDF导出选项。所以 应该已经在XLS文档中设置了。

答案 1 :(得分:0)

导出为PDF,然后使用PDFbox之类的PDF库将页面旋转90度。

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

答案 2 :(得分: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);

答案 3 :(得分: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);
            }

        };