How do I add self repeating text all around the border (margins) of a PDF page?

Q: I would like to add some self repeating text all around the border
(margins) of the page. For instance I wish to add the following text:
" - - - >DOCPATH>" around the
borders continously. So, if on the top border I can place the text up
untill the DOCTITLE, say for instance, 15th character, I would go on
and start from the 16th character on the Right margin (vertically
disposed text) etc.


A: The following is some sample code to add self repeating text all
around the border (margins) of the page…
The code is not completely general (for cases where the page is
rotated or cropped) but it can be used as a starting point for your
project.

using System;
using pdftron;
using pdftron.Common;
using pdftron.Filters;
using pdftron.SDF;
using pdftron.PDF;

namespace TestCS
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
PDFNet.Initialize();
try
{
PDFDoc doc = new PDFDoc();
ElementBuilder b = new ElementBuilder(); // ElementBuilder is used
to build new Element objects
ElementWriter w = new ElementWriter(); // ElementWriter is used to
write Elements to the page

Page page = doc.PageCreate();
doc.PagePushBack(page); // Add page to document’s page sequence.

w.Begin(page); // begin writing to this page

string txt = "- - - -
";

// Draw horizontal text (left → right)

float font_size = 12;
System.Drawing.Font sys_font = new System.Drawing.Font(“Comic Sans
MS”, font_size);
Font font = Font.CreateTrueTypeFont(doc, sys_font, true, true);
Element element = b.CreateTextBegin(font, font_size);
w.WriteElement(element);

element.SetTextMatrix(1, 0, 0, 1, 0, page.GetPageHeight() -
font_size);

Rect box = new Rect();
double max = page.GetPageWidth() - font_size;
int i = 0;
while (true)
{
if (i >= txt.Length) i = 0;

element = b.CreateTextRun(new String(txt[i++], 1));
element.GetBBox(box);
if (box.x2 < max) w.WriteElement(element);
else break;
}
w.WriteElement(b.CreateTextEnd());

// Draw vertical text (top → down)

element = b.CreateTextBegin(font, font_size);
double deg2rad = 3.1415926535 / 180.0;

Matrix2D mtx = Matrix2D.RotationMatrix(90 * deg2rad);
mtx.Translate(page.GetPageWidth() - font_size, page.GetPageHeight
() - font_size);
element.SetTextMatrix(mtx);
w.WriteElement(element);

while (true)
{
if (i >= txt.Length) i = 0;

element = b.CreateTextRun(new String(txt[i++], 1));
element.GetBBox(box);
if (box.y1 > font_size) w.WriteElement(element);
else break;
}

w.WriteElement(b.CreateTextEnd());

// Draw horizontal text (right → left)

element = b.CreateTextBegin(font, font_size);

mtx = Matrix2D.RotationMatrix(180 * deg2rad);
mtx.Translate(page.GetPageWidth() - font_size, font_size);
element.SetTextMatrix(mtx);
w.WriteElement(element);

while (true)
{
if (i >= txt.Length) i = 0;

element = b.CreateTextRun(new String(txt[i++], 1));
element.GetBBox(box);
if (box.x1 > font_size) w.WriteElement(element);
else break;
}

w.WriteElement(b.CreateTextEnd());

// Draw vertical text (bottom → top)

element = b.CreateTextBegin(font, font_size);

mtx = Matrix2D.RotationMatrix(-90 * deg2rad);
mtx.Translate(font_size, font_size);
element.SetTextMatrix(mtx);
w.WriteElement(element);

while (true)
{
if (i >= txt.Length) i = 0;

element = b.CreateTextRun(new String(txt[i++], 1));
element.GetBBox(box);
if (box.y2 < page.GetPageHeight() - font_size) w.WriteElement
(element);
else break;
}

w.WriteElement(b.CreateTextEnd());

w.End(); // save changes to the current page

w.Dispose();
b.Dispose();

doc.Save("…/…/…/…/TestFiles/Output/out.pdf",
SDFDoc.SaveOptions.e_linearized);
doc.Close();
}
catch (PDFNetException e)
{
Console.WriteLine(e.Message);
}
}
}
}