Itext将图像适合单元格

时间:2016-03-09 18:01:06

标签: java pdf-generation itext

我试图将图像放到表格单元格中。但是,当我打印我的文档时,在其他图像的顶部有一个小的坐姿图像,如图所示。我面临什么问题?

Screenshot

public class PrintableLabel {
private static Font arial_12 = FontFactory.getFont("Arial", 12, Font.BOLD);
private static Font arial_11 = FontFactory.getFont("Arial", 11, Font.BOLD);
private static Font arial_10 = FontFactory.getFont("Arial", 10, Font.BOLD);
private static Font arial_9 = FontFactory.getFont("Arial", 9);
private static Font arial_9_bold = FontFactory.getFont("Arial", 9, Font.BOLD);

public static ByteArrayOutputStream generateOFSLabel(List<PrintableLabelConfig> configList) throws IOException{

    float topBottommagin = com.itextpdf.text.Utilities.inchesToPoints(1.0f);
float leftRightmagin = com.itextpdf.text.Utilities.inchesToPoints(0.25f);

float labelHeight = com.itextpdf.text.Utilities.inchesToPoints(6.0f);

    Document document = new Document(PageSize.LEGAL, leftRightmagin, leftRightmagin, topBottommagin, topBottommagin);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
      PdfWriter.getInstance(document, baos);
      document.open();
      // Main table
      PdfPTable table = new PdfPTable(2);
      table.setWidthPercentage(100.0f);
      table.getDefaultCell().setBorderWidth(0);

      PdfPCell cell_1 = new PdfPCell();//Table one the RIGHT
      cell_1.setPadding(20);
      cell_1.setFixedHeight(labelHeight);
      cell_1.setBorderWidth(0);

      PdfPCell cell_2 = new PdfPCell();//Table two the LEFT
      cell_2.setPadding(20);
      cell_2.setFixedHeight(labelHeight);
      cell_2.setBorderWidth(0);

      PdfPCell cell_3 = new PdfPCell();//Table three the BOTTOM RIGHT
      cell_3.setPadding(20);
      cell_3.setFixedHeight(labelHeight);
      cell_3.setBorderWidth(0);

      PdfPCell cell_4 = new PdfPCell();//Table four the BOTTOM LEFT
      cell_4.setPadding(20);
      cell_4.setFixedHeight(labelHeight);
      cell_4.setBorderWidth(0);

      PrintableLabelConfig config_1 = configList.get(0);
      PrintableLabelConfig config_2 = configList.get(1);
      PrintableLabelConfig config_3 = configList.get(2);
      PrintableLabelConfig config_4 = configList.get(3);

      cell_1.addElement(tableFor(config_1));
      cell_2.addElement(tableFor(config_2));

      cell_3.addElement(tableFor(config_3));
      cell_4.addElement(tableFor(config_4));

      table.addCell(cell_1);
      table.addCell(cell_2);

      table.addCell(cell_3);
      table.addCell(cell_4);

      document.add(table);
      document.close();
  } catch (DocumentException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
  }
    return baos;
}

