PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 2:31 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Sat Jun 14, 2008 4:05 pm 
Offline

Joined: Sat Apr 12, 2008 8:51 am
Posts: 10
Hi !
I needed to shade a word inside a paragraph, so I've made some changes...
Felt like sharing :)

First things first, I added a Shading property to the FormattedText class (MigraDocLite\MigraDoc.DocumentObjectModel\MigraDoc.DocumentObjectModel\FormattedText.cs) :

Code:
    /// <summary>
    /// Gets the shading object.
    /// </summary>
    public Shading Shading
    {
        get
        {
            if (this.shading == null)
                this.shading = new Shading(this);

            return this.shading;
        }
        set
        {
            SetParent(value);
            this.shading = value;
        }
    }
    [DV]
    internal Shading shading;


Then I've adapted the DeepCopy() method :

Code:
    /// <summary>
    /// Implements the deep copy of the object.
    /// </summary>
    protected override object DeepCopy()
    {
      FormattedText formattedText = (FormattedText)base.DeepCopy();
      if (formattedText.font != null)
      {
        formattedText.font = formattedText.font.Clone();
        formattedText.font.parent = formattedText;
      }
      if (formattedText.shading != null)
      {
          formattedText.shading = formattedText.shading.Clone();
          formattedText.shading.parent = formattedText;
      }
      if (formattedText.elements != null)
      {
        formattedText.elements = formattedText.elements.Clone();
        formattedText.elements.parent = formattedText;
      }
      return formattedText;
    }


And the Serialize() one as well (I admit, I'm quite not sure of what I've done in this one :oops: ) :

Code:
    /// <summary>
    /// Converts FormattedText into DDL.
    /// </summary>
    internal override void Serialize(Serializer serializer)
    {
      bool isFormatted = false;
      if (!this.IsNull("Font"))
      {
        this.Font.Serialize(serializer);
        isFormatted = true;
      }
      else
      {
        if (!this.style.IsNull)
        {
          serializer.Write("\\font(\"" + this.Style + "\")");
          isFormatted = true;
        }
      }

      if (!this.IsNull("Shading"))
          this.shading.Serialize(serializer);

      if (isFormatted)
        serializer.Write("{");

      if (!this.IsNull("Elements"))
        this.Elements.Serialize(serializer);

      if (isFormatted)
        serializer.Write("}");
    }


Now that FormattedText supports shading.. we need to render it ;)

Next changes are in the ParagraphRender class (MigraDocLite\MigraDoc.Rendering\MigraDoc.Rendering\ParagraphRenderer.cs) :

First, a GetShading() method based on GetHyperlink() (lazy me :oops: ) :

Code:
    Shading GetShading()
    {
        DocumentObject parent = DocumentRelations.GetParent(this.currentLeaf.Current);
        parent = DocumentRelations.GetParent(parent);
        if (parent is FormattedText)
        {
            FormattedText text;

            text = ((FormattedText)parent);
            if (text.IsNull("Shading"))
                return null;
            else
                return text.Shading;
        }
        else
            return null;
    }


