到达页面末尾时出现PDF问题 [英] PDF issue while reaching end of page

查看:90
本文介绍了到达页面末尾时出现PDF问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好,我在创建新页面时遇到了麻烦.

Well I'm having some troubles when creating new pages.

我从查询中获取数据.

很抱歉,如果我发布了很多代码,但这都是必需的,而且我不确定如何做MCVE,因为我在使用PDF时还很陌生.

Sorry if I post a lot of code, but it's all needed and I'm not sure how to do a MCVE as I'm pretty new at working with PDF.

此问题是:

在编写下一个代码块之前,我可以检查当前的Y坐标,但是我需要创建新的Page并再次开始编写时,

Before writing next block of code I check current Y coord, I can achieve this, but when I need to create new Page and start writing again, well happens this

以下是PDF中使用的代码:

Here are the codes used in the PDF:

public float checkContentStream(float y) throws Exception {
    float newY = checkYCoord(y, 3, 10);
    if (newY == 700) {
        if (content != null) {
        content.close();
        }

        File file = new File(logoPath);
        PDJpeg logoImg = new PDJpeg(doc, new FileInputStream(file));
        PDPage page = new PDPage(PDPage.PAGE_SIZE_LETTER);
        doc.addPage(page);
        content = new PDPageContentStream(doc, page);
        content.drawImage(logoImg, 50, 720);
        rHeader();
    }
    return newY;
    }

此方法检查是否已达到BOTTOM MARGIN = 60

This method checks if reached BOTTOM MARGIN = 60

private float checkYCoord(float y, int lines, int space) {
    float newY = y;
    for (int i = 0; i < lines; i++) {
        if ((newY - space) <= BOTTOM_MARGIN) {
        newY = 700f;
        return newY;
        } else {
        newY = newY - space;
        }
    }
    return y;
    }

此方法与1st相同,但同时使用了这两种方法,我知道我不应该也不得使用硬编码的值,例如10或3.

This method does the same as 1st but used both, I know I shouldn't and must not use hardcoded values, like 10 or 3.

public float checkContentStream(float y, int lines, int space) throws Exception {
    float newY = checkYCoord(y, lines, space);
    if (newY == 700) {
        if (content != null) {
        content.close();
        }
        File file = new File(logoPath);
        PDJpeg logoImg = new PDJpeg(doc, new FileInputStream(file));
        PDPage page = new PDPage(PDPage.PAGE_SIZE_LETTER);
        doc.addPage(page);
        content = new PDPageContentStream(doc, page);
        content.drawImage(logoImg, 50, 720);
        rHeader();
    }
}

public float rText(float x, float y, int space, String labelField, String value, int fieldSize) throws Exception {
    if(fieldSize == 1){
        return rText(x, y, space, labelField, value, FIELD_WIDTH, VALUE_WIDTH);
        }
    else{
        if(fieldSize == 2){
        return rText(x, y, space, labelField, value, FIELD_WIDTH, DESC_WIDTH);
        }
        else{
        return rText(x, y, space, labelField, value, FIELD_WIDTH, TEXT_WIDTH);
        }
    }
}

我几乎可以确定错误是由以下方法引起的:

I'm almost sure the error comes from this method:

public float rText(float x, float y, int space, String labelField, String value, int fieldWidth, int valueWidth) throws Exception {
    PDFont font = PDType1Font.TIMES_BOLD;
    content.setFont(font, 9);
    float y1 = 0f;
    float y2 = 0f;
    //y = y >= 700 ? 700 : checkContentStream(y, 3, 10);
    if (value == null) {
        //y = checkYCoord(y, 1, 10);
        return rText(labelField, fieldWidth, x, y - 19, space, font, false);
        }else {
        //y = checkYCoord(y, 3, 10);
        y1 = rText(labelField, fieldWidth, x, y - 20, space, font, false);
        font = PDType1Font.TIMES_ROMAN;
        content.setFont(font, 9);
        y = checkYCoord(y, 3, 10); // Comment / Uncoment this line
        y2 = rText(value, valueWidth, x + fieldWidth + 10, y - 20, space, font, true);
        if (y1 >= y2) {
        return y2;
        } else {
        return y1;
        }
    }
}

此方法绘制文字

