-1

I have a small HTML-to-PDF which uses MigraDoc to produce PDF files. There seems to be a bug with adding formatted text to a paragraph. The problem is that underline formatting gets lost for all text on first page in a multipage PDF. Bold and Italic formatting stays preserved on all pages, but underline only shows starting from second page. Is it a known Migradoc bug?

Just to avoid further questions here is my code:

var textFormat = this.BuildTextFormat(isBold, isItalic, isUnderline);
var formattedText = paragraph.AddFormattedText(textFormat);
formattedText.Add(new Text(text));


private TextFormat BuildTextFormat(bool isBold, bool isItalic, bool isUnderline)
{
    var textFormat = TextFormat.NoUnderline;

    if (isUnderline)
    {
        textFormat = TextFormat.Underline;
    }

    if (isBold)
    {
        textFormat |= TextFormat.Bold;
    }

    if (isItalic)
    {
        textFormat |= TextFormat.Italic;
    }

    return textFormat;
}

UPDATE:

Just to let PdfSharp devs know, that although I solved the issue but I still cannot understand whether it was an implementation or library bug, or both. The problem was solved by changing definition of styles, and more precisely, font colours.

For the normal style we added a code similar to the following:

    var style = document.Styles["Normal"];
    style.Font.Color = Color.Parse("0x222222");

Then, other styles inherited from the normal style also could have fonts with different colours, also parsed from a Hexadecimal code.

The final fix was tiny, just making change to each parsed colour like this:

style.Font.Color = Color.Parse("0xFF222222");

Another way to solve the bug was to create a CMYK colour. As a separate issue, when doing that I also noticed that colour parsed with the above line and the CMYK-generated colour are slightly different, so the colour parser also seems to be buggy. Here I assume that the HEX colour 0x222222 should be same as CMYK(0,0,0,86.7).

The question is why the described fix solved the underline issue bug, and why the bug appeared only before page brake? Also, interestingly, when debugging this issue then I initially started to remove some pdf content and there was also a scenario when bug would disappear when certain tables were removed from the PDF document, which makes no sense to me.

In any case, thank you for letting me know how to create an mdddl file. It was helpful as it allowed me to test few things.

