2014-04-01 44 views
1

嗯,在创建新页面时遇到了一些麻烦。到达页面末尾时出现PDF问题

我从查询中获取我的数据。

对不起,如果我发布了很多代码,但这一切都需要,我不知道如何做一个MCVE,因为我很新的工作与PDF。

这个问题是:

之前写的代码下一块我检查当前y坐标,我能做到这一点,但是当我需要创建新的一页,重新开始写作,以及发生this

这里是在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; 
    } 

此方法不一样,但是第一次同时使用,我知道我不应该也不能使用硬编码值,如10或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); 
     } 
    } 
} 

我几乎可以肯定的误差来源于此方法:

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 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++; 
} 

任何帮助,引导,将是非常apreciated并在此先感谢

+0

你的'renderSubscriptionNew'迭代'dataList',并且三个或一个这样的列表元素相邻排列。因此,如果三个数据元素彼此相邻,则在调用“rtext”期间不得切换页面,但必须已在“renderSubscriptionNew”中检查数据三倍并切换页面一次(*在调用' rtext'为三元组的第一个条目)。我怕你的代码需要一些清理。 – mkl

+0

@mkl所以如果我很好地理解你在说什么,我应该删除rText上的所有checkContentStream,但是为了价值,我是对吗?这样它将验证我的Y坐标每行一次,并将值检查,所以在较大的文本,它总是会分页。 – Frakcool

+0

其实我认为从rtext里面检查是一个好主意,我认为检查应该是代码知道是否有多个数据紧挨着或者没有,即在renderSubscriptionNew中。也许你的想法已经足够,但我不知道。 – mkl

回答

2

感谢@mkl他的评论,我解决了这个问题是这样的:

我下面的代码添加上“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(最后一个):

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; 
}