In itext7, Various columns document or switched column document

风格不统一 提交于 2021-01-29 05:01:08

问题


I have to make a pdf document like this.

In itext 7, I made columns document by ColumnDocumentRenderer class but i can't made like this.

Whole document was separated to two columns.

https://riptutorial.com/itext/example/20402/text2pdfcolumns-java--itext-7-

Can someonw help me?


回答1:


I solved this problem by self.

i refer these pages. https://kb.itextpdf.com/home/it7kb/ebooks/itext-7-building-blocks/chapter-2-adding-content-to-a-canvas-or-a-document

https://kb.itextpdf.com/home/it7kb/examples/itext-7-building-blocks-chapter-2-rootelement-examples#iText7BuildingBlocks-Chapter2:RootElementexamples-c02e11_jekyllhydev7

And this is my code

First renderer class.

package com.baejangho.util.pdf;

import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.layout.LayoutArea;
import com.itextpdf.layout.layout.LayoutResult;
import com.itextpdf.layout.layout.RootLayoutArea;
import com.itextpdf.layout.property.AreaBreakType;
import com.itextpdf.layout.renderer.DocumentRenderer;
import com.itextpdf.layout.renderer.IRenderer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashSet;

public class ColumnRenderer extends DocumentRenderer {
    private final Logger logger = LoggerFactory.getLogger(ColumnRenderer.class);
    private boolean isBody = false;
    private float lastHeaderEmptyHeight = 0;
    protected final Rectangle headerColumn;
    protected final Rectangle[] columns;
    protected int nextAreaNumber;
    protected int currentAreaNumber;
    protected int nextHeadAreaNumber;
    protected int currentHeadAreaNumber;
    protected int nextBodyAreaNumber;
    protected int currentBodyAreaNumber;
    protected HashSet<Integer> moveColumn = new HashSet();

    public ColumnRenderer(Document document, Rectangle headerColumn, Rectangle[] columns) {
        super(document, false);
        this.headerColumn = headerColumn;
        this.columns = columns;
    }

    public boolean isBody() {
        return isBody;
    }

    public void setBody(boolean body) {
        isBody = body;
    }

    @Override
    protected LayoutArea updateCurrentArea(LayoutResult overflowResult) {
        if (overflowResult != null
            && overflowResult.getAreaBreak() != null
            && overflowResult.getAreaBreak().getType()
            != AreaBreakType.NEXT_AREA) {
            nextAreaNumber = 0;
            nextHeadAreaNumber = 0;
            nextBodyAreaNumber = 0;
        }
        if (isBody) {
            if (
                (nextBodyAreaNumber > 0 && nextBodyAreaNumber % columns.length == 0)
            ) {
                super.updateCurrentArea(overflowResult);
            }
            if (nextBodyAreaNumber == 0 && null != currentArea && !currentArea.isEmptyArea()) {
                lastHeaderEmptyHeight = currentArea.getBBox().getHeight();
            }
            currentAreaNumber = nextAreaNumber + 1;
            nextAreaNumber++;
            currentBodyAreaNumber = nextBodyAreaNumber + 1;
            Rectangle newArea = columns[nextBodyAreaNumber++ % columns.length].clone();
            if (currentBodyAreaNumber < 3 && lastHeaderEmptyHeight > 30) {
                newArea.setHeight(lastHeaderEmptyHeight - 10);
            }
            return (currentArea = new RootLayoutArea(currentPageNumber, newArea));
        } else {
            super.updateCurrentArea(overflowResult);
            currentAreaNumber = nextAreaNumber + 1;
            nextAreaNumber++;
            currentHeadAreaNumber = nextHeadAreaNumber + 1;
            nextHeadAreaNumber++;
            return (currentArea = new RootLayoutArea(currentPageNumber, headerColumn.clone()));
        }
    }

    @Override
    protected PageSize addNewPage(PageSize customPageSize) {
        if (
            currentHeadAreaNumber != nextHeadAreaNumber
            || (
                currentBodyAreaNumber != nextBodyAreaNumber
                && currentBodyAreaNumber % columns.length != 0
            )
        ) {
            moveColumn.add(currentPageNumber - 1);
        }
        return super.addNewPage(customPageSize);
    }

    @Override
    protected void flushSingleRenderer(IRenderer resultRenderer) {
        int pageNum = resultRenderer.getOccupiedArea().getPageNumber();
        if (moveColumn.contains(pageNum)) {
            if (isBody) {
                resultRenderer.move(columns[0].getWidth() / 2, 0);
            } else {
                resultRenderer.move(headerColumn.getWidth() / 2, 0);
            }
        }
        super.flushSingleRenderer(resultRenderer);
    }
}

And class to configure columns size

package com.baejangho.util.pdf;

import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.geom.Rectangle;

public class LAYOUT_CONFIG {

