栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Openoffice转换excel为pdf格式问题处理

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Openoffice转换excel为pdf格式问题处理

问题背景:

基于Openoffice转换excel为pdf时,在linux环境下,默认配置转换后的pdf样式不受控。

解决方案:

JodConverter在文件正式转化前暴露了filter接口,供用户对文档格式等内容进行操作。在此基于自定义filter修改excel页面样式。

默认项目基于springboot,已经实现pdf转换的相关基础功能。

  1. 自定义JodConverter取代默认JodConverter,在其中引入自定义的新filter
import org.jodconverter.core.documentConverter;
import org.jodconverter.core.document.documentFormatRegistry;
import org.jodconverter.core.office.OfficeManager;
import org.jodconverter.local.LocalConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class JodConverterConfiguration {

    @Bean
    documentConverter localdocumentConverter(OfficeManager localOfficeManager, documentFormatRegistry documentFormatRegistry) {
        return LocalConverter.builder().filterChain(
        				//引入处理页面格式filter
                        new PageStyleRefactorFilter())
                .officeManager(localOfficeManager).formatRegistry(documentFormatRegistry).build();
    }
}
  1. 自定义filter内容:具体实现参考JodConverter原生fllter(PageMarginsFilter),页面格式相关参数参考OpenOfiice api。

OpenOffice原生api非常难用,JodConverter作了一定的封装,建议使用Jod中封装的方法

import com.sun.star.beans.XPropertySet;
import com.sun.star.container.XNameAccess;
import com.sun.star.container.XNameContainer;
import com.sun.star.lang.XComponent;
import com.sun.star.style.XStyle;
import com.sun.star.style.XStyleFamiliesSupplier;
import lombok.extern.slf4j.Slf4j;
import org.jodconverter.core.office.OfficeContext;
import org.jodconverter.local.filter.Filter;
import org.jodconverter.local.filter.FilterChain;
import org.jodconverter.local.office.utils.Calc;
import org.jodconverter.local.office.utils.Lo;


public class PageStyleRefactorFilter implements Filter {

    public PageStyleRefactorFilter() {
    }

    @Override
    public void doFilter(OfficeContext context, XComponent document, FilterChain chain) throws Exception {
    	//只处理excel相关格式,其它格式文件可自行定义
        if (Calc.isCalc(document)) {
        	//以下api调用路径请参考OpenOffice官方api
            // Get the StyleFamiliesSupplier interface of the document
            final XStyleFamiliesSupplier xSupplier = Lo.qi(XStyleFamiliesSupplier.class, document);
            // Use the StyleFamiliesSupplier interface to get the XNameAccess interface of the
            // actual style families
            final XNameAccess xFamilies = Lo.qi(XNameAccess.class, xSupplier.getStyleFamilies());

            // Access the 'PageStyles' Family
            final XNameContainer xFamily = Lo.qi(XNameContainer.class, xFamilies.getByName("PageStyles"));

            // Get the style of the current page from the PageStyles family
            for (String name : xFamily.getElementNames()) {
                final XStyle xStyle = Lo.qi(XStyle.class, xFamily.getByName(name));
                // Get the property set of the style
                final XPropertySet xStyleProps = Lo.qi(XPropertySet.class, xStyle);
                //所有列打印到同一页
                xStyleProps.setPropertyValue("ScaleToPagesX", (short) 1);
                //打印excel grid
                xStyleProps.setPropertyValue("PrintGrid", true);
                //更多参数请参考官方api:https://wiki.openoffice.org/wiki/documentation/DevGuide/Spreadsheets/Printing_Spreadsheet_documents
            }
        }
        // Invoke the next filter in the chain
        chain.doFilter(context, document);
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/685807.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号