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

Right Aligned Paragraphs in text frames overflowing borders
https://forum.pdfsharp.net/viewtopic.php?f=3&t=3024
Page 1 of 1

Author:  AdamDiment [ Thu Jan 08, 2015 6:05 pm ]
Post subject:  Right Aligned Paragraphs in text frames overflowing borders

I'm using the latest non pre release version of PDFSharp / Migradoc, downloaded via nuget into Visual Studio 2013 community edition

A weird one - The problem is shown in the screenshots, but fundamentally is this:

When I have several lines of text in a paragraph (lines are separated by line breaks) that is right aligned, the text on each line seems to overflow the border of the paragraph by a factor of half a horizontal character width per space character in the string on that line.

The problem does not occur for left aligned paragraphs with line breaks, only right aligned.

I.E a right aligned paragraph containing three lines like this

One
One Two
One Two Three

Will be perfectly aligned to the paragraph border on the first line, out by half a character width on the second line, and a whole character with on the third line, etc!

Can you tell me if I'm doing something fundamentally wrong here or if this is a bug? I would like the right aligned text to perfectly align.

I have a workaround of adding spaces to every string so that each string has an equal number of space characters, but this is not ideal obviously!

It doesn't seem to be a problem on left aligned paragraphs.

Thanks for any help you can give.

Here's a snippet of my code to give you an idea - I've included with the workaround and without, and also the styles and frames I've defined at the bottom, just in case there's an issue there (although removing the styles and just calling AddText() the issue still remained for me).

Code:

without workaround (I have made the data anonymous)
Code:
            var paragraph = _businessAddressFrame.AddParagraph();
            paragraph.Format.Borders.Style = BorderStyle.Dot;
            paragraph.Format.Alignment = ParagraphAlignment.Right;
            paragraph.Style = "AddressHeader";
            paragraph.AddLineBreak();
            paragraph.AddFormattedText("A fake Address Header", "AddressHeader");
            paragraph.AddLineBreak();
            paragraph.AddFormattedText("1st Floor", "AddressNormal");
            paragraph.AddLineBreak();
            paragraph.AddFormattedText("Fake House", "AddressNormal");
            paragraph.AddLineBreak();
            paragraph.AddFormattedText("11-13 FakeStreet", "AddressNormal");
            paragraph.AddLineBreak();
            paragraph.AddFormattedText("Fake Town", "AddressNormal");
            paragraph.AddLineBreak();
            paragraph.AddFormattedText("XXXX XXX", "AddressNormal");
            paragraph.AddLineBreak();
            paragraph.AddFormattedText("Telephone", "AddressHeader");
            paragraph.AddLineBreak();
            paragraph.AddFormattedText("00000 555555", "AddressNormal");
            paragraph.AddLineBreak();
            paragraph.AddFormattedText("Facsimile", "AddressHeader");
            paragraph.AddLineBreak();
            paragraph.AddFormattedText("00000 777777", "AddressNormal");
            paragraph.AddLineBreak();
            paragraph.AddFormattedText("Email", "AddressHeader");
            paragraph.AddLineBreak();
            paragraph.AddFormattedText("info@fakeemailaddress.co.uk", "AddressNormal");

with workaround (I have made the data anonymous)
Code:
            var paragraph = _businessAddressFrame.AddParagraph();
            paragraph.Format.Borders.Style = BorderStyle.Dot;
            paragraph.Format.Alignment = ParagraphAlignment.Right;
            paragraph.Style = "AddressHeader";
            paragraph.AddLineBreak();
            paragraph.AddFormattedText("A fake Address Header", "AddressHeader");
            paragraph.AddLineBreak(); paragraph.AddSpace(2);
            paragraph.AddFormattedText("1st Floor", "AddressNormal");
            paragraph.AddLineBreak(); paragraph.AddSpace(2);
            paragraph.AddFormattedText("Fake House", "AddressNormal");
            paragraph.AddLineBreak(); paragraph.AddSpace(2);
            paragraph.AddFormattedText("11-13 FakeStreet", "AddressNormal");
            paragraph.AddLineBreak(); paragraph.AddSpace(2);
            paragraph.AddFormattedText("Fake Town", "AddressNormal");
            paragraph.AddLineBreak(); paragraph.AddSpace(2);
            paragraph.AddFormattedText("XXXX XXX", "AddressNormal");
            paragraph.AddLineBreak(); paragraph.AddSpace(3);
            paragraph.AddFormattedText("Telephone", "AddressHeader");
            paragraph.AddLineBreak(); paragraph.AddSpace(2);
            paragraph.AddFormattedText("00000 555555", "AddressNormal");
            paragraph.AddLineBreak(); paragraph.AddSpace(3);
            paragraph.AddFormattedText("Facsimile", "AddressHeader");
            paragraph.AddLineBreak(); paragraph.AddSpace(2);
            paragraph.AddFormattedText("00000 777777", "AddressNormal");
            paragraph.AddLineBreak(); paragraph.AddSpace(3);
            paragraph.AddFormattedText("Email", "AddressHeader");
            paragraph.AddLineBreak(); paragraph.AddSpace(3);
            paragraph.AddFormattedText("info@fakeemailaddress.co.uk", "AddressNormal");

The _businessAddressFrame is created like this, so seems to have plenty of width:

Code:
        private void CreateBusinessAddressFrame(Section section)
        {
            _businessAddressFrame = section.AddTextFrame();
            _businessAddressFrame.Height = "5.5cm";
            _businessAddressFrame.Width = "8cm";
            _businessAddressFrame.RelativeHorizontal = RelativeHorizontal.Margin;
            _businessAddressFrame.RelativeVertical = RelativeVertical.Margin;
            _businessAddressFrame.Top = "2.0cm";
            _businessAddressFrame.MarginRight = "0.0cm";
            _businessAddressFrame.Left = ShapePosition.Right;
        }

and the formatted text styles are like this

Code:
            //Create a new style called address header based on normal
            style = _document.Styles.AddStyle("AddressHeader", "Normal");
            style.Font.Color = new Color(14, 118, 188);
            style.Font.Bold = true;

            //Create a new style called address header based on normal
            style = _document.Styles.AddStyle("AddressNormal", "Normal");
            style.Font.Color = Colors.Black;


Attachments:
File comment: This is a temp fix I've managed, adding extra spaces to make sure each line has an equal number of spaces
With Workaround (aligned but still overflowing).PNG
With Workaround (aligned but still overflowing).PNG [ 34 KiB | Viewed 3691 times ]
File comment: This shows the problem I'm having
Illustrating the problem.PNG
Illustrating the problem.PNG [ 33.77 KiB | Viewed 3691 times ]

Author:  Thomas Hoevel [ Thu Jul 02, 2015 9:26 am ]
Post subject:  Re: Right Aligned Paragraphs in text frames overflowing bord

Hi!

I'm sorry for the late reply, but your question must have slipped through.

Today I tried your code with MigraDoc 1.32, both GDI+ and WPF builds. I couldn't not replicate the problem.
So maybe the cause of the problem is with something you do not show here.

Please provide an SSCCE for further examination.

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