如何在java中的itext pdf库中添加边框到段落? [英] How to add border to paragraph in itext pdf library in java?

查看:1304
本文介绍了如何在java中的itext pdf库中添加边框到段落?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在java中使用itext pdf库创建了一个段落。我必须为段落添加边框,而不是整个文档。怎么做?

解决方案

请看一下中显示:





onStartPage() onEndPage()实现是明智的:

  class ParagraphBorder extends PdfPageEventHelper {
public boolean active = false;
public void setActive(boolean active){
this.active = active;
}

public float offset = 5;
public float startPosition;

@Override
public void onStartPage(PdfWriter writer,Document document){
startPosition = document.top();
}

@Override
public void onParagraph(PdfWriter writer,Document document,float paragraphPosition){
this.startPosition = paragraphPosition;
}

@Override
public void onEndPage(PdfWriter writer,Document document){
if(active){
PdfContentByte cb = writer.getDirectContentUnder( );
cb.rectangle(document.left(),document.bottom() - offset,
document.right() - document.left(),startPosition - document.bottom());
cb.stroke();
}
}

@Override
public void onParagraphEnd(PdfWriter writer,Document document,float paragraphPosition){
if(active){
PdfContentByte cb = writer.getDirectContentUnder();
cb.rectangle(document.left(),paragraphPosition - offset,
document.right() - document.left(),startPosition - paragraphPosition);
cb.stroke();
}
}
}


I have created a paragraph in using itext pdf library in java. I have to add border to paragraph, not to the whole document. How to do it ?

解决方案

Please take a look at the BorderForParagraph example. It shows how to add a border for a paragraph like this:

There is no method that allows you to create a border for a Paragraph, but you can create a PdfPageEvent implementation that allows you to draw a rectangle based on the start and end position of the Paragraph:

class ParagraphBorder extends PdfPageEventHelper {
    public boolean active = false;
    public void setActive(boolean active) {
        this.active = active;
    }

    public float offset = 5;
    public float startPosition;

    @Override
    public void onParagraph(PdfWriter writer, Document document, float paragraphPosition) {
        this.startPosition = paragraphPosition;
    }

    @Override
    public void onParagraphEnd(PdfWriter writer, Document document, float paragraphPosition) {
        if (active) {
            PdfContentByte cb = writer.getDirectContentUnder();
            cb.rectangle(document.left(), paragraphPosition - offset,
                document.right() - document.left(), startPosition - paragraphPosition);
            cb.stroke();
        }
    }
}

As you can see, I introduced a boolean parameter named active. By default, I've set this parameter to false. I also create an offset (change this value to fine-tune the result) and a startPosition parameter.

Each time iText starts rendering a Paragraph object, the startPosition value is updated. Each time iText ends rendering a Paragraph, a rectangle is drawn if active is true (otherwise nothing happens).

We use this event like this:

public void createPdf(String dest) throws IOException, DocumentException {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
    ParagraphBorder border = new ParagraphBorder();
    writer.setPageEvent(border);
    document.open();
    document.add(new Paragraph("Hello,"));
    document.add(new Paragraph("In this document, we'll add several paragraphs that will trigger page events. As long as the event isn't activated, nothing special happens, but let's make the event active and see what happens:"));
    border.setActive(true);
    document.add(new Paragraph("This paragraph now has a border. Isn't that fantastic? By changing the event, we can even provide a background color, change the line width of the border and many other things. Now let's deactivate the event."));
    border.setActive(false);
    document.add(new Paragraph("This paragraph no longer has a border."));
    document.close();
}

As you can see, we declare the event to the PdfWriter using the setPageEvent() method. We activate the event like this:

border.setActive(true);

and we deactivate it like this:

border.setActive(false);

This is only a proof of concept! You will need to implement the onStartPage() and onEndPage() method if you want this to work for paragraphs that span more than one page. That's shown in BorderForParagraph2:

The onStartPage() and onEndPage() implementation is a no-brainer:

class ParagraphBorder extends PdfPageEventHelper {
    public boolean active = false;
    public void setActive(boolean active) {
        this.active = active;
    }

    public float offset = 5;
    public float startPosition;

    @Override
    public void onStartPage(PdfWriter writer, Document document) {
        startPosition = document.top();
    }

    @Override
    public void onParagraph(PdfWriter writer, Document document, float paragraphPosition) {
        this.startPosition = paragraphPosition;
    }

    @Override
    public void onEndPage(PdfWriter writer, Document document) {
        if (active) {
            PdfContentByte cb = writer.getDirectContentUnder();
            cb.rectangle(document.left(), document.bottom() - offset,
                document.right() - document.left(), startPosition - document.bottom());
            cb.stroke();
        }
    }

    @Override
    public void onParagraphEnd(PdfWriter writer, Document document, float paragraphPosition) {
        if (active) {
            PdfContentByte cb = writer.getDirectContentUnder();
            cb.rectangle(document.left(), paragraphPosition - offset,
                document.right() - document.left(), startPosition - paragraphPosition);
            cb.stroke();
        }
    }
}

这篇关于如何在java中的itext pdf库中添加边框到段落?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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