PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 4:26 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Fri Mar 19, 2010 3:12 pm 
Offline

Joined: Fri Mar 19, 2010 3:00 pm
Posts: 3
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.


Top
 Profile  
Reply with quote  
PostPosted: Mon Mar 22, 2010 10:26 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Thu Jun 24, 2010 5:01 pm 
Offline
Supporter
User avatar

Joined: Thu May 27, 2010 7:40 pm
Posts: 59
Location: New Hampshire, USA
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()
     }
};


Top
 Profile  
Reply with quote  
PostPosted: Mon Nov 08, 2010 11:56 am 
Offline

Joined: Thu Nov 04, 2010 11:57 am
Posts: 3
thanks for this solution, it worked great for my needs!


Top
 Profile  
Reply with quote  
PostPosted: Wed Dec 13, 2017 7:57 pm 
Offline

Joined: Mon Oct 17, 2016 4:29 pm
Posts: 1
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


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

All times are UTC


Who is online

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