Q: I'm trying to use function below. Using this one, I can get the
"page number" located in center on the page. Do you know how I can
change the location of the text on the page? (I try to locate "page
number" upper left corner for this.)
private void Stamp(PDFDoc pdfdoc, string text, double font_sz, ColorPt
fill_color, double rot_angle) {
ElementBuilder eb = new ElementBuilder();
ElementWriter writer = new ElementWriter();
pdftron.PDF.Font myFont = pdftron.PDF.Font.Create(pdfdoc,
pdftron.PDF.Font.StandardType1Font.e_times_roman);
pdftron.PDF.ColorSpace fill_cs = ColorSpace.CreateDeviceRGB
();
double deg2rad = 3.1415926535 / 180.0;
Matrix2D rot_mtx = Matrix2D.RotationMatrix(rot_angle *
deg2rad);
rot_mtx.Scale(1, 1);
rot_mtx.Set(1, 1, 1, 1, 1, 1);
PageIterator itr = pdfdoc.PageBegin();
PageIterator end = pdfdoc.PageEnd();
int PageCount = 0;
for (; itr != end; itr.Next()) {
PageCount = PageCount + 1;
Page page = itr.Current();
writer.Begin(page, false);
Element element = eb.CreateTextBegin(myFont, font_sz);
writer.WriteElement(element);
element = eb.CreateTextRun(text + Convert.ToString
(PageCount));
GState gs = element.GetGState();
gs.SetFillColorSpace(fill_cs);
gs.SetFillColor(fill_color);
// Position the text run
Matrix2D mtx = new Matrix2D(rot_mtx);
// scale the stamp relative to standard 'letter'
page.
double scale_factor = page.GetPageWidth() / 612.0;
mtx.Scale(scale_factor, scale_factor);
mtx.Translate((page.GetPageWidth() -
element.GetTextLength() * Math.Cos(rot_angle *deg2rad)) / 2,
(page.GetPageHeight() + element.GetTextLength() * Math.Sin(rot_angle
*deg2rad)) / 2);
element.SetTextMatrix(mtx);
writer.WriteElement(element);
writer.WriteElement(eb.CreateTextEnd());
writer.End();
}
}
}
-----
A: I can update the line with mtx.Translate(). Something along the
following lines:
For upper-left corner:
mtx.Translate( 10, page.GetPageHeigh() - font_sz - 10);
For lower-right corner:
mtx.Translate(page.GetPageWidth() - element.GetTextLength() - 10,
font_sz + 10);