Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 到达页面末尾时出现PDF问题_Java_Pdf_Pdf Generation_Rendering_Pdfbox - Fatal编程技术网

Java 到达页面末尾时出现PDF问题

Java 到达页面末尾时出现PDF问题,java,pdf,pdf-generation,rendering,pdfbox,Java,Pdf,Pdf Generation,Rendering,Pdfbox,我在创建新页面时遇到了一些问题 我从查询中获取数据 抱歉,如果我发布了很多代码,但是这些都是必需的,我不知道如何做MCVE,因为我对使用PDF非常陌生 这个问题是: 在编写下一段代码之前,我检查一下当前的Y坐标,我可以做到这一点,但当我需要创建新页面并重新开始编写时,情况会很好 以下是PDF中使用的代码: public float checkContentStream(float y) throws Exception { float newY = checkYCoord(y, 3, 1

我在创建新页面时遇到了一些问题

我从查询中获取数据

抱歉,如果我发布了很多代码,但是这些都是必需的,我不知道如何做MCVE,因为我对使用PDF非常陌生

这个问题是:

在编写下一段代码之前,我检查一下当前的Y坐标,我可以做到这一点,但当我需要创建新页面并重新开始编写时,情况会很好

以下是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;
    }
此方法检查是否达到底部边距=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;
    }
我几乎可以肯定错误来自此方法:

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;
}
private float rText(字符串文本、整型宽度、浮点x、浮点y、整型空格、PDFont font、布尔值isValue)引发异常{
float newY=y;
int rowHeight=0;
ArrayList rowList=getRows(文本、宽度、字体);
如果(isValue){
newY=checkContentStream(newY);
newY=newY==700?680:newY;
用于(字符串行:行列表){
如果(行高>=10){
newY=checkContentStream(newY-10);
newY=newY==700?680:newY;
}
否则{
newY=checkContentStream(newY);
newY=newY==700?680:newY;
}
content.beginText();
content.moveTextPositionByAmount(x,newY);
内容:抽绳(行);
content.endText();
行高=行高+10;
}
}
否则{
newY=checkContentStream(newY,rowList.size(),空格);
newY=newY==700?680:newY;
用于(字符串行:行列表){
content.beginText();
content.moveTextPositionByAmount(x,newY-行高);
内容:抽绳(行);
content.endText();
行高=行高+10;
}
newY-=(行高-9);
}
返回newY;
}
另一个类中的此方法从查询中获取数据:

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++;
}
private float renderSubscriptionNew(PdfRenderingPC pdf,float y,SubscriptionNew sNew)引发异常{
DataResponse dr=dataSvc.buildResponse(对开本、sNew、unitSvc);
List dataList=dr.getDataList();
int i=0;
int j=0;
浮动y2[]=新浮动[3];
用于(数据:数据列表){
String labelField=constants.getString(data.getName());
字符串值=getValue(数据);
浮点xCoord=0;
布尔getNewRow=false;
int fieldSize=1;
开关(一){
案例0:
案例4:
xCoord=左_边距;
打破
案例1:
案例5:
xCoord=中柱;
打破
案例2:
案例6:
xCoord=右列;
getNewRow=true;
打破
案例3:
xCoord=左_边距;
getNewRow=true;
打破
}
如果(getNewRow){
如果(j==0){
y=pdf.rText(xCoord,y,10,labelField,value,fieldSize);
}
否则{
y=pdf.rText(xCoord,y,10,labelField,value,fieldSize);
浮动最小值=y;
对于(int k=(j-1);k>=0;k--){
if(y2[k]
任何帮助,向导,都会非常感激,并提前感谢他的评论,我这样解决了它:

我在“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++;
}
if(可见){
如果(getNewRow){
如果(j==0){
y=pdf.checkContentStream(y,3,10);
y=pdf.rText(xCoord,y,10,labelField,value,
字段大小);
}
否则{
y=pdf.rText(xCoord,y,10,labelField,value,
字段大小);
浮动最小值=y;
对于(int k=(j-1);k>=0;k--){
if(y2[k]
这是rText(最后一个):

private float rText(字符串文本、整数宽度、浮点x、浮点y、整数空格、,
PDFont字体,布尔值)引发异常{
float newY=y;
int rowHeight=0;
ArrayList rowList=getRows(文本、宽度、字体);
如果(isValue){
用于(字符串行:行列表){
如果(行高>=10){
newY=checkContentStream(newY-10);
newY=newY==700?680:newY;
}
否则{
}
content.beginText();
content.moveTextPositionByAmount(x,newY);
内容:抽绳(行);
content.endText();
行高=行高+10;
}
}
否则{
用于(字符串行:行列表){
content.beginText();
content.moveTextPositionByAmount(x,newY-行高);
内容:抽绳(行);
content.endText();
行高=行高+10;
}
newY-=(行高-9);
}
返回newY;
}

您的
renderSubscriptionNew
数据列表
上迭代,三个或一个这样的列表元素排列在一起。因此,如果三个数据元素相邻,则在调用
rtext
期间不得切换页面,而必须在
renderSubscriptionNew
中检查数据三元组,并仅为其切换页面一次(在调用
rtext
三元组的第一个条目之前)。恐怕您的代码需要进行一些清理。@mkl所以,如果我完全理解您的意思,我应该删除rText上的所有checkContentStream,但对于值,我是对的吗?这样,它将验证我的Y坐标每行一次,并检查值,因此在较大的文本中,它将始终进行分页符。实际上,我怀疑从rtext内部检查是否是一个好主意,我认为检查应该是正确的
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++;
}
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;
}