Set the position of a text or an image in a pdf using itextsharp(C#/asp.net)
If you tried img.SetAbsolutePosition(10000f,10000f);
then your image is way out of the visible area of the PDF. You are creating your Document
like this:
Document document = new Document(PageSize.A4, 188f, 88f, 5f, 10f);
This means that the size of the page is 595 x 842 user units. Using x = 10000
and y = 10000
doesn't fit inside a rectangle of 595 x 842.
Please try:
img.SetAbsolutePosition(0,0);
When you use these coordinates, the lower-left corner of the image will coincide with the lower-left corner of the page.
Please consult the official iText documentation and search for coordinate system. See for instance:
- How should I interpret the coordinates of a rectangle in PDF?
- Where is the origin (x,y) of a PDF page?
- ...
This will help you find how to define the coordinates for the SetAbsolutePosition()
method.
Update:
You are also asking about adding text at absolute positions. Here we have to make the distinction between a single line of text and a block of text. See also the section Absolute positioning of text on the official web site.
A single line of text:
See for instance How to position text relative to page? and you'll find the showTextAligned()
method:
ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
new Phrase("Some text"), 100, 100, 0);
Please make sure that you read other examples to so that you discover what the canvas
object is about.
A block of text:
Take a look at How to add text inside a rectangle?
ColumnText ct = new ColumnText(cb);
ct.SetSimpleColumn(rect);
ct.AddElement(new Paragraph("This is the text added in the rectangle"));
ct.Go();
Please take a look at the full example to find out what cb
and rect
are about.