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

MigraDoc horizontal line divider
https://forum.pdfsharp.net/viewtopic.php?f=2&t=1101
Page 1 of 1

Author:  Pierre [ Fri Mar 19, 2010 3:12 pm ]
Post subject:  MigraDoc horizontal line divider

Hello,
I'm just starting with PDFSharp & MigraDoc and like to know if it's possible to insert some kind of horizontal line separator. I want to display something in header and then separate it visualy form the content. Anyway, it might be generally useful drawing horizontal separator even in content...
Thanks.

Author:  Thomas Hoevel [ Mon Mar 22, 2010 10:26 am ]
Post subject:  Re: MigraDoc horizontal line divider

Pierre wrote:
Anyway, it might be generally useful drawing horizontal separator even in content...Thanks.

Yep.

Paragraphs have borders.
If you only set bottom and/or top borders, you'll get horizontal lines.

Code:
paragraph.Format.Borders.Bottom


Tables can also be used.

Author:  jeffhare [ Thu Jun 24, 2010 5:01 pm ]
Post subject:  Re: MigraDoc horizontal line divider

Hello,
It's pretty simple to create horizontal rules. The main idea is that you don't add any text to the paragraph, just style the paragraph and set a font size to control the rule thickness and shading/border colors to control the appearance.

However, if you don't want control over the border color, you can dispense with defining the borders entirely.
I may have a typo or two here, but the concept is sound I think. It results in a HR that spans the page or cell where it lives.

Code:
double height = (1.0 or greater);
Color hrFillColor = new Color(fA, fR, fG, fB);
Color hrBorderColor = new Color(bA, bR, bG, bB);

Paragraph p = new Paragraph();
Border newBorder = new Border { Style = BorderStyle.Single,  Color = hrBorderColor };

p.Format = new ParagraphFormat
{
     Font = new Font("Courier New", new Unit(height)),
     Shading = new Shading { Visible = true, Color = hrFillColor },
     Borders = new Borders
     {
          Bottom = newBorder,
          Left = newBorder.Clone(),
          Right = newBorder.Clone(),
          Top = newBorder.Clone()
     }
};

Author:  sanddy19 [ Mon Nov 08, 2010 11:56 am ]
Post subject:  Re: MigraDoc horizontal line divider

thanks for this solution, it worked great for my needs!

Author:  IdahoBangBang [ Wed Dec 13, 2017 7:57 pm ]
Post subject:  Re: MigraDoc horizontal line divider

sanddy19 wrote:
thanks for this solution, it worked great for my needs!


Here's another rendition:

public static Paragraph AddHorizontalRule(Section section, Unit fontSize, double percentWidth,
Color? color = null)
{
double percent = ((percentWidth <= 0.0 || percentWidth > 100.0) ? 100.0 : percentWidth) / 100.0;
Color hrColor = color ?? MigraDocColors.TextGray;

Unit contentWidth = section.PageSetup.PageWidth - section.PageSetup.LeftMargin - section.PageSetup.RightMargin;
Unit indentSize = (contentWidth - (percent * contentWidth)) / 2.0;
Paragraph paragraph = section.AddParagraph();
paragraph.Format.LeftIndent = indentSize;
paragraph.Format.RightIndent = indentSize;
paragraph.Format.Font.Size = fontSize;
paragraph.Format.Shading.Color = hrColor;
paragraph.Format.LineSpacingRule = LineSpacingRule.Single;
paragraph.Format.Borders.Visible = false;

return paragraph;
}

Pass in a font size of "0.5" and you'll get a single horizontal line that is the same thickness as the default border thickness. You can define your own default color (I like dark gray, #666666). The method above lets you define a percent width, and the horizontal rule is centered horizontally regardless. Note that the method above also returns a Paragraph, and the default Paragraph.Format.SpaceBefore and SpaceAfter are going to be zero. Depending on the form, I'll add a little space before and after, e.g. "0.15cm".

Have fun!
Brian

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