Next is a new RealizeShading() method (there's already a RenderShading here...) :

Code:
    void RealizeShading(XUnit width)
    {       
        Shading shading = GetShading();

        if (shading != null)
        {
            ShadingRenderer shadingRenderer = new ShadingRenderer(this.gfx, shading);

            shadingRenderer.Render(this.currentXPosition, this.currentYPosition, width, this.currentVerticalInfo.height);
        }               
    }   


Now, we juste need to call this one everytime it's necessary...

...In RenderImage(), RenderTab(), RenderBlank(), RenderWord() and RenderLinebreak()

Code:
    void RenderImage(Image image)
    {
      RenderInfo renderInfo = this.CurrentImageRenderInfo;
      XUnit top = this.CurrentBaselinePosition;
      Area contentArea = renderInfo.LayoutInfo.ContentArea;
      top -= contentArea.Height;
     
      RealizeShading(contentArea.Width);
       
      RenderByInfos(this.currentXPosition, top, new RenderInfo[] { renderInfo });

      RenderUnderline(contentArea.Width, true);
      RealizeHyperlink(contentArea.Width);
     

      this.currentXPosition += contentArea.Width;
    }



Code:
    void RenderTab()
    {
      TabOffset tabOffset = NextTabOffset();
      RealizeShading(tabOffset.offset);
      RenderUnderline(tabOffset.offset, false);
      RenderTabLeader(tabOffset);
      RealizeHyperlink(tabOffset.offset);     
      this.currentXPosition += tabOffset.offset;
    }


Code:
    void RenderBlank()
    {
      if (!IgnoreBlank())
      {
        XUnit wordDistance = this.CurrentWordDistance;
        RealizeShading(wordDistance);
        RenderUnderline(wordDistance, false);
        RealizeHyperlink(wordDistance);       
        this.currentXPosition += wordDistance;
      }
      else
      {
        // RealizeShading(0); Useless !
        RenderUnderline(0, false);
        RealizeHyperlink(0);       
      }
    }


Code:
    void RenderWord(string word)
    {
      Font font = this.CurrentDomFont;
      XFont xFont = CurrentFont;
      if (font.Subscript || font.Superscript)
        xFont = FontHandler.ToSubSuperFont(xFont);
      XUnit wordWidth = MeasureString(word);

      RealizeShading(wordWidth);
      this.gfx.DrawString(word, xFont, CurrentBrush, this.currentXPosition, this.CurrentBaselinePosition);     
      RenderUnderline(wordWidth, true);
      RealizeHyperlink(wordWidth);
     
      this.currentXPosition += wordWidth;
    }


Code:
    void RenderLinebreak()
    {
      //this.RealizeShading(0); useless !
      this.RenderUnderline(0, false);
      this.RealizeHyperlink(0);     
    }


Some calls are in comments 'cuz there're kind of useless when with the width is null :roll:

I've not tested all the cases, but it's seems to be working :wink:

Have a nice day !


Top
 Profile  
Reply with quote  
PostPosted: Wed Dec 15, 2010 12:22 pm 
Offline

Joined: Fri Nov 05, 2010 3:53 pm
Posts: 4
Hi,

I tried to implement this into the latest MigraDoc V1.31 and cannot get it to work. The FormattedText class is aware of the new Shading syntax, but the rendering of my rtf documents doesn't show any shading on the text.

I use my own small function to write FormattedText to my documents. As a test, I tried to force all text that is written into my document to have a Yellow background. In the end I will be adapting this to allow me to put a background shading on any text/tabs that require them.

Here's my function code. Am I doing something wrong?
Code:
public static void rtf_WriteText(string FontName, char Bold, char Italic, string FontSize,
         MigraDoc.DocumentObjectModel.Color Colour, string TextOut)
      {
         FormattedText ft = paragraph.AddFormattedText(TextOut);
         ft.Style = "ICPRow";
         ft.Font.Name = FontName;
         ft.Font.Size = FontSize;
         ft.Font.Color = Colour;
         ft.Shading.Color = MigraDoc.DocumentObjectModel.Colors.Yellow;  // << This doesn't perform the shading.
         if (Bold == 'Y')
            ft.Font.Bold = true;
         else
            ft.Font.Bold = false;
         if (Italic == 'Y')
            ft.Font.Italic = true;
         else
            ft.Font.Italic = false;
         ft.Font.Underline = Underline.None;
      }


I have implemented the updated dll's into my project and re-built the entire solution. I know this post is a couple of years old now, but if you (or anyone following this thread) could please help, I would be most grateful!

Kind regards,

Sean


Top
 Profile  
Reply with quote  
PostPosted: Wed May 13, 2015 4:47 am 
Offline

Joined: Wed May 13, 2015 4:04 am
Posts: 1
ldelabre wrote:
Hi !
I needed to shade a word inside a paragraph, so I've made some changes...
Felt like sharing :)
...

It is realy works! Thanks for this code.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 142 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group