Create a new PDF and position some text C#

Hi,

I am using the PDFTron.NET.x64 in C#.

The part that I am not understanding is how exactly does the positioning system work.

I am looking that the samples and cannot work out how to position text.

Ok if I could get clarity on.

  1. How to make my page A4.
  2. How to then put some text with a specific font in the top left corner.
  3. Then put some text below that etc.

I am not sure if everything needs to be enclosed in a rectange etc.

If I could get some starters.

BTW. To save some time I have already got the SDK installed and running and can add some text to page but am stuck here.

Thanks again,

Ward

Hello, I’m Ron, an automated tech support bot :robot:

While you wait for one of our customer support representatives to get back to you, please check out some of these documentation pages:

Guides:APIs:Forums:

Hi Ward,

  1. There are 72 PPI (Points Per Inch). This means that a standard A4 page would be 595 x 842 points.

  2. To properly position your text you must give the coordinates of were you would like to place the text. You can set the coordinates using the SetTextMatrix method.
    Please see the following links to get further information on how the coordinates work:
    PDFTron Systems Inc. | Documentation
    Class ElementWriter

  3. To put text below that, you can simply use the SetTextMatrix to re-position your text to another set of coordinates. You can also look for the method CreateTextNewLine().
    You can take a look at the following sample for some guidance: PDFTron Systems Inc. | Documentation

Alternatively, you can use higher level API stamper class to create the new line. Please see the following sample for more information: PDFTron Systems Inc. | Documentation

Finally, if you need reflow, many users use html or docx to pdf conversion.

For further help, here is some code that will create an A4 page and print “Hello World!” to the top left corner and then prints “Second Line” on the next line.
using (PDFDoc doc = new PDFDoc())
using (ElementBuilder eb = new ElementBuilder()) // ElementBuilder is used to build new Element objects
using (ElementWriter writer = new ElementWriter()) // ElementWriter is used to write Elements to the page
{
// Start a new page ------------------------------------
// Position an image stream on several places on the page
// A4 dimensions
int w = 595;
int h = 842;
Page page = doc.PageCreate(new Rect(0,0,w,h));

writer.Begin(page, ElementWriter.WriteMode.e_overlay, true);    // begin writing to this page

// Begin writing a block of text
Element element = eb.CreateTextBegin(Font.Create(doc, Font.StandardType1Font.e_times_roman), 24);
writer.WriteElement(element);

string data = "Hello World!";
element = eb.CreateTextRun(data);
element.SetTextMatrix(1, 0, 0, 1, 0, h - 28);
writer.WriteElement(element);
writer.WriteElement(eb.CreateTextNewLine())
element = eb.CreateTextRun("Second Line");
writer.WriteElement(element);

writer.WriteElement(eb.CreateTextEnd());

writer.End();  // save changes to the current page
doc.PagePushBack(page);

// End page ------------------------------------

doc.Save(output_path + "element_builder_03.pdf", SDFDoc.SaveOptions.e_remove_unused);
Console.WriteLine("Done. Result saved in element_builder.pdf...");

}