-1

我有一个小的 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 文档中删除某些表格时,错误会消失,这对我来说毫无意义。

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

4

1 回答 1

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。

于 2014-02-27T09:41:08.037 回答