PDF text centered in middle of page?

How do I center the text when creating text in a PDF?

Trying to create a test doc and all I know of is the x, y values for distance from top and left.

The text will be created dynamically based on user inputs so I will not know the width of the block of text.

Thanks

Try this

var sTitle as string
sTitle = "Service WorkOrder"
var dWidth as double = Document.Graphics.TextWidth(sTitle)

var TextLength as Integer = (Document.Graphics.Width / 2) - (dWidth / 2)
Document.Graphics.DrawText(sTitle, TextLength, 115)
1 Like

TextShape support Alignment but i don’t know if PDF support DrawObject now.

Thanks, I’ll be testing that first thing tomorrow morning.

Hi @Amy_Barnes

If you want to center a block of text, you can do it the same way @Gary_Smith told to you, except you need to do the maths with the wrapping value instead of the string width.

For example:

Const kLorem As String = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquid ex ea commodi consequat. Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint obcaecat cupiditat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."

    Var d As New PDFDocument
    Var g As Graphics = d.Graphics

    Var x As Double = (d.PageWidth/2) - 200 // 220 because we are going to wrapt the text at a 400 width.

    g.DrawText(kLorem,x,100,400)

    Var f As FolderItem = SpecialFolder.Desktop.Child("CenteredText.pdf")

    d.Save(f)
    f.Open

CenteredText

1 Like