Hi,
 
I am a Java/J2EE developer in Parsippany, NJ. I have been asked to use iText to generate and process PDF files for an upcoming project. One of the requirements of this project is to place column text at specified X1 (lower left X corner), Y1 (lower left Y corner), X2 (upper right X corner) and Y2 (upper right Y corner) positions on a PDF file and to align this column at a position specified by the client (either left, right or center). The parameters for this task (X1, Y1, X2 and Y2 coordinates, type of column text to right, type of alignment, etc.) are specified in a property file.
 
In this case, I am trying to write a column text that starts at X1 position 44 and Y1 position 45 and ending at X2 position 123 and Y2 position 67. I find that if the column text is left-aligned, then it starts at the correct X1 position. However, if its alignment is changed to Right or Centered, then its X1 position gets messed up, i.e., it starts at an X1 position that is further to the right from the leftmost side of the PDF page (e.g., in this case, it will start at position 74 if it is center-aligned and at position 124 if it is right-aligned). Is there any way to change the alignment of column text while making it start from the correct X1 position at the same time?
 
The following is the code I am using to perform the above-mentioned task:
  public static void addColumnText(Document document, Hashtable htDetail, Hashtable htHeader, PdfContentByte cb)
  throws Exception
  {
    Font textFont = pdfUtils.getFont(htDetail, htHeader);
    String text = (String) htDetail.get("TEXT");
     int max_chars = 0;
    if (htDetail.containsKey("MAX_CHARS") && !stringUtils.isNull((String) htDetail.get("MAX_CHARS"))) {
      max_chars = Integer.parseInt((String) htDetail.get("MAX_CHARS"));
    }
    if (max_chars > 0 && max_chars < text.length())
      text = text.substring(0, max_chars);
    String style = null;
    if (htDetail.containsKey("STYLE") && !stringUtils.isNull((String) htDetail.get("STYLE"))) {
      style = (String) htDetail.get("STYLE");
      if (style.equals("B"))
        textFont.setStyle("bold");
      else if (style.equals("I"))
        textFont.setStyle("italic");
      else if (style.equals("U"))
        textFont.equals("underline");
    }
    if (!stringUtils.isNull(style))
      text = "<" + style + ">" + text + "</" + style + ">";
    htDetail.put("TEXT", text);
    text = pdfUtils.replaceTextWithEquivAsciiChar(text, htDetail);
    htDetail.put("TEXT", text);
    float pageHeight = document.getPageSize().height();
    ArrayList alStrings = new ArrayList();
    ArrayList alFontStyles = new ArrayList();
    Paragraph para = new Paragraph();
    stringUtils.parseHtml(text, alStrings, alFontStyles);
    for (int i = 0; i < alStrings.size(); i++) {
      String currStr = (String) alStrings.get(i);
      String currFontStyle = (String) alFontStyles.get(i);
      Font textFont2 = new Font(textFont.getBaseFont(), textFont.size());
      textFont2.setColor(textFont.color());
      textFont2.setStyle(currFontStyle);
      Chunk chunk = new Chunk(currStr);
      chunk.setFont(textFont2);
      para.add(chunk);
    }
    String justify = (String) htDetail.get("JUSTIFY");
    int justifyVal = 0;
    if (justify.equals("L"))
      justifyVal = Element.ALIGN_LEFT;
    else if (justify.equals("R"))
      justifyVal = Element.ALIGN_RIGHT;
    else {
      justifyVal = Element.ALIGN_CENTER;
    }
    float fFromX = Float.parseFloat((String) htDetail.get("X1"));
    float fToX = Float.parseFloat((String) htDetail.get("X2")) + textFont.size();
    float fFromY = pageHeight -
                   Float.parseFloat((String) htDetail.get("Y1")) - textFont.size() + para.leading();
    float fToY = pageHeight -
                   Float.parseFloat((String) htDetail.get("Y2")) - textFont.size() + para.leading();
    ColumnText ct = new ColumnText(cb);
    float[] right = {fFromX, fFromX};
    float[] left = {fToX, fToX};
    //ct.setAlignment(justifyVal);
    ct.setYLine(fToY);
    ct.addText(para);
    int status = 0;
    int column = 0;
    while((status & ColumnText.NO_MORE_TEXT) == 0) {
      ct.setSimpleColumn(right[column], fFromY,
      left[column], -fToY, para.leading(), justifyVal);
      status = ct.go();
      boolean checkForEndOfPage = false;
      if ((status & ColumnText.NO_MORE_COLUMN) != 0) {
        checkForEndOfPage = true;
        if (checkForEndOfPage)
        {
          break;
        }
      }
    }
  }
 
---------------------------------------------------------------------------------
 
Thanks,
 
Kalpana


Do you Yahoo!?
Free Pop-Up Blocker - Get it now

Reply via email to