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,

2 Answers 2

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.

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.

2
  • 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. Jun 21, 2013 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, 2013 at 12:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.