开发者

How can I convert a document to landscape mode using a Java library?

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?


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.


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()).


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);


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

        };
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