none
Word Open XML Plain text content control RRS feed

  • Question

  • Hi,

    I am using Open XML format to generate Word documents based on defined templates, in the template i'm having a Plain text content control for displaying a paragraphs.  I'm trying to show multiple paragraphs in this plain text content control.  If there are only one paragraph all the data is showing without any issues.  When there are multiple paragraphs to be shown, i'm using new line of '\r\n' for line feeds its showing a special character of square box and some of the text gets overlapped. 

    Can you please suggest, how i can handle this new line characters to avoid showing special characters.

    Thanks

    Saravanan

    Wednesday, October 17, 2012 11:40 PM

Answers

  • Hi Saravanan,

    Thanks for posting in the MSDN Forum.

    I would recommend you take a look following snippet to see whether it can solve your issue:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml.Wordprocessing;
    using com.mksword.Net.OpenXmlTools;
    
    namespace ConsoleApplication11
    {
        public class EditSdtblock : OpenXmlUtil
        {
            public void Action()
            {
                OpenFile(OXUFileFilter.WORD);
                string Path = CopyFileName(OXUPathTarget.ORIGINAL);
                File.Copy(OriginalPath, Path, true);
                using (WordprocessingDocument WPD = WordprocessingDocument
                    .Open(Path, true))
                {
                    MainDocumentPart MDP = WPD.MainDocumentPart;
                    Document D = MDP.Document;
                    SdtBlock SB = D.Descendants<SdtBlock>().FirstOrDefault();
                    //Clean the content in Plain text content control
                    foreach (Paragraph P in 
                        SB.SdtContentBlock.Descendants<Paragraph>().ToList())
                    {
                        SetLog(P.InnerText, OXULogType.INFO);
                        P.Remove();
                    }
                    Paragraph P1 = new Paragraph();
                    Run R1 = new Run();
                    Text T1 = new Text() { Text = "Paragraph 1" };
                    R1.Append(T1);
                    P1.Append(R1);
                    SB.SdtContentBlock.Append(P1);
                    Paragraph P2 = new Paragraph();
                    Run R2 = new Run();
                    Text T2 = new Text() { Text = "Paragraph 2" };
                    R2.Append(T2);
                    P2.Append(R2);
                    SB.SdtContentBlock.Append(P2);
                    D.Save();
                }
            }
        }
    }

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Friday, October 19, 2012 2:41 AM
    Moderator