2014-02-18 4 views
0

2 년 후에 MigraDoc & PDFsharp로 몇 가지 작업을 수행했습니다. 생활을 더 편하게하기 위해 스타일을위한 도우미 함수를 만들었고 왜 이것이 예상대로 작동하지 않는지 궁금합니다.MigraDoc - 내 ParagraphFormats가 표시되지 않는 이유는 무엇입니까?

모든 글꼴 할당이 올바르게 작동합니다. 글꼴 모음, 크기 및 유형은 물론 색상이 올바르게 나옵니다. 또한 링크가 만들어지고 작동합니다.

그러나 모든 단락 스타일은 무시됩니다. 디버거에서 볼 수있는 것처럼 새로운 스타일에 할당되지만 렌더링되지는 않습니다.

아마 모르는 단락과 절에 대한 규칙 일 수 있습니다.

누군가 내게 깨달을 수 있습니까?

// 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); 

답변

0

단락 스타일을 설정하려면 currentParagraph.Style = style(styl);을 사용하십시오. 단락에 해당 스타일을 사용하려는 경우 AddFormattedText() 대신 AddText()을 사용하여 텍스트를 추가 할 수 있습니다. 다른 문자 형식의 텍스트를 추가하려면 AddFormattedText()을 계속 호출 할 수 있습니다.

AddFormattedText는 단락 서식이 아닌 문자 서식 만 지원합니다. 문단의 일부이므로 논리적입니다.

API가이를 고려해야합니다.

관련 문제