PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sun Apr 28, 2024 2:48 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Fri Mar 12, 2010 11:19 am 
Offline
Supporter

Joined: Fri May 15, 2009 3:28 pm
Posts: 96
Hi,

I have a function to trim a string to a given rectangle - i have done this by creating a Document object, adding a paragraph object to it and then recursively adding text to it, when it goes onto the 2nd page, it knows to stop as the text is bigger than the rectangle) this has worked fine for me up until now...

I have a piece of text with two line breaks:

1.Schedule meeting with the 2 Chief Delivery Officers (CDO).
2. Schedule meeting with the CEO.
3. Authorization for 1 or 2 days up front consulting work.

which basically looks like this (in the code):
1.Schedule meeting with the 2 Chief Delivery Officers (CDO).\r2. Schedule meeting with the CEO.\r3. Authorization for 1 or 2 days up front consulting work.

as i am trimming this text to two lines, the "page break" which means the rectangle has been filled happens on the 2nd "\r" next to "the CEO." this page break is being put onto the next page and will eventually be trimmed off, but when i try to do PrepareDocument(), it crashes out and wont even step into my Catch statement! I have tried using "\n" instead of the "\r" to create the break and also used paragraph.AddLineBreak() and both cause it to crash out. this is a very specific problem, but surely its a bug in MigraDoc?

I am using 1.2 of the framework and unfortunately as this code has already been released to the world, i cant really upgrade as it may cause issues to arise.

any help would be appreciated.

Here is the function:

Code:
public static string TrimTextToXLines(string txt, double width, int maxLines, Font font)
        {
            try
            {
                //txt = txt.Replace("\r", "\n");
                string _currText = "";
                Document _doc = new Document();
                MigraDoc.Rendering.DocumentRenderer _renderer = new MigraDoc.Rendering.DocumentRenderer(_doc);
                for (int i = 0; i < txt.Length; i++)
                {
                    if (txt[i].ToString() == "\n")
                    {
                        txt = txt;
                    }
                    try
                    {
                        Paragraph _para = null;
                        try
                        {
                            _doc.Sections.Clear();
                            _doc.AddSection();
                            _doc.LastSection.PageSetup.PageWidth = width;
                            _doc.LastSection.PageSetup.LeftMargin = _doc.LastSection.PageSetup.RightMargin = _doc.LastSection.PageSetup.TopMargin = _doc.LastSection.PageSetup.BottomMargin = 0;
                            _doc.LastSection.PageSetup.PageHeight = maxLines * (font.Size + 5);
                            _para = _doc.LastSection.AddParagraph();
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                        try
                        {
                            string _txt = txt.Substring(0, i);
                            _para.AddFormattedText(_txt, font);
                            if(_txt.Contains("\r"))
                            {
                                System.Text.RegularExpressions.Regex _regex = new Regex("\r");
                                for(int f = 0; f < _regex.Matches(_txt).Count; f++)
                                    _para.AddLineBreak();
                            }
                        }
                        catch(Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                        try
                        {
                            string _currChar = txt[txt.Length - 1].ToString();
                            Console.WriteLine("Char: '" + _currChar + "'");
                            Console.WriteLine("Char in string: '" + txt[txt.Length - 1] + "'");
                            _renderer.PrepareDocument();
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                        if (_renderer.FormattedDocument.PageCount > 1)
                        {
                            break;
                        }
                        try
                        {
                            _currText = txt.Substring(0, i);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                        if (i == txt.Length - 1)
                        {
                            _currText = txt;
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
                _doc.Sections.Clear();
                return _currText;
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
                return "";
            }
        }


It is being passed these params:
txt = 1.Schedule meeting with the 2 Chief Delivery Officers (CDO).\r2. Schedule meeting with the CEO.\r3. Authorization for 1 or 2 days up front consulting work.
width = 352.84168601036072
font = (Arial, regular, size 8)

thanks,

Mike


Top
 Profile  
Reply with quote  
PostPosted: Fri Mar 12, 2010 12:42 pm 
Offline
Supporter

Joined: Fri May 15, 2009 3:28 pm
Posts: 96
ok i solved the problem by hacking my code.

Basically instead of a line break, i added a new paragraph object with FormatText(" ", font) where font is the same as the one used in the main paragraph.


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

All times are UTC


Who is online

Users browsing this forum: Bing [Bot] and 194 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