C# 为什么我的段落格式不显示?

C# 为什么我的段落格式不显示?,c#,pdf,format,paragraph,migradoc,C#,Pdf,Format,Paragraph,Migradoc,两年后,我刚回来和MigraDoc&PDFsharp做了几件事。 为了让生活更轻松,我为样式创建了一个助手函数,我想知道为什么它不能像预期的那样工作 所有字体分配都很好:字体系列、大小、类型以及颜色都很好。链接也会被创建,并且也会起作用 但所有段落样式都被忽略。正如我在调试器中看到的那样,它们被指定给新样式,但它们不会被渲染 这可能是关于段落和章节的规则,我不知道 有人能启发我吗 // all styles by name/definition public string allStyles =

两年后,我刚回来和MigraDoc&PDFsharp做了几件事。 为了让生活更轻松,我为样式创建了一个助手函数,我想知道为什么它不能像预期的那样工作

所有字体分配都很好:字体系列、大小、类型以及颜色都很好。链接也会被创建,并且也会起作用

但所有段落样式都被忽略。正如我在调试器中看到的那样,它们被指定给新样式,但它们不会被渲染

这可能是关于段落和章节的规则,我不知道

有人能启发我吗

// all styles by name/definition
public string allStyles = "";

private string style(string styleName)
      {
          if (allStyles.IndexOf(" " + styleName + " ") < 0)  // the stylename is new, so we create it..
          {
                string style = styleName + "   ";
                string fontChar = style[0].ToString();
                string fs = "";
                if (style[1] >= '0' & style[1] <= '9') fs += style.Substring(1, 1);
                if (style[2] >= '0' & style[2] <= '9') fs += style.Substring(2, 1);
                if (style[3] >= '0' & style[3] <= '9') fs += style.Substring(3, 1);
                int fontSize = Convert.ToInt32(fs);
// now digits after position 2 may be ignored
// we use the rest of the stylename for the rest of the style details..
                string styleName2 = styleName.Substring(1);
// add base style to the document style cache       
                Style newStyle = document.AddStyle(styleName, "Normal");
// now we modify the new style..:
                newStyle.Font.Bold = styleName.IndexOf("B") >= 0;
                newStyle.Font.Italic = styleName.IndexOf("I") >= 0;
                if (fontChar == "A") newStyle.Font.Name = "Arial";                
// .. 25 more fonts omitted..
// ..
                if (styleName.IndexOf("a") >= 0) newStyle.Font.Color = MigraDoc.DocumentObjectModel.Colors.AntiqueWhite;
// .. 25 more colors omitted..
// ..
// .. here a a few ParagraphFormat styles, all of which don't work!!
                if (styleName2.IndexOf("L") >= 0) newStyle.ParagraphFormat.Alignment = ParagraphAlignment.Left;
                else if (styleName2.IndexOf("R") >= 0) newStyle.ParagraphFormat.Alignment = ParagraphAlignment.Right;
                else if (styleName2.IndexOf("C") >= 0) newStyle.ParagraphFormat.Alignment = ParagraphAlignment.Center;
                else if (styleName2.IndexOf("J") >= 0) newStyle.ParagraphFormat.Alignment = ParagraphAlignment.Justify;
                if (styleName2.IndexOf("____") >= 0) newStyle.ParagraphFormat.SpaceAfter = 15;
                else if (styleName2.IndexOf("___") >= 0) newStyle.ParagraphFormat.SpaceAfter = 10;
                else if (styleName2.IndexOf("__") >= 0) newStyle.ParagraphFormat.SpaceAfter = 6;
                else if (styleName2.IndexOf("_") >= 0) newStyle.ParagraphFormat.SpaceAfter = 3;

// add stylename to the collection string
                allStyles += "  " + styleName + "  ";
            }
// return the name after creating and modifying the style
            return styleName;                


// a plain FT output function           
        public void writeFT(Section currentSection, string text, string styl, bool newParagraph)
        {
            Paragraph currentParagraph;
            if (newParagraph) currentParagraph = currentSection.AddParagraph();
            else currentParagraph = currentSection.LastParagraph;
            currentParagraph.AddFormattedText(text, style(styl));
        }    



 // an function to output a hyperlink               
         public void writeLink(Section currentSection, string text, string link, string styl, bool newParagraph)
        {
            Paragraph currentParagraph;
            if (newParagraph) currentParagraph = currentSection.AddParagraph();
            else  currentParagraph = currentSection.LastParagraph;
            Hyperlink HL = currentParagraph.AddHyperlink(link, HyperlinkType.Bookmark);
            HL.AddFormattedText(text, style(styl));
        }               

 // and one for anchors

        public void writeAnchor(Section currentSection, string text, string anchor, string styl, bool newParagraph)
        {
            Paragraph currentParagraph;
            if (newParagraph ) currentParagraph = currentSection.AddParagraph();
            else currentParagraph = currentSection.LastParagraph;
            currentParagraph.AddFormattedText(   text, style(styl)); 
            currentParagraph.AddBookmark(anchor);
        }                


// an example call
            writeFT(somesection, "This should be BIG & BLUE ", "A16b",true);
            writeFT(somesection, "This should be BIG & RED  ", "A16r",true);
            writeFT(somesection, "GREEN but not spaced out", "A16g---___",true);
            writeFT(somesection, "This should be BIG & BLACK", "A16k",true);
            writeFT(somesection, "This should be BIG & BLUE ", "A16b",true);
            writeFT(somesection, "This should be BIG & BLUE ", "A16b",true);

要设置段落样式,只需使用current段落.Style=stylestyl;。如果要为段落使用该样式,可以使用AddText而不是AddFormattedText添加文本,并且仍然可以调用AddFormattedText以添加具有不同字符格式的文本

AddFormattedText仅支持字符格式,不支持段落格式。合乎逻辑,因为它是段落的一部分

您的API应该考虑到这一点