lekso
  • 1,731
  • 3
  • 24
  • 46
  • The best way to avoid questions is providing a SSCCE (see also: http://sscce.org/ ). Could be a MigraDoc bug, could be a usage problem. Instead of an SSCCE, you could provide a MigraDoc DDL file that shows the error: http://pdfsharp.net/wiki/MigraDocDDL.ashx – I liked the old Stack Overflow Feb 27 '14 at 07:19
  • "Is it a known Migradoc bug?" Is it a MigraDoc bug or is it a usage problem? Would be nice to get some feedback beyond the code snippet posted so far. – I liked the old Stack Overflow Mar 04 '14 at 09:33
  • Color "0x222222" is transparent, so "0xff222222" is the correct value for an opaque color. MigraDoc converts "0xff222222" to CMYK(0,0,0,86.7), so the code does not seem buggy. But Adobe Reader seems to calculate a little bit different and CMYK(0,0,0,86.7) looks slightly different than "0xff222222". So "0x222222" should be a completely transparent color - but should look the same for text and underline and on page 1 and page 2. But "0xff222222" is the correct value and with this value everything works. An MDDDL file allows you to remove confidential data and we could replicate the bug. – I liked the old Stack Overflow Mar 20 '14 at 15:13
  • thank you for quick response. Around 11.30 UK time I sent an email to info2@empira with mdddl files. Hope that they'll help to replicate the bug, otherwise let me know if you need anything else. – lekso Mar 21 '14 at 11:34

1 Answers1

0

I cannot replicate a problem with MigraDoc.

Here's the code I added to the MigraDoc Hello World sample:

paragraph2 = section.AddParagraph();
var textFormat0 = BuildTextFormat(false, false, false);
var textFormat1 = BuildTextFormat(false, false, true);
var textFormat2 = BuildTextFormat(false, true, false);
var textFormat3 = BuildTextFormat(false, true, true);
var textFormat4 = BuildTextFormat(true, false, false);
var textFormat5 = BuildTextFormat(true, false, true);
var textFormat6 = BuildTextFormat(true, true, false);
var textFormat7 = BuildTextFormat(true, true, true);
var formattedText = paragraph2.AddFormattedText(textFormat0);
formattedText.Add(new Text("Hello, World! "));
formattedText = paragraph2.AddFormattedText(textFormat1);
formattedText.Add(new Text("Hello, World! "));
formattedText = paragraph2.AddFormattedText(textFormat2);
formattedText.Add(new Text("Hello, World! "));
formattedText = paragraph2.AddFormattedText(textFormat3);
formattedText.Add(new Text("Hello, World! "));
formattedText = paragraph2.AddFormattedText(textFormat4);
formattedText.Add(new Text("Hello, World! "));
formattedText = paragraph2.AddFormattedText(textFormat5);
formattedText.Add(new Text("Hello, World! "));
formattedText = paragraph2.AddFormattedText(textFormat6);
formattedText.Add(new Text("Hello, World! "));
formattedText = paragraph2.AddFormattedText(textFormat7);
formattedText.Add(new Text("Hello, World! "));
formattedText = paragraph2.AddFormattedText(textFormat0);
formattedText.Add(new Text("Hello, World! "));
formattedText = paragraph2.AddFormattedText(textFormat1);
formattedText.Add(new Text("Hello, World! "));
formattedText = paragraph2.AddFormattedText(textFormat2);
formattedText.Add(new Text("Hello, World! "));
formattedText = paragraph2.AddFormattedText(textFormat3);
formattedText.Add(new Text("Hello, World! "));
formattedText = paragraph2.AddFormattedText(textFormat4);
formattedText.Add(new Text("Hello, World! "));
formattedText = paragraph2.AddFormattedText(textFormat5);
formattedText.Add(new Text("Hello, World! "));
formattedText = paragraph2.AddFormattedText(textFormat6);
formattedText.Add(new Text("Hello, World! "));
formattedText = paragraph2.AddFormattedText(textFormat7);
formattedText.Add(new Text("Hello, World! "));
paragraph2 = section.AddParagraph();
formattedText = paragraph2.AddFormattedText(textFormat0);
formattedText.Add(new Text("Hello, World! "));
formattedText = paragraph2.AddFormattedText(textFormat1);
formattedText.Add(new Text("Hello, World! "));
formattedText = paragraph2.AddFormattedText(textFormat2);
formattedText.Add(new Text("Hello, World! "));
formattedText = paragraph2.AddFormattedText(textFormat3);
formattedText.Add(new Text("Hello, World! "));
formattedText = paragraph2.AddFormattedText(textFormat4);
formattedText.Add(new Text("Hello, World! "));
formattedText = paragraph2.AddFormattedText(textFormat5);
formattedText.Add(new Text("Hello, World! "));
formattedText = paragraph2.AddFormattedText(textFormat6);
formattedText.Add(new Text("Hello, World! "));
formattedText = paragraph2.AddFormattedText(textFormat7);
formattedText.Add(new Text("Hello, World! "));
section.AddPageBreak();
paragraph2 = section.AddParagraph();
formattedText = paragraph2.AddFormattedText(textFormat0);
formattedText.Add(new Text("Hello, World! "));
formattedText = paragraph2.AddFormattedText(textFormat1);
formattedText.Add(new Text("Hello, World! "));
formattedText = paragraph2.AddFormattedText(textFormat2);
formattedText.Add(new Text("Hello, World! "));
formattedText = paragraph2.AddFormattedText(textFormat3);
formattedText.Add(new Text("Hello, World! "));
formattedText = paragraph2.AddFormattedText(textFormat4);
formattedText.Add(new Text("Hello, World! "));
formattedText = paragraph2.AddFormattedText(textFormat5);
formattedText.Add(new Text("Hello, World! "));
formattedText = paragraph2.AddFormattedText(textFormat6);
formattedText.Add(new Text("Hello, World! "));
formattedText = paragraph2.AddFormattedText(textFormat7);
formattedText.Add(new Text("Hello, World! "));
formattedText = paragraph2.AddFormattedText(textFormat0);
formattedText.Add(new Text("Hello, World! "));
formattedText = paragraph2.AddFormattedText(textFormat1);
formattedText.Add(new Text("Hello, World! "));
formattedText = paragraph2.AddFormattedText(textFormat2);
formattedText.Add(new Text("Hello, World! "));
formattedText = paragraph2.AddFormattedText(textFormat3);
formattedText.Add(new Text("Hello, World! "));
formattedText = paragraph2.AddFormattedText(textFormat4);
formattedText.Add(new Text("Hello, World! "));
formattedText = paragraph2.AddFormattedText(textFormat5);
formattedText.Add(new Text("Hello, World! "));
formattedText = paragraph2.AddFormattedText(textFormat6);
formattedText.Add(new Text("Hello, World! "));
formattedText = paragraph2.AddFormattedText(textFormat7);
formattedText.Add(new Text("Hello, World! "));
paragraph2 = section.AddParagraph();
formattedText = paragraph2.AddFormattedText(textFormat0);
formattedText.Add(new Text("Hello, World! "));
formattedText = paragraph2.AddFormattedText(textFormat1);
formattedText.Add(new Text("Hello, World! "));
formattedText = paragraph2.AddFormattedText(textFormat2);
formattedText.Add(new Text("Hello, World! "));
formattedText = paragraph2.AddFormattedText(textFormat3);
formattedText.Add(new Text("Hello, World! "));
formattedText = paragraph2.AddFormattedText(textFormat4);
formattedText.Add(new Text("Hello, World! "));
formattedText = paragraph2.AddFormattedText(textFormat5);
formattedText.Add(new Text("Hello, World! "));
formattedText = paragraph2.AddFormattedText(textFormat6);
formattedText.Add(new Text("Hello, World! "));
formattedText = paragraph2.AddFormattedText(textFormat7);
formattedText.Add(new Text("Hello, World! "));

This results in a PDF file with two pages and underlined text shows correctly on both pages.

Not a general problem - I should have waited for the SSCCE.

  • thanks and sorry for letting you wait. I updated my initial post. This issue is a bit more complicated and is also difficult to replicate. Sorry I cannot submit my full code here – lekso Mar 20 '14 at 11:35