PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sun Jul 14, 2024 10:49 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Thu Feb 27, 2014 10:39 am 
Offline

Joined: Thu Feb 27, 2014 10:26 am
Posts: 4
I have a box which is 10cm by 10cm in which I am putting a string of text.

I am using the formatter to word-wrap, which works perfect.

However, when I have a lot of text, it seems to ignore the height of the rectangle and spills out below it.

Is there a way to stop this?

Anything which does not fit in the box, should simply not be visible on the PDF.


Top
 Profile  
Reply with quote  
PostPosted: Thu Feb 27, 2014 10:54 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3101
Location: Cologne, Germany
I presume the question is about PDFsharp.

How do you wrap the text? I presume it's XTextFormatter.

If you use XTextFormatter: copy the source code to your project and make the class stop adding lines when the allowed space was already filled.

XTextFormatter is just a quick shot to get you started, it's not the ultimate formatter.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Thu Feb 27, 2014 2:36 pm 
Offline

Joined: Thu Feb 27, 2014 10:26 am
Posts: 4
I've made the changes and it works a treat, thank you.
For the benefit of anyone wanting to make a similar change, I altered the CreateLayout method; removing the following lines of code:
Code:
if (y > rectHeight)
{
   block.Stop = true;
   break;
}

And following the if (block.Type == BlockType.LineBreak) then..else (in the same method), I first added the following, which met the requirement that I initially had, to ensure that the text did not flow beyond the boundary of the rectangle.
Code:
if (y < rectHeight) continue;
block.Stop = true;
break;

I have since extended this a little further, so that in any instance where the text "would" have overflowed the rectangle, it will not allow the overflow and will append "[...]" in place of the last word, on the last line...
Code:
if (y < rectHeight) continue;
if (idx < count)
{
    int lastidx = idx;
    while (lastidx > 0 && this.blocks[lastidx].Type == BlockType.LineBreak)
    {
        lastidx--;
    }
    this.blocks[lastidx].Text = "[...]";
}
block.Stop = true;
break;

Note that I replaced the last word, rather than appending to it, because I wanted to avoid making the text potentially overflow the right edge of the rectangle, and don't have the time right now to work out how to ensure that this doesn't ever occur


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: Baidu [Spider], Google [Bot] and 15 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group