    public static final PageSize PAGE_SIZE = PageSize.A4;
    public static final float PAGE_INNER_BOX_BOLDER_WITH = 1.0F;
    public static final float PAGE_OUTER_BOX_BOLDER_WITH = 3.5F;
    public static final float PAGE_BOX_BOLDER_OFFSET = 1.5F;
    public static final float MARGIN_TOP    = 72.0F;
    public static final float MARGIN_RIGHT  = 48.0F;
    public static final float MARGIN_BOTTOM = 52.0F;
    public static final float MARGIN_LEFT   = 48.0F;

    public static final float COLUMN_PADDING = 10.0F;

    public static final float HEADER_HORIZONTAL_OFFSET = MARGIN_LEFT + MARGIN_RIGHT;
    public static final float HEADER_VERTICAL_OFFSET = MARGIN_TOP + MARGIN_BOTTOM;
    public static final float HEADER_COLUMN_WIDTH = PAGE_SIZE.getWidth() - HEADER_HORIZONTAL_OFFSET - COLUMN_PADDING * 2;
    public static final float HEADER_COLUMN_HEIGHT = PAGE_SIZE.getHeight() - HEADER_VERTICAL_OFFSET;
    public static final float HEADER_COLUMN_X = MARGIN_LEFT + COLUMN_PADDING;

    public static final Rectangle HEADER_COLUMN = new Rectangle(HEADER_COLUMN_X, MARGIN_BOTTOM, HEADER_COLUMN_WIDTH, HEADER_COLUMN_HEIGHT);

    public static final float BODY_HORIZONTAL_OFFSET = MARGIN_LEFT + MARGIN_RIGHT;
    public static final float BODY_VERTICAL_OFFSET = MARGIN_TOP + MARGIN_BOTTOM;
    public static final float BODY_COLUMN_WIDTH = ((PAGE_SIZE.getWidth() - BODY_HORIZONTAL_OFFSET) / 2) - COLUMN_PADDING * 2;
    public static final float BODY_COLUMN_HEIGHT = PAGE_SIZE.getHeight() - BODY_VERTICAL_OFFSET;
    public static final float BODY_COLUMN_X = MARGIN_LEFT + COLUMN_PADDING;

    public static final Rectangle[] BODY_COLUMNS = {
        new Rectangle(BODY_COLUMN_X, MARGIN_BOTTOM, BODY_COLUMN_WIDTH, BODY_COLUMN_HEIGHT),
        new Rectangle(BODY_COLUMN_X + BODY_COLUMN_WIDTH + COLUMN_PADDING * 2, MARGIN_BOTTOM, BODY_COLUMN_WIDTH, BODY_COLUMN_HEIGHT)
    };

}

And class for make pdf.

public class MakePdf {

    public void main(String[] args) throws Exception {
        execute();
    }

    public void execute() throws Exception {
        String text_data = "";
        File legacy_file = new File("C:/Desktop/test.txt");
        PdfDocument pdf = new PdfDocument(new PdfWriter("test.pdf"));
        Document document = new Document(pdf).setTextAlignment(TextAlignment.JUSTIFIED);
        document.setMargins(LAYOUT_CONFIG.MARGIN_TOP, LAYOUT_CONFIG.MARGIN_RIGHT, LAYOUT_CONFIG.MARGIN_BOTTOM, LAYOUT_CONFIG.MARGIN_LEFT);
        document.setBackgroundColor(ColorConstants.BLACK);

        pdf.addNewPage(LAYOUT_CONFIG.PAGE_SIZE);

        ColumnRenderer renderer = new ColumnRenderer(document, LAYOUT_CONFIG.HEADER_COLUMN, LAYOUT_CONFIG.BODY_COLUMNS);
        document.setRenderer(renderer);
        AreaBreak bodyStart = new AreaBreak(AreaBreakType.NEXT_AREA);
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(legacy_file.getAbsolutePath()), "euc-kr"));
            String current_line = "";
            int line_idx = 0;
            while (null != (current_line = reader.readLine())) {
                if (line_idx == 0) {
                    document.add(new Paragraph(current_line).setFont(normal_font));
                } else if (line_idx == 1) {
                    document.add(new Paragraph(current_line).setFont(bold_font).setFontSize(26));
                } else if (line_idx == 2) {
                    document.add(new Paragraph(current_line).setFont(normal_font));
                } else {
                    document.add(new Paragraph(current_line).setFont(normal_font));
                }
                if (line_idx == 26) {
                    renderer.setBody(true);
                    document.add(bodyStart);
                }
                line_idx++;
            }
            reader.close();
        } catch (IOException e) {
            logger.debug(e.getMessage());
        }
        renderer.flush();
        document.close();
    }
}


来源:https://stackoverflow.com/questions/62614513/in-itext7-various-columns-document-or-switched-column-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!