4

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

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

4

4 回答 4

4

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

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

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

于 2011-09-19T07:06:04.900 回答
0

我找到了解决方案。我已经使用 Open Office API for java 将文档转换为横向 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);
于 2011-09-26T12:00:45.537 回答
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);
            }

        };
于 2013-04-04T13:18:08.707 回答
0

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

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

于 2011-09-19T07:16:30.250 回答