Java iText PDF动态生成

Java iText PDF动态生成,java,pdf,alignment,itext,Java,Pdf,Alignment,Itext,我有一个动态生成PDF的类, 其中我有4-6列。。 在第二列描述中,动态显示项的名称, 它大约有20-40个字符。。现在,我希望一致地显示行高。。 如果任何一个描述字符超过30个字符,我必须将所有行高度显示为相等高度(20f) 如果字符不超过30个字符,并且在30个字符以下结束,我必须将所有行高度显示为(10f) 如何动态地对齐相同的对象? 这是我的密码: mainTable.addCell (PDFUtil.getBlankLine (20f)); float[] colsWidth = {0

我有一个动态生成PDF的类, 其中我有4-6列。。 在第二列描述中,动态显示项的名称, 它大约有20-40个字符。。现在,我希望一致地显示行高。。 如果任何一个描述字符超过30个字符,我必须将所有行高度显示为相等高度(20f) 如果字符不超过30个字符,并且在30个字符以下结束,我必须将所有行高度显示为(10f)

如何动态地对齐相同的对象? 这是我的密码:

mainTable.addCell (PDFUtil.getBlankLine (20f));
float[] colsWidth = {0.5f,3.8f,0.9f,0.9f,1.3f,0.9f,1.3f,1.4f}; 
PdfPTable itemListTab = new PdfPTable(colsWidth);

PdfPCell slNo = PDFUtil.getTableHeaderCell(PDFUtil.S_NO,
        PDFUtil.getBoldFont());
PdfPCell itmDesc = PDFUtil.getTableHeaderCell(PDFUtil.ITEM_DESC,
        PDFUtil.getBoldFont());
PdfPCell loose = PDFUtil.getTableHeaderCell(PDFUtil.LOOSE_QTY,
        PDFUtil.getBoldFont());
PdfPCell full = PDFUtil.getTableHeaderCell(PDFUtil.FULL_QTY,
        PDFUtil.getBoldFont());
PdfPCell unitPrc = PDFUtil.getTableHeaderCell(PDFUtil.QUOTED_PRICE,
        PDFUtil.getBoldFont());
PdfPCell disc = PDFUtil.getTableHeaderCell(
        PDFUtil.ITM_DISCOUNT_PERCENT, PDFUtil.getBoldFont());
PdfPCell netAmt = PDFUtil.getTableHeaderCell(PDFUtil.NET_PRICE,
        PDFUtil.getBoldFont());
PdfPCell amt = PDFUtil.getTableHeaderCell(PDFUtil.AMOUNT_LABEL, PDFUtil
        .getBoldFont());
amt.setHorizontalAlignment(Element.ALIGN_RIGHT);
itemListTab.addCell(slNo);
itemListTab.addCell(itmDesc);
itemListTab.addCell(full);
itemListTab.addCell(loose);
itemListTab.addCell(unitPrc);
itemListTab.addCell(disc);
itemListTab.addCell(netAmt);
itemListTab.addCell(amt);
ArrayList aList = vendPOVO.getItemList();
if (aList != null)
{
    Object[] iList = aList.toArray();
    Arrays.sort(iList, new ItemSNoComparator(aList));
    int len = iList.length;
    InventoryLogger.logDebug("After sorting the item list by SNo");
    InventoryLogger.logDebug("length"+len);
    for (int i = 0; i < len; i++)
    {
        InventoryLogger.logDebug("inside for");
        ItemMasterVO itmMstrVO = (ItemMasterVO) iList[i];
        if (InventoryConstants.TRUE.equals(itmMstrVO
            .getVendPurOrdInd()))
        {
            String ctry = " ";
            InventoryLogger.logDebug ("Country of origin"+itmMstrVO.getCtryOfOrigin());
            if(itmMstrVO.getCtryOfOrigin() != null)
            {
                ctry =  InventoryUtil.getStaticElementDescription(
                        itmMstrVO.getCtryOfOrigin(),InventoryConstants.STATIC_ELE_CTRY);
            }
            String uomDesc = "";
            String looseUomTemp = "";
            if(itmMstrVO.getUomDesc().matches(InventoryConstants.UOM_KGGM) )
            {
                uomDesc = "1 KG ";
                looseUomTemp = "KG";
            }
            else
            {
                uomDesc = itmMstrVO.getUomDesc();
                looseUomTemp = itmMstrVO.getLooseUOM();
            }
            String flFlagCheck = "";
            InventoryLogger.logDebug("fl"+itmMstrVO.getFullLooseInd());
            if(itmMstrVO.getFullLooseInd().matches(InventoryConstants.FULL_LOOSE_IND_F))
            {
                flFlagCheck = itmMstrVO.getFullUOM();
            }
            else
            {
                flFlagCheck = itmMstrVO.getLooseUOM();
            }

            itemListTab.addCell(new PdfPCell(new Phrase(itmMstrVO
                .getCusQuoSerialNo(), PDFUtil
                .getNormalFont())));

            itemListTab.addCell(new PdfPCell(new Phrase(itmMstrVO
                .getItemDisplayName()+ " / " + uomDesc + "/" + ctry , PDFUtil
                .getNormalFont())));

            itemListTab.addCell(new PdfPCell(new Phrase(Long
                .toString(itmMstrVO.getQuoFull())+" "+itmMstrVO.getFullUOM(), PDFUtil
                .getNormalFont())));
            PdfPCell cellone = new PdfPCell(new Phrase(Long.toString(itmMstrVO.getQuoLoose())+" "+itmMstrVO.getLooseUOM(), PDFUtil.getNormalFont()));
            cellone.setFixedHeight(25f);
            itemListTab.addCell(cellone);itemListTab.addCell(new PdfPCell(new Phrase(InventoryUtil.getTwoDecDigPrecisionMin(itmMstrVO.getDiscUnitPrice())+"/"+flFlagCheck, PDFUtil
                .getNormalFont())));

            itemListTab.addCell(new PdfPCell(new Phrase(InventoryUtil.getTwoDecDigPrecisionMin(itmMstrVO.getDiscPercent()), PDFUtil
                .getNormalFont())));
            itemListTab.addCell(new PdfPCell(new Phrase((InventoryUtil.getTwoDecDigPrecisionMin(itmMstrVO.getDiscUnitPrice()))+"/" +flFlagCheck, PDFUtil.getNormalFont())));
            itemListTab.addCell(new PdfPCell(new Phrase(InventoryUtil.getTwoDecDigPrecisionMin(itmMstrVO.getAmount()), PDFUtil
                .getNormalFont())));
        }
    }
}
PdfPCell addCell = new PdfPCell(itemListTab);
addCell.setBorder(0);
mainTable.addCell(addCell);
mainTable.addCell(PDFUtil.getBlankLine(20f));
float[]colsWidth={0.5f,3.8f,0.9f,0.9f,1.3f,0.9f,1.3f,1.4f};
PdfPTable itemListTab=新的PdfPTable(colsWidth);
PdfPCell slNo=PDFUtil.getTableHeaderCell(PDFUtil.S_编号,
PDFUtil.getBoldFont());
PdfPCell itmDesc=PDFUtil.getTableHeaderCell(PDFUtil.ITEM_DESC,
PDFUtil.getBoldFont());
PdfPCell loose=PDFUtil.getTableHeaderCell(PDFUtil.loose\u数量,
PDFUtil.getBoldFont());
PdfPCell full=PDFUtil.getTableHeaderCell(PDFUtil.full\U数量,
PDFUtil.getBoldFont());
PdfPCell unitPrc=PDFUtil.getTableHeaderCell(PDFUtil.QUOTED_价格,
PDFUtil.getBoldFont());
PdfPCell disc=PDFUtil.getTableHeaderCell(
PDFUtil.ITM_折扣率,PDFUtil.getBoldFont());
PdfPCell netAmt=PDFUtil.getTableHeaderCell(PDFUtil.NET_价格,
PDFUtil.getBoldFont());
PdfPCell amt=PDFUtil.getTableHeaderCell(PDFUtil.AMOUNT_标签,PDFUtil
.getBoldFont());
金额设置水平对齐(元素右对齐);
itemListTab.addCell(slNo);
itemListTab.addCell(itmDesc);
itemListTab.addCell(完整);
itemListTab.addCell(松散);
itemListTab.addCell(单位PRC);
itemListTab.addCell(光盘);
itemListTab.addCell(netAmt);
itemListTab.addCell(金额);
ArrayList aList=vendPOVO.getItemList();
if(aList!=null)
{
Object[]iList=aList.toArray();
sort(iList,newitemsnocomparator(aList));
int len=iList.length;
logDebug(“在通过SNo对项目列表进行排序之后”);
logDebug(“长度”+len);
对于(int i=0;i
嘿,伙计们,我终于找到了解决方案:

  mainTable.addCell(PDFUtil.getBlankLine(20f));
  float[] colsWidth = {0.5f,3.8f,0.9f,0.9f,1.3f,0.9f,1.3f,1.4f}; 
  PdfPTable itemListTab = new PdfPTable(colsWidth);

  PdfPCell slNo = PDFUtil.getTableHeaderCell(PDFUtil.S_NO,
        PDFUtil.getBoldFont());

  PdfPCell itmDesc = PDFUtil.getTableHeaderCell(PDFUtil.ITEM_DESC,
        PDFUtil.getBoldFont());

  PdfPCell loose = PDFUtil.getTableHeaderCell(PDFUtil.LOOSE_QTY,
        PDFUtil.getBoldFont());
  PdfPCell full = PDFUtil.getTableHeaderCell(PDFUtil.FULL_QTY,
        PDFUtil.getBoldFont());
  PdfPCell unitPrc = PDFUtil.getTableHeaderCell(PDFUtil.QUOTED_PRICE,
        PDFUtil.getBoldFont());

  PdfPCell disc = PDFUtil.getTableHeaderCell(
        PDFUtil.ITM_DISCOUNT_PERCENT, PDFUtil.getBoldFont());
  PdfPCell netAmt = PDFUtil.getTableHeaderCell(PDFUtil.NET_PRICE,
        PDFUtil.getBoldFont());
  PdfPCell amt = PDFUtil.getTableHeaderCell(PDFUtil.AMOUNT_LABEL, PDFUtil
        .getBoldFont());
  amt.setHorizontalAlignment(Element.ALIGN_RIGHT);
  itemListTab.addCell(slNo);

  itemListTab.addCell(itmDesc);

  itemListTab.addCell(full);
  itemListTab.addCell(loose);
  itemListTab.addCell(unitPrc);

  itemListTab.addCell(disc);
  itemListTab.addCell(netAmt);
  itemListTab.addCell(amt);ArrayList aList = vendPOVO.getItemList();
  if (aList != null)
  {
    Object[] iList = aList.toArray();
    Arrays.sort(iList, new ItemSNoComparator(aList));
    int len = iList.length;
    InventoryLogger.logDebug("After sorting the item list by SNo");
    InventoryLogger.logDebug("length"+len);



    int lenthChr = 0;
    int lineLength = 34;
    float maxLineLength = 34;
    for (int j = 0; j < len; j++)
    {
        ItemMasterVO itmMstrVO = (ItemMasterVO) iList[j];
        if (InventoryConstants.TRUE.equals(itmMstrVO.getVendPurOrdInd()))
        {

    String ctry = " ";
    if(itmMstrVO.getCtryOfOrigin() != null)
    {
        ctry =  InventoryUtil.getStaticElementDescription(itmMstrVO.getCtryOfOrigin(),InventoryConstants.STATIC_ELE_CTRY);
    }
    String uomDesc = "";
    String looseUomTemp = "";
    String Space = "";
    if(itmMstrVO.getUomDesc().matches(InventoryConstants.UOM_KGGM) )
    {
        uomDesc = "1 KG ";
        looseUomTemp = "KG";
    }
    else
    {
        uomDesc = itmMstrVO.getUomDesc();
        looseUomTemp = itmMstrVO.getLooseUOM();
    }

    int lenthChrTmp = (itmMstrVO.getItemDesc1() + " / " + itmMstrVO.getItemDesc2() + " / " + uomDesc+ "/" + ctry).length() ;

    lenthChr = (lenthChr > lenthChrTmp)?lenthChr:lenthChrTmp;


        }
    }



    int mod = ((lenthChr % lineLength)> 0)?1:0;
    maxLineLength = ((lenthChr/lineLength) + mod) * 13;

    for (int i = 0; i < len; i++)
    {
        InventoryLogger.logDebug("inside for");
        ItemMasterVO itmMstrVO = (ItemMasterVO) iList[i];
        if (InventoryConstants.TRUE.equals(itmMstrVO
        .getVendPurOrdInd()))
        {
    String ctry = " ";
    InventoryLogger.logDebug("Country of origin"+itmMstrVO.getCtryOfOrigin());
    if(itmMstrVO.getCtryOfOrigin() != null)
    {
        ctry =  InventoryUtil.getStaticElementDescription(
      itmMstrVO.getCtryOfOrigin(),InventoryConstants.STATIC_ELE_CTRY);
    }
    String uomDesc = "";
    String looseUomTemp = "";
    if(itmMstrVO.getUomDesc().matches(InventoryConstants.UOM_KGGM) )
    {
        uomDesc = "1 KG ";
        looseUomTemp = "KG";
    }
    else
    {
        uomDesc = itmMstrVO.getUomDesc();
        looseUomTemp = itmMstrVO.getLooseUOM();
    }
    String flFlagCheck = "";
    InventoryLogger.logDebug("fl"+itmMstrVO.getFullLooseInd());
    if(itmMstrVO.getFullLooseInd().matches(InventoryConstants.FULL_LOOSE_IND_F))
    {
        flFlagCheck = itmMstrVO.getFullUOM();
    }
    else
    {
        flFlagCheck = itmMstrVO.getLooseUOM();
    }

    itemListTab
            .addCell(new PdfPCell(new Phrase(itmMstrVO
        .getCusQuoSerialNo(), PDFUtil
        .getNormalFont())));

    PdfPCell descCell = new  PdfPCell(new Phrase(itmMstrVO
            .getItemDisplayName()+ " / " + uomDesc + "/" + ctry , PDFUtil
            .getNormalFont()));
    descCell.setFixedHeight(maxLineLength);
    itemListTab.addCell(descCell);

    itemListTab.addCell(new PdfPCell(new Phrase(Long
            .toString(itmMstrVO.getQuoFull())+" "+itmMstrVO.getFullUOM(), PDFUtil
            .getNormalFont())));
    PdfPCell cellone = new PdfPCell(new Phrase(Long.toString(itmMstrVO.getQuoLoose())+" "+itmMstrVO.getLooseUOM(), PDFUtil.getNormalFont()));itemListTab.addCell(cellone);itemListTab.addCell(new PdfPCell(new Phrase(InventoryUtil.getTwoDecDigPrecisionMin(itmMstrVO.getDiscUnitPrice())+"/"+flFlagCheck, PDFUtil
            .getNormalFont())));

    itemListTab.addCell(new PdfPCell(new Phrase(InventoryUtil.getTwoDecDigPrecisionMin(itmMstrVO.getDiscPercent()), PDFUtil
            .getNormalFont())));
    itemListTab.addCell(new PdfPCell(new Phrase((InventoryUtil.getTwoDecDigPrecisionMin(itmMstrVO.getDiscUnitPrice()))+"/" +flFlagCheck, PDFUtil.getNormalFont())));
    itemListTab.addCell(new PdfPCell(new Phrase(InventoryUtil.getTwoDecDigPrecisionMin(itmMstrVO.getAmount()), PDFUtil
            .getNormalFont())));
        }
    }
  }
  PdfPCell addCell = new PdfPCell(itemListTab);
  addCell.setBorder(0);
  mainTable.addCell(addCell);
mainTable.addCell(PDFUtil.getBlankLine(20f));
float[]colsWidth={0.5f,3.8f,0.9f,0.9f,1.3f,0.9f,1.3f,1.4f};
PdfPTable itemListTab=新的PdfPTable(colsWidth);
PdfPCell slNo=PDFUtil.getTableHeaderCell(PDFUtil.S_编号,
PDFUtil.getBoldFont());
PdfPCell itmDesc=PDFUtil.getTableHeaderCell(PDFUtil.ITEM_DESC,
PDFUtil.getBoldFont());
PdfPCell loose=PDFUtil.getTableHeaderCell(PDFUtil.loose\u数量,
PDFUtil.getBoldFont());
PdfPCell full=PDFUtil.getTableHeaderCell(PDFUtil.full\U数量,
PDFUtil.getBoldFont());
PdfPCell unitPrc=PDFUtil.getTableHeaderCell(PDFUtil.QUOTED_价格,
PDFUtil.getBoldFont());
PdfPCell disc=PDFUtil.getTableHeaderCell(
PDFUtil.ITM_折扣率,PDFUtil.getBoldFont());
PdfPCell netAmt=PDFUtil.getTableHeaderCell(PDFUtil.NET_价格,
PDFUtil.getBoldFont());
PdfPCell amt=PDFUtil.getTableHeaderCell(PDFUtil.AMOUNT_标签,PDFUtil
G