在分页符之前,Migradoc丢失了下划线格式

时间:2014-02-26 12:54:51

标签: formatting pdf-generation underline migradoc

我有一个小的HTML-to-PDF,它使用MigraDoc生成PDF文件。将格式化文本添加到段落似乎存在错误。问题是多页PDF中第一页上的所有文本都会丢失下划线格式。粗体和斜体格式保留在所有页面上,但下划线仅显示从第二页开始。它是一个已知的Migradoc错误吗?

为了避免进一步的问题,我的代码是:

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;
}

更新:

只是让PdfSharp开发者知道,虽然我解决了这个问题,但我仍然无法理解它是实现还是库错误,或者两者兼而有之。通过更改样式的定义,更准确地说,字体颜色来解决问题。

对于正常样式,我们添加了类似于以下内容的代码:

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

然后,从普通样式继承的其他样式也可能具有不同颜色的字体,也可以从十六进制代码解析。

最后修复很小,只需对每种解析后的颜色进行更改:

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

解决这个错误的另一种方法是创建一个CMYK颜色。作为一个单独的问题,当我这样做时,我也注意到用上面的线解析的颜色和CMYK生成的颜色略有不同,所以颜色解析器似乎也是错误的。这里我假设HEX颜色0x222222应该与CMYK(0,0,0,86.7)相同。

问题是为什么所描述的修复解决了下划线问题错误,为什么错误只出现在页面制动之前?另外,有趣的是,在调试这个问题时,我最初开始删除一些pdf内容,并且当从PDF文档中删除某些表时,bug也会消失,这对我来说没有意义。

无论如何,感谢您让我知道如何创建mdddl文件。这很有帮助,因为它让我可以测试一些东西。

1 个答案:

答案 0 :(得分:0)

我无法用MigraDoc复制问题。

以下是我添加到MigraDoc Hello World示例中的代码:

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! "));

这会生成一个包含两个页面的PDF文件,并在两个页面上正确显示带下划线的文本。

不是一般问题 - 我应该等待SSCCE。