package com.chinaweal.aiccs.aiccs.test;

import com.deepoove.poi.render.RenderContext;
import org.apache.poi.xwpf.usermodel.*;
import org.ddr.poi.html.HtmlRenderPolicy;
import org.ddr.poi.html.util.CSSLength;

import java.util.List;

/**
 * @author Lee
 * @since 2021-09-13 15:26
 */
public class customHtmlRenderPolicy extends HtmlRenderPolicy {
    public customHtmlRenderPolicy(String globalFont, CSSLength globalFontSize) {
        super(globalFont, globalFontSize);
    }

    @Override
    protected void afterRender(RenderContext<String> context) {
        clearPlaceholder(context, true);

        IBody container = context.getContainer();
        if (container.getPartType() == BodyType.TABLECELL) {
            // 单元格的最后一个元素应为p，否则可能无法正常打开文件
            List<IBodyElement> bodyElements = container.getBodyElements();
            if (bodyElements.isEmpty() || bodyElements.get(bodyElements.size() - 1).getElementType() != BodyElementType.PARAGRAPH) {
                ((XWPFTableCell) container).addParagraph();
            }
        }
    }
}