private float rText(String text, int width, float x, float y, int space, PDFont font, boolean isValue) throws Exception {
    float newY = y;
    int rowHeight = 0;
    ArrayList<String> rowList = getRows(text, width, font);
    if(isValue){
        newY = checkContentStream(newY);
        newY = newY == 700 ? 680 : newY;
        for (String row : rowList) {
        if(rowHeight >= 10){
            newY = checkContentStream(newY - 10);
            newY = newY == 700 ? 680 : newY;
        }
        else{
            newY = checkContentStream(newY);
            newY = newY == 700 ? 680 : newY;
        }
        content.beginText();
        content.moveTextPositionByAmount(x, newY);
        content.drawString(row);
        content.endText();
        rowHeight = rowHeight + 10;
        }
    }
    else{
        newY = checkContentStream(newY, rowList.size(), space);
        newY = newY == 700 ? 680 : newY;
        for(String row : rowList){
        content.beginText();
        content.moveTextPositionByAmount(x, newY - rowHeight);
        content.drawString(row);
        content.endText();
        rowHeight = rowHeight + 10;
        }
        newY -= (rowHeight - 9);
    }
    return newY;
}

此方法在另一个类中,可从查询中获取数据:

This method which is in another class brings data from query:

private float renderSubscriptionNew(PdfRenderingPC pdf, float y, SubscriptionNew sNew) throws Exception {
    DataResponse dr = dataSvc.buildResponse(folio, sNew, unitSvc);
    List<Data> dataList = dr.getDataList();

    int i = 0;
    int j = 0;
    float y2[] = new float[3];
    for (Data data : dataList) {
        String labelField = constants.getString(data.getName());
        String value = getValue(data);
        float xCoord = 0;
        boolean getNewRow = false;
        int fieldSize = 1;
        switch (i) {
        case 0:
        case 4:
        xCoord = LEFT_MARGIN;
        break;
        case 1:
        case 5:
        xCoord = MIDDLE_COL;
        break;
        case 2:
        case 6:
        xCoord = RIGHT_COL;
        getNewRow = true;
        break;
        case 3:
        xCoord = LEFT_MARGIN;
        getNewRow = true;
        break;
        }
        if (getNewRow) {
        if(j == 0){
            y = pdf.rText(xCoord, y, 10, labelField, value, fieldSize);
        }
        else{
            y = pdf.rText(xCoord, y, 10, labelField, value, fieldSize);
            float min = y;
            for(int k = (j - 1); k >= 0; k--){
            if(y2[k] < min){
                min = y2[k];
            }
            }
            y = min;
            j = 0;
        }   
        } else {
            y2[j] = pdf.rText(xCoord, y, 10, labelField, value, fieldSize);
            j++;
        }
        i++;
}

任何帮助,指导人员都会非常感激,并在此先感谢

Any help, guide, would be pretty apreciated and thanks in advance

推荐答案

感谢

Thanks to @mkl for his comment, I solved it like this:

我在"renderSubscriptionNew"上添加了以下代码

I added the following code on 'renderSubscriptionNew'

if(visible){
    if (getNewRow) {
        if(j == 0){
        y = pdf.checkContentStream(y, 3, 10);
        y = pdf.rText(xCoord, y, 10, labelField, value, 
                  fieldSize);
        }
        else{
        y = pdf.rText(xCoord, y, 10, labelField, value, 
                  fieldSize);
        float min = y;
        for(int k = (j - 1); k >= 0; k--){
            if(y2[k] < min){
            min = y2[k];
            }
        }
        y = min;
        j = 0;
        }

    } else {
        if(j == 0){
        y = pdf.checkContentStream(y, 3, 10);
        }
        y2[j] = pdf.rText(xCoord, y, 10, labelField, value, 
                  fieldSize);
        j++;
    }
    }
    i++;
}

这是rText(最后一个):

And this is rText (the last one):

private float rText(String text, int width, float x, float y, int space,
        PDFont font, boolean isValue) throws Exception {
float newY = y;
int rowHeight = 0;
ArrayList<String> rowList = getRows(text, width, font);
if(isValue){
    for (String row : rowList) {
    if(rowHeight >= 10){
        newY = checkContentStream(newY - 10);
        newY = newY == 700 ? 680 : newY;
    }
    else{
    }
    content.beginText();
    content.moveTextPositionByAmount(x, newY);
    content.drawString(row);
    content.endText();
    rowHeight = rowHeight + 10;
    }
}
else{
    for(String row : rowList){
    content.beginText();
    content.moveTextPositionByAmount(x, newY - rowHeight);
    content.drawString(row);
    content.endText();
    rowHeight = rowHeight + 10;
    }
    newY -= (rowHeight - 9);
}
return newY;
}

这篇关于到达页面末尾时出现PDF问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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