0

Is there a way to apply shading to part (for example, just a word) of a Paragraph in PdfSharp/MigraDoc? I tried adding a Style with the shading to the Document and then passing the style name to the AddFormattedText method but it only takes the font information from the style.

Thanks,

theburningmonk
  • 15,701
  • 14
  • 61
  • 104

2 Answers2

1

You can try loading your style to the Paragraph like this:

paragraph = section.AddParagraph();    
paragraph.Style = "StyleName";

Personally I didn't use the shading feature, but that's the way I load my styles and it works ok.

f1v3
  • 379
  • 6
  • 12
  • That adds a new paragraph though. I'm looking for a way to change the shading for a section of a paragraph not the whole paragraph itself. – theburningmonk Jun 21 '13 at 18:03
  • Did you solve the problem? You could just use a workaround and try creating a new paragraph containing the single word or just "your section", and then style the new paragraph acordingly. – f1v3 Jul 18 '13 at 12:32
1

I'm using PdfSharp/MigraDoc from few weeks and before answer precisely to your question I've read the source code of it, freely disponible.

The short answer is: IS NOT POSSIBLE

The long answer is: The only part on Style considered by AddFormattedText(string text, string style) is the Character part. Then the Shading, that is a part of ParagraphFormat, cannot be applied, and the renderized, by PdfSharp/MigraDoc.

The coded answer is:

   public FormattedText AddFormattedText(string text, string style)
    {
      FormattedText formattedText = AddFormattedText(text);
      formattedText.Style = style;
      return formattedText;
    }



 internal class FormattedTextRenderer : RendererBase
   ...
 /// <summary>
    /// Renders the style if it is a character style and the font of the formatted text.
    /// </summary>
    void RenderStyleAndFont()
    {
      bool hasCharacterStyle = false;
      if (!this.formattedText.IsNull("Style"))
      {
        Style style = this.formattedText.Document.Styles[this.formattedText.Style];
        if (style != null && style.Type == StyleType.Character)
          hasCharacterStyle = true;
      }
      object font = GetValueAsIntended("Font");
      if (font != null)
      {
        if (hasCharacterStyle)
          this.rtfWriter.WriteControlWithStar("cs", this.docRenderer.GetStyleIndex(this.formattedText.Style));

        RendererFactory.CreateRenderer(this.formattedText.Font, this.docRenderer).Render();
      }
    }

I hope this can help you. Davide.

Davide Dolla
  • 340
  • 4
  • 9