简体   繁体   中英

MigraDOC and pdfSharp, center dynamic text

i was looking for a solution to create a document (PDF) in such why that i can calculate an input field: The PDF content should look something like this

(all pdf content needs to be centered)

Header


Field1 : not dynamic (needs to be centered)


Field2 : UserName (dynamic- needs to be appended in the center of the paragraph) - since each user has a different name length

So my questions is , does pdfSharp or migraDoc has a method or something that can align the text to center (meaning that it does some calculates - determine the font-family, font-size and does the magic so that in the end the marked text is centered) ? If so what is the method since i've searched the migraDoc and pdfSharp documentation and could not find anything like that.

And if such method does not exist, did someone try this? worked with it? has any suggestions how can i achieve this behavior? maybe some source to look from. Thank you

Sample 1 shows with a lot of code example how to create a pdf and use almost all functionality that Migradoc offers. Sample 2 shows in detail how to create tables, which might also be interesting for you in respect to layout the page's content.

The alignment (center/left/right) is usually done by setting the Format.Alignment property like this:

Paragraph par = new Paragraph();

par.Format.Alignment = ParagraphAlignment.Center;

A short version of a document with centered content would be:

// first you need a document
Document MigraDokument  = new Document();

// each document needs at least one section
Section section = MigraDokument.AddSection();
section.PageSetup.PageFormat = PageFormat.A4;

// and then you add paragraphs to the section
Paragraph par = section.AddParagraph();
// and set the alignment as you wish
par.Format.Alignment = ParagraphAlignment.Center;

// now just fill it with content and set the rest of the parameters...
par.AddText("text");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM