How can I add a text with a border around to an existing page?

Q:

I tried to add a text to an existing pdf page. But I get an exception
when I added a byte buffer to the element. The code I use is as
follows:

PDFDoc doc = new PDFDoc(documentInfo.InputDocument);
if (doc.GetPagesCount() > 0) {
  ElementWriter writer = new ElementWriter();
  ElementBuilder elementBuilder = new ElementBuilder();
  Element element = elementBuilder.CreateTextBegin();
  byte[] buffer = new byte[stamp.Value.Length];
  char[] stringBuffer = stamp.Value.ToCharArray();
  for (int i = 0; i < stamp.Value.Length; i++) {
    buffer[i] = (byte)stringBuffer[i];
  }
  buffer[0] = 23;
  element.SetTextData(buffer, stamp.Value.Length);
  writer.Begin(doc.PageFind(0).Current());
  writer.WriteElement(element);
  writer.End();
  doc.Save(documentInfo.OutputFile, Doc.SaveOptions.e_remove_unused);
  doc.Close();
}
else {
  System.Windows.Forms.MessageBox.Show("No page found in document");
}

I think I did something wrong. How can I add a text with a border
around to an existing page?
----
A:
The problem is that you are trying to write text as part of
e_text_begin element, however the new text can be written only as part
of e_text element which must be nested between e_text_begin and
e_text_end element.

The following is a basic example of how to stamp an existing page with
new text:

PDFDoc doc = new PDFDoc("in.pdf");
doc.InitSecurityHandler();

ElementBuilder eb = new ElementBuilder();
ElementWriter writer = new ElementWriter();

Page pg=doc.PageFind(1).Current();
writer.Begin(pg);
Element element = eb.CreateTextBegin(
   Font.Create(doc, Font.StandardType1Font.e_times_roman),64);
writer.WriteElement(element);

element = eb.CreateTextRun("Hello World!");

// Position the text run (the last two parameters in the
transformation matrix
// represent horizontal and vertical offset where the text will be
placed).
element.SetTextMatrix(1, 0, 0, 1, 100, 20);
writer.WriteElement(element);

writer.WriteElement(eb.CreateTextEnd()); // e_text_end
writer.End();
doc.Save("out.pdf", 0);
doc.Close();

You can find more information on how to stamp text in the following
FAQ:
  http://www.pdftron.com/net/faq.html#how_watermark

Also you may want to search PDFNet SDK Knowledge Base (http://
groups.google.com/group/pdfnet-sdk/topics) using the following search
query: "How can I stamp all PDF pages with a rotated and centered text
stamp"

To draw a border around the text you would need to create a new path
element. For example:

Rect bbox = new Rect();

// Write text
Element element = eb.CreateTextBegin(...);
writer.WriteElement(element);

element = eb.CreateTextRun("Hello World!");
...
element.GetBBox(bbox)
writer.WriteElement(element);
...
writer.WriteElement(eb.CreateTextEnd());

// Draw a border around the text

element = eb.CreateRect(); // create a path element
// stroke, but don't fill the rectangle
element.SetPathFill(false);
element.SetPathStroke(true);
element.GetGState().SetStrokeColorSpace(ColorSpace.CreateDeviceRGB());
element.GetGState().SetStrokeColor(new ColorPt(1, 0, 0)); // red
element.GetGState().SetLineWidth(3); // 3 point stroke width
writer.WritePlacedElement(element);