How do I wrap PDF text using ElementWriter & ElementBuilder?

Q:
Thanks you very much for all your help. We are almost there, finally we
want to wrap the text in the cell if it exceeds the table cell width.
Can you please suggest how can we do this??
---
A:

For an example of how to wrap text you may want to take a look at one
of the code snippets in ElementBuilder sample project
(http://www.pdftron.com/net/samplecode.html#ElementBuilder):

For example:

// Begin writing a block of text
System.Drawing.Font sys_font = new System.Drawing.Font("Comic Sans MS",
12);
Font my_font = Font.CreateTrueTypeFont(doc, sys_font, true, true);
element = eb.CreateTextBegin(my_font, 12);
element.SetTextMatrix(1.5, 0, 0, 1.5, 50, 600);
element.GetGState().SetLeading(15); // Set the spacing between lines
writer.WriteElement(element);

string para = "A PDF text object consists of operators that can show "
+
"text strings, move the text position, and set text state and certain "
+
"other parameters. In addition, there are three parameters that are " +
"defined only within a text object and do not persist from one text " +
"object to the next: Tm, the text matrix, Tlm, the text line matrix, "
+
"Trm, the text rendering matrix, actually just an intermediate result "
+
"that combines the effects of text state parameters, the text matrix "
+
"(Tm), and the current transformation matrix";

int para_end = para.Length;
int text_run = 0;
int text_run_end;

double para_width = 300; // paragraph/column width is 300 units
double cur_width = 0;

while (text_run < para_end) {
  text_run_end = para.IndexOf(' ', text_run);
  if (text_run_end < 0)
    text_run_end = para_end - 1;

  string text = para.Substring(text_run, text_run_end-text_run+1);
  element = eb.CreateTextRun(text);
  if (cur_width + element.GetTextLength() < para_width) {
    writer.WriteElement(element);
    cur_width += element.GetTextLength();
  }
  else {
    writer.WriteElement(eb.CreateTextNewLine()); // New line
    text = para.Substring(text_run, text_run_end-text_run+1);
    element = eb.CreateTextRun(text);
    cur_width = element.GetTextLength();
    writer.WriteElement(element);
  }

  text_run = text_run_end+1;
}

// Finish the block of text
writer.WriteElement(eb.CreateTextEnd());

------------

Also, attached is another code snippet (written in C++) that
illustrartes how to draw multi-line text (i.e. paragraph).

----
C++ code sample:

void DrawTextColumnRun(Element* e,
             double col_width,
             Field::TextJustification just,
             const Matrix2D& mtx,
             ElementWriter* writer)
{
  if (writer)
  {
    Matrix2D txt_mtx(mtx);
    double horiz_adjust = 0;
    if (just == Field::e_centered) {
      horiz_adjust = (col_width - e->GetTextLength())/2;
    }
    else if (just == Field::e_right_justified) {
      horiz_adjust = col_width - e->GetTextLength();
    }

    if (horiz_adjust) {
      txt_mtx.Translate(horiz_adjust, 0);
    }

    e->SetTextMatrix(txt_mtx);
    writer->WriteElement(e);
  }
}

/**
* @return the number of text lines in the column
*/
int DrawTextColumn(const UChar* text_buf, int text_buf_sz,
           double col_width,
           Field::TextJustification just,
           Matrix2D& mtx,
           double leading,
           ElementBuilder& eb,
           ElementWriter* writer) // note: writer may be NULL
{
  Element* e;
  int num_lines = 0;
  const UChar *ti = text_buf, *run_start = text_buf;
  const UChar *tend = text_buf + text_buf_sz;
  const UChar *run_end = 0;
  double cur_width = 0;
  double text_run_len;
  while (ti<tend)
  {
    if (*ti == '\n' || *ti == 0x0D || *ti == 0x0A) // New line
    {
      if (cur_width>0) // Flush the collected text run, if any
      {
        e = eb.CreateTextRun(run_start, run_end-run_start);
        DrawTextColumnRun(e, col_width, just, mtx, writer);
      }

      ++num_lines;
      ++ti;
      run_start = ti; // start tracking the new run.
      mtx.Translate(0, -leading);
      cur_width = 0;
    }
    else
    {
      run_end = detail::FindTRunEnd(ti, tend);
      e = eb.CreateTextRun(ti, run_end-ti);
      text_run_len = e->GetTextLength();

      if (cur_width + text_run_len > col_width) // New line.
      {
        // Flush the collected text run, if any
        if (cur_width > 0)
        {
          e = eb.CreateTextRun(run_start, ti-run_start);
          DrawTextColumnRun(e, col_width, just, mtx, writer);
          run_start = ti;
          cur_width = text_run_len;
        }
        else // the new run fits the width of the entire colum, so write
it. Note that text runs longer than width are not broken like in
Acrobat
        {
          DrawTextColumnRun(e, col_width, just, mtx, writer);
          run_start = run_end;
          cur_width = 0;
        }

        mtx.Translate(0, -leading);
        ++num_lines;
      }
      else {
        cur_width += text_run_len;
      }

      ti = run_end;
    }
  }

  if (cur_width>0) // Flush the remaining collected text run, if any
  {
    e = eb.CreateTextRun(run_start, run_end-run_start);
    DrawTextColumnRun(e, col_width, just, mtx, writer);
    ++num_lines;
  }

  return num_lines;
}