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

发布于 2024-12-05 09:38:22 字数 166 浏览 1 评论 0原文

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

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

I am writing a program in Java (I am using Ubuntu). I am using Jodconverter to convert the document to PDF. I have to convert the document to landscape mode but I have read that Jodconverter doesn't support orientation changes. I also tried with OpenOffice API but I am facing the same issue.

Is there any Java library that does conversion to landscape?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦年海沫深 2024-12-12 09:38:22

来自有关在 Open Office 文档中使用 Jodconverter 的类似问题:

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

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

From a similar question regarding using Jodconverter with an Open Office document:

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

Short answer: you can't. The page orientation is a property of the
document (menu Format > Page in Calc), not a PDF export option. So it
should be set already in the XLS document.

短暂陪伴 2024-12-12 09:38:22

导出为 PDF,然后使用 PDF 库(例如 PDFbox)将页面旋转 90 度。

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

Export to PDF and then use a PDF library like PDFbox to rotate the pages by 90 degrees.

Try PDPage.setRotation(int) on all pages (PDDocument.getDocumentCatalog().getAllPages()).

习惯成性 2024-12-12 09:38:22

我已经找到了解决方案。我已经使用 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);

I have found the solution. I have converted document to landscape pdf using open office API for java. Here is the code for the same.

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);
岛徒 2024-12-12 09:38:22

尝试覆盖 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);
            }

        };

Try overriding 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);
            }

        };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文