PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

writing long string to several pages
https://forum.pdfsharp.net/viewtopic.php?f=2&t=4037
Page 1 of 1

Author:  Jessica [ Sun Nov 03, 2019 8:31 pm ]
Post subject:  writing long string to several pages

Hi,

In my program I have a very long string, that contains text. It can fill up 2 or more pages of a PDF file (I don't know how many pages).

I make a new page in my code like this:

Code:
page = document.AddPage();
gfx = XGraphics.FromPdfPage(page);


Then I write the long string to the page like this ("text" is the string that must be written):

Code:
XTextFormatter tf = new XTextFormatter(gfx);
tf.Alignment = XParagraphAlignment.Justify;
tf.DrawString(text, font, XBrushes.Black, new XRect(0 + 10, 0 + 10, page.Width - 20, page.Height - 20), XStringFormats.TopLeft);


The string is too long for 1 page so it doesn't fit. It's written until the current page is filled (part of the string is not written, because it's too long for the page)

Is there a way PDFsharp can add more pages automatically when a string is too long for 1 page?
Or how do I solve this problem?

Jessica

Author:  TH-Soft [ Sun Nov 03, 2019 10:52 pm ]
Post subject:  Re: writing long string to several pages

Jessica wrote:
Is there a way PDFsharp can add more pages automatically when a string is too long for 1 page?
Or how do I solve this problem?
MigraDoc can break strings across several pages. Consider using MigraDoc.

If you want to stick to PDFsharp:
There is an improved version of XTextFormatter that returns how much text was written. If the text was truncated, just try to write the remaining text on the next page until no truncation occurs.
See also:
viewtopic.php?f=8&t=3192

Author:  Jessica [ Sun Nov 03, 2019 11:23 pm ]
Post subject:  Re: writing long string to several pages

Hi

I want to use MigraDoc. Can you give a small code example including the method to use for writing a string across several pages automatically? I'm a beginner and don't know what to use.

Thanks

Author:  Thomas Hoevel [ Mon Nov 04, 2019 9:44 am ]
Post subject:  Re: writing long string to several pages

Hi!
Jessica wrote:
Can you give a small code example including the method to use for writing a string across several pages automatically?
A string passed in a call to "AddParagraph(text)" will break across several lines and pages if needed.
"AddFormattedText" is the advanced version used in this sample:
http://www.pdfsharp.net/wiki/MigraDocHe ... ample.ashx

See also:
http://www.pdfsharp.net/wiki/HelloMigraDoc-sample.ashx

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/