IText 7如何在页眉中添加Div或段落而不重叠页面内容? [英] IText 7 How To Add Div or Paragraph in Header Without Overlapping Page Content?

查看:58
本文介绍了IText 7如何在页眉中添加Div或段落而不重叠页面内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正面临以下尚未解决的问题.我正在为医学实验室实现一个平台.他们希望针对每个事件将报告写入系统,然后从系统生成并打印报告.我正在使用itext 7来完成此任务.但是我面临以下问题.

I am facing the following problem for which i haven't found any solution yet. I am implementing a platform for a medical laboratory. They want for every incident to write the report to the system and then generate and print it from the system. I am using itext 7 to accomplish this. However i am facing the following problem.

他们有一个非常奇怪的模板.他们希望在开始的第一页上打印特定的表,而在每隔一页的开始处,他们要打印其他内容.因此,我需要知道页面何时更改,以便在页面的开头打印相应的表格.

They have a very strange template. On the first page in the beginning they want to print a specific table, while in the beginning of every other page they want to print something else. So i need to know when pages change in order to print in the beginning of the page the corresponding table.

在阅读了各种资源之后,我最终正常地创建了第一页,然后添加了一个页眉事件处理程序来检查页码,并始终执行除第一页以外的内容.

After reading various sources i ended up creating the first page normally and then adding a header event handler that checks the page number and gets executed always except page 1.

public class VariableHeaderEventHandler implements IEventHandler {


    @Override
    public void handleEvent(Event event) {
        System.out.println("THIS IS ME: HEADER EVENT HANDLER STARTED.....");
        PdfDocumentEvent documentEvent = (PdfDocumentEvent) event;
        PdfDocument pdfDoc = documentEvent.getDocument();
        PdfPage page = documentEvent.getPage();
        Rectangle pageSize = page.getPageSize();
        int pageNumber = pdfDoc.getPageNumber(page);

        if (pageNumber == 1) return; //Do nothing in the first page...

        System.out.println("Page size: " + pageSize.getHeight());

        Rectangle rectangle = new Rectangle(pageSize.getLeft() + 30, pageSize.getHeight()-234, pageSize.getWidth() - 60, 200);


        PdfCanvas pdfCanvas = new PdfCanvas(page.newContentStreamBefore(), page.getResources(), pdfDoc);
        pdfCanvas.rectangle(rectangle);
        pdfCanvas.setFontAndSize(FontsAndStyles.getRegularFont(), 10);
        Canvas canvas = new Canvas(pdfCanvas, pdfDoc, rectangle);

        Div header = new Div();

        Paragraph paragraph = new Paragraph();

        Text text = new Text("Διαγνωστικό Εργαστήριο Ιστοπαθολογίας και Μοριακής Παθολογοανατομικής").addStyle(FontsAndStyles.getBoldStyle());
        paragraph.add(text);
        paragraph.add(new Text("\n"));
        text = new Text("Μοριακή Διάγνωση σε Συνεργασία με").addStyle(FontsAndStyles.getBoldStyle());
        paragraph.add(text);
        paragraph.add(new Text("\n"));
        text = new Text("Γκούρβας Βίκτωρας, M.D., Ph.D.").addStyle(FontsAndStyles.getBoldStyle());
        paragraph.add(text);
        paragraph.add(new Text("\n"));
        text = new Text("Τσιμισκή 33, Τ.Κ. 54624, ΘΕΣΣΑΛΟΝΙΚΗ").addStyle(FontsAndStyles.getNormalStyle());
        paragraph.add(text);
        paragraph.add(new Text("\n"));
        text = new Text("Τήλ/Φάξ: 2311292924 Κιν.: 6932104909 e-mail: vgourvas@gmail.com").addStyle(FontsAndStyles.getNormalStyle());
        paragraph.add(text);
        header.add(paragraph);

//        =============Horizontal Line BOLD============
        SolidLine solidLine = new SolidLine((float) 1.5);
        header.add(new LineSeparator(solidLine));
//        ========Horizontal Line BOLD End==========


        text = new Text("ΠΑΘΟΛΟΓΟΑΝΑΤΟΜΙΚΗ ΕΞΕΤΑΣΗ").addStyle(FontsAndStyles.getBoldStyle());
        paragraph = new Paragraph().add(text);


        header.add(paragraph);
        header.setTextAlignment(TextAlignment.CENTER);
        canvas.add(header);
        canvas.close();
    }

但是,我现在面临的问题是标题与内容重叠,并且我不知道如何为每页设置不同的页边距.例如第2页及以后的表格,我想要不同的topMargin.

However the problem i am facing now is that header overlaps content and i can't figure out how to set different margins per page. For example form page 2 and beyond i would like different topMargin.

有人曾经遇到过这些问题并找到了可行的解决方案吗?我执行正确吗?有没有更好的方法来达到相同的结果?

Has anyone faced these problems before and have found a working solution? Am I implementing correct? Is there a better way of accomplishing the same result?

预先感谢

Toutoudakis Michail

Toutoudakis Michail

推荐答案

您应该创建自己的自定义文档渲染器,并减少用于放置除第一页之外的每个页面内容的区域.

You should create your own custom document renderer and decrease the area which would be used to place content for each page except for the first one.

请查看下面的代码段,尤其是updateCurrentArea方法.

Please look at the snippet below and updateCurrentArea method in particular.

    class CustomDocumentRenderer extends DocumentRenderer {
    public CustomDocumentRenderer(Document document) {
        super(document);
    }

    @Override
    public IRenderer getNextRenderer() {
        return new CustomDocumentRenderer(this.document);
    }

    @Override
    protected LayoutArea updateCurrentArea(LayoutResult overflowResult) {
        LayoutArea area = super.updateCurrentArea(overflowResult);
        if (currentPageNumber > 1) {
            area.setBBox(area.getBBox().decreaseHeight(200));
        }  
        return area;
    }
}

然后只需在文档上设置渲染器:

Then just set the renderer on your document:

    Document doc = new Document(pdfDoc);
    doc.setRenderer(new CustomDocumentRenderer(doc));

我为您的文档得到的pdf如下:

The resultant pdf which I get for your document looks as follows:

但是,还有另一种解决方案.将至少一个元素添加到文档后,即可更改默认文档的边距.所做的更改将应用​​于之后创建的所有页面(在您的情况下,这些页面是第2、3,...页)

There is another solution however. Once you've added at least one element to your document, you can change the default document's margins. The change will be applied on all pages created afterwards (and in your case these are pages 2, 3, ...)

        doc.add(new Paragraph("At least one element should be added. Otherwise the first page wouldn't be created and changing of the default margins would affect it."));
    doc.setMargins(200, 36, 36, 36);
    // now you can be sure that all the next pages would have new margins

这篇关于IText 7如何在页眉中添加Div或段落而不重叠页面内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