private static Image logoElement() {
    String path = "/xampp/tomcat/webapps/ocsLabel/jsp/ofs/logo/ofs_logo.png";
    //Eclipse\Workspace\ocsLabel\WebContent\jsp\ofs\logo
    //String path = "/Eclipse/Workspace/ocsLabel/WebContent/jsp/ofs/logo/ofs_logo.png";
    Image image = null;
    try {
        image = Image.getInstance(path);
    } catch (BadElementException e) {
        e.printStackTrace();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return image;
}

private static final Image img = logoElement();
private static PdfPTable tableFor(PrintableLabelConfig tableConfig){
    String partNumber = "";
    String partNumberDesc = "";
    String serialNumber = "";
    Set<ReportStub> lossDataInfoSet = null;
    Date todayDate = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("M/dd/yyyy");
    String datePrint = sdf.format(todayDate);
    String length = "";
    String unit = "";

    if(tableConfig !=null){
        partNumber = tableConfig.getOfs_PartNumber();
        partNumberDesc = tableConfig.getOfs_Description();
        serialNumber = tableConfig.getOfs_SerialNumber();
        lossDataInfoSet = tableConfig.getLossDataInfo();
        length = tableConfig.getLength();
        unit = tableConfig.getUnit();
    }else{
        //Do nothing
    }

    PdfPTable table = new PdfPTable(3);
    table.setWidthPercentage(100f);

    insertCell(table, img, 0, 3, 2);

    /*(PdfPTable table, String text, int align, int colspan, Font font, int padding, float grayFill)*/
    insertCell(table, "Part Number: ", Element.ALIGN_LEFT, 1, arial_12, 0, 0.0f);// This is Part Numbertitile
    insertCell(table, partNumber, Element.ALIGN_LEFT, 2, arial_10, 0, 0.0f); // This is Part Number info

    insertCell(table, "Description: ", Element.ALIGN_LEFT, 0, arial_12, 0, 0.0f); // This is Description titile
    insertCell(table, partNumberDesc, Element.ALIGN_LEFT, 2, arial_9_bold, 0, 0.0f); // This is Description info

    insertCell(table, "Serial #: ", Element.ALIGN_LEFT, 0, arial_12, 0, 0.0f); // This is Serial # titile
    insertCell(table, serialNumber, Element.ALIGN_LEFT, 2, arial_9_bold, 0, 0.0f); // This is Serial # info

    insertCell(table, "Loss Data", Element.ALIGN_CENTER, 0, arial_11, 0, 0.85f); // This is Loss Data
    insertCell(table, "Insertion Loss dB", Element.ALIGN_CENTER, 0, arial_11, 0, 0.85f); // This is Loss Data
    insertCell(table, "Return Loss dB", Element.ALIGN_CENTER, 0, arial_11, 0, 0.85f); // This is Loss Data

    if(lossDataInfoSet !=null && lossDataInfoSet.size() >= 1){
        for(ReportStub report: tableConfig.getLossDataInfo()){
            insertCell(table, "LC-"+report.getFib_Sequence_Nr()+"    A/B", Element.ALIGN_RIGHT, 0, arial_10, 0, 0.0f);
            insertCell(table, MathUtil.precision(2, report.getFim_IL_End_A())+" / "+MathUtil.precision(2, report.getFim_IL_End_B()), Element.ALIGN_RIGHT, 0, arial_10, 0, 0.0f);
            insertCell(table, MathUtil.precision(2, report.getFim_Reflectance_End_A())+" / "+MathUtil.precision(2, report.getFim_Reflectance_End_B()), Element.ALIGN_RIGHT, 0, arial_10, 0, 0.0f);
        }
    }else{
        insertCell(table, "LC-    A/B", Element.ALIGN_RIGHT, 0, arial_11, 0, 0.0f);
        insertCell(table, "/", Element.ALIGN_RIGHT, 0, arial_11, 0, 0.0f);
        insertCell(table, "/", Element.ALIGN_RIGHT, 0, arial_11, 0, 0.0f);
    }

    insertCell(table, "Date Of MFG", Element.ALIGN_CENTER, 0, arial_9, 0, 0.85f); // This is "Date Of MFG"
    insertCell(table, length+" "+unit, Element.ALIGN_CENTER, 0, arial_10, 0, 0.85f);
    insertCell(table, datePrint, Element.ALIGN_CENTER, 0, arial_9, 0, 0.85f);

    return table;
}

private static void insertCell(PdfPTable table, Image img, int align, int colspan, int padding){

    //scale image
    img.scalePercent(90);

    //create a new cell with the specified Image
    PdfPCell cell = new PdfPCell(img);
    //set the cell alignment
    if(align != 0){
        cell.setHorizontalAlignment(align);
    }else{
        //Do nothing
    }
    //set the cell column span in case you want to merge two or more cells
    if(colspan != 0){
        cell.setColspan(colspan);
    }else{
        //Do nothing
    }
    //set the cell padding
    if(padding != 0){
        cell.setPadding(padding);
    }else{
        //Do nothing
    }

    //add the call to the table
    table.addCell(cell);
}

private static void insertCell(PdfPTable table, String text, int align, int colspan, Font font, int padding, float grayFill){
    //create a new cell with the specified Text and Font
    PdfPCell cell = new PdfPCell(new Phrase(text.trim(), font));
    //set the cell alignment
    cell.setHorizontalAlignment(align);
    //set the cell column span in case you want to merge two or more cells
    if(colspan != 0){
        cell.setColspan(colspan);
    }else{
        //Do nothing
    }
    //set the cell padding
    if(padding != 0){
        cell.setPadding(padding);
    }else{
        //Do nothing
    }
    //set gray fill
    if(grayFill != 0.0){
        cell.setGrayFill(grayFill);
    }else{
        //Do nothing
    }
    //add the call to the table
    table.addCell(cell);
} 
}//End public class PrintableLabel

0 个答案:

没有答案