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

Fonts, Colors and Multiple Uses
https://forum.pdfsharp.net/viewtopic.php?f=2&t=1901
Page 1 of 1

Author:  Cory Dee [ Wed Feb 08, 2012 6:44 pm ]
Post subject:  Fonts, Colors and Multiple Uses

At the top of my document I have three Font's defined like this:

Code:
private Font headerFont = new Font("Arial", 13);
private Font titleFont = new Font("Clarendon", 20);
private Font normalFont = new Font("Arial", 10);


Then, at the beginning of a function I set their colors like this:

Code:
normalFont.Color = Colors.Black;
normalFont.Bold = false;
titleFont.Color = Color.Parse("0x43545E");
titleFont.Bold = true;
headerFont.Color = Color.Parse("0x43545E");
headerFont.Bold = true;


During the creation of my document, I do many calls like this (txtFr is a textframe):

Code:
var p = txtFr.AddParagraph("Header");
p.Format.Font = headerFont.Clone();

p = txtFr.AddParagraph("Content");
p.AddLineBreak();
p.AddLineBreak();
p.Format.Font = normalFont.Clone();

p = txtFr.AddParagraph("Another Title");
p.Format.Font = titleFont.Clone();


Anything that I set to normalFont seems to be working just fine.

The first time I use headerFont it works fine. Second (third, fourth, etc) time I use headerfont, the text is output correctly, but seems to be set to white (on a white background). Am I missing something here?

Cheers

Author:  Thomas Hoevel [ Thu Feb 09, 2012 8:35 am ]
Post subject:  Re: Fonts, Colors and Multiple Uses

Hi!
Cory Dee wrote:
Am I missing something here?
We never use MigraDoc this way. Maybe something goes wrong in the "Clone()" calls.

We would define Styles with the different colors and font styles. Then we'd just set the Style of the paragraphs.

See the file Styles.cs in the HelloMigraDoc sample for recommended usage.

Author:  Cory Dee [ Thu Feb 09, 2012 4:17 pm ]
Post subject:  Re: Fonts, Colors and Multiple Uses

Thanks for the idea, Thomas. I still seem to be having the same problem, though.

Here's the code now:

Code:
            //set the standard font
            var style = document.Styles["Normal"];
            style.Font.Name = "Arial";
            style.Font.Size = 10;
            style.Font.Color = Colors.Black;
            style.Font.Bold = false;

            //set the title font
            style = document.Styles["Heading1"];
            style.Font.Name = "Clarendon";
            style.Font.Size = 20;
            style.Font.Color = Color.Parse("0x43545E");
            style.Font.Bold = true;

            //set the heading font
            style = document.Styles["Heading2"];
            style.Font.Name = "Arial";
            style.Font.Size = 13;
            style.Font.Color = Color.Parse("0x43545E");
            style.Font.Bold = true;

            p = txtFr.AddParagraph("heading here");
            p.Style = "Heading2";
           
            p = txtFr.AddParagraph("normal text here");
            p.AddLineBreak();
            p.AddLineBreak();
            p.Style = "Normal";

            p = txtFr.AddParagraph("heading2");
            p.Style = "Heading2";


The "heading2" looks to be rendered (I can cut and paste it), but it appears to white text on white background.

Author:  Cory Dee [ Thu Feb 09, 2012 8:24 pm ]
Post subject:  Re: Fonts, Colors and Multiple Uses

This seems to be an issue with Color.Parse(). I've switched this:
style.Font.Color = Color.Parse("0x43545E");
For this:
style.Font.Color = Color.FromRgbColor((byte)255, Color.Parse("0x43545E"));

And now it works just fine! Weird.

Author:  Thomas Hoevel [ Mon Feb 13, 2012 9:27 am ]
Post subject:  Re: Fonts, Colors and Multiple Uses

Cory Dee wrote:
style.Font.Color = Color.Parse("0x43545E");

This should also work: style.Font.Color = Color.Parse("0xFF43545E");

Your text probably wasn't white, it was transparent. This makes a difference when you activate the transparency grid in Adobe Reader.
I should have checked the Color.Parse source code last week. :oops:

So your original code should work - but using styles is better when the document grows (and you can add own styles when needed).

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