繁体   English   中英

DynamicJasper API:PDF中的边框未对齐

[英]DynamicJasper API: not aligned border in PDF

当我的报告行对于单个页面来说太长时,它将在下一页继续显示,但是,如该图所示,边框被切断了。

有任何想法吗?

我正在使用JRPdfExporter将报告导出为PDF。

在此处输入图片说明

更新:

        DynamicReport dynamicReport;
        ...
        fastReportBuilder.setPageSizeAndOrientation(Page.Page_A4_Landscape());
        fastReportBuilder.setUseFullPageWidth(true);

        //margin 50
        fastReportBuilder.setMargins(MARGIN, MARGIN, MARGIN, MARGIN);

        fastReportBuilder.setDefaultStyles(getTitleStyle(), null, getColumnHeaderStyle(), getColumnDetailsStyle());

   private Style getColumnHeaderStyle() {
        Style hStyle = new Style();
        hStyle.setBorder(Border.THIN());
        hStyle.setTransparent(false);
        hStyle.setBackgroundColor(new Color(0, 142, 175));
        hStyle.setTextColor(Color.WHITE);
        hStyle.setHorizontalAlign(HorizontalAlign.CENTER);
        hStyle.setVerticalAlign(VerticalAlign.MIDDLE);
        hStyle.setFont(new Font(10, MY_FONT, false));
        return hStyle;
    }

    private Style getColumnDetailsStyle() {
        Style cStyle = new Style();
        cStyle.setBorder(Border.THIN());
        cStyle.setFont(new Font(10, MY_FONT, false));
        cStyle.setVerticalAlign(VerticalAlign.TOP);
        return cStyle;
    }

使用iReport 5.1.0设计的JasperReport PDF中的边框伪像给了我一个主意。

我最终扩展了JRPdfExporter。 出于某种原因,当数据被剪切并在下一页继续时,单元格边框的高度和数据被剪切掉的底部在左侧的相邻单元格上不匹配。 为了解决这个问题,我存储了以前的单元格边界数据,因此当它到达不匹配的单元格边界数据时,可以对其进行更正。

import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfContentByte;
import net.sf.jasperreports.engine.JRLineBox;
import net.sf.jasperreports.engine.JRPrintElement;
import net.sf.jasperreports.engine.export.JRPdfExporter;

import java.awt.*;

public class CustomPdfExporter extends JRPdfExporter {

    private float previousTop;
    private float previousHeight;
    private float prevY1;
    private float prevY2;

    private static final float FLOAT_ZERO = 0F;

    protected void exportBox(JRLineBox box, JRPrintElement element) {

        if (!isBoxVisible(box)) return;

        pdfContentByte.setLineCap(PdfContentByte.LINE_CAP_PROJECTING_SQUARE);

        float x1 = element.getX() + getOffsetX();
        float y1 = jasperPrint.getPageHeight() - element.getY() - getOffsetY();
        float x2 = element.getX() + getOffsetX() + element.getWidth();
        float y2 = jasperPrint.getPageHeight() - element.getY() - getOffsetY() - element.getHeight();

        Rectangle r = new Rectangle(x1, y2, x2, y1);

        if (previousTop == r.getTop() && previousHeight != r.getHeight()) {
            r.setBottom(prevY2);
        } else {
            prevY2 = y2;
            prevY1 = y1;
            previousHeight = r.getHeight();
            previousTop = r.getTop();
        }

        int borderFlag = 0;

        float lineWidth = 0;
        Color lineColor = null;

        if (box.getLeftPen().getLineWidth().floatValue() > FLOAT_ZERO) {
            borderFlag |= Rectangle.LEFT;
            lineWidth = box.getLeftPen().getLineWidth().floatValue();
            lineColor = box.getLeftPen().getLineColor();
        }

        if (box.getTopPen().getLineWidth().floatValue() > FLOAT_ZERO) {
            borderFlag |= Rectangle.TOP;
            if (lineWidth == 0) {
                lineWidth = box.getTopPen().getLineWidth().floatValue();
                lineColor = box.getTopPen().getLineColor();
            }
        }

        if (box.getRightPen().getLineWidth().floatValue() > FLOAT_ZERO) {
            borderFlag |= Rectangle.RIGHT;
            if (lineWidth == 0) {
                lineWidth = box.getRightPen().getLineWidth().floatValue();
                lineColor = box.getRightPen().getLineColor();
            }
        }

        if (box.getBottomPen().getLineWidth().floatValue() > FLOAT_ZERO) {
            borderFlag |= Rectangle.BOTTOM;
            if (lineWidth == 0) {
                lineWidth = box.getBottomPen().getLineWidth().floatValue();
                lineColor = box.getBottomPen().getLineColor();
            }
        }
        r.setBorder(borderFlag);
        r.setBorderColor(lineColor);
        r.setBorderWidth(lineWidth);

        pdfContentByte.rectangle(r);
        pdfContentByte.stroke();

        pdfContentByte.setLineDash(FLOAT_ZERO);
        pdfContentByte.setLineCap(PdfContentByte.LINE_CAP_ROUND);
    }

    private boolean isBoxVisible(JRLineBox box) {
        return box.getLeftPen().getLineWidth().floatValue() > FLOAT_ZERO
                || box.getTopPen().getLineWidth().floatValue() > FLOAT_ZERO
                || box.getRightPen().getLineWidth().floatValue() > FLOAT_ZERO
                || box.getBottomPen().getLineWidth().floatValue() > FLOAT_ZERO
                ;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM