How to Export TextArea to PDFDocument?

I have a TextArea that I want to export to a PDFDocument. If I follow the Xojo documentation I use the following code:

Var tempPDFDocument As PDFDocument = New PDFDocument(PDFDocument.PageSizes.A4)
Var tempGraphics As Graphics = tempPDFDocument.Graphics

tempGraphics.DrawText(myTextEditor.Text, 20, 20, tempPDFDocument.PageWidth - 40)
tempPDFDocument.Save(f)

There are two thing this cannot do:

  1. Print the formatted text
  2. Print more than one page

So, I tried to use the code to print to the current printer (see below), but it crashes inside the While loop.

Var tempStyledTextPrinter As StyledTextPrinter = myTextEditor.StyledTextPrinter(tempGraphics, tempPDFDocument.PageWidth)

While Not tempStyledTextPrinter.EOF
  tempStyledTextPrinter.DrawBlock(0, 0, tempPDFDocument.PageHeight) 'crashes here
  
  tempGraphics.NextPage
Wend

How can I export a styled, multipage TextArea to a PDFDocument?

Currently PDFDocument doesn’t support StyledParagraphs; so probably you will need to handle them manually setting the font, style and color for each one when adding the text to the PDF document.

Maybe you want add a Feature Request for this to happen? :wink:

https://tracker.xojo.com/xojoinc/xojo/-/issues/72187

Do you have a solution for the MultiPage printing?

Right now… everything needs to be done manually, sorry :pensive: (that means, taking care of the Height of every added line/paragraph). I hope it will be added soon to PDFDocument so everything this can be handled automatically. :+1:

1 Like

For our DynaPDF plug-in, we got a WriteStyledText function, which translates the styles to write it to the PDF.
You may use that for the time being :smiley:

1 Like

Thanks for the suggestion Christian.