none
how to merge up all the property that apply to one run? RRS feed

  • Question

  • Hi

    I've read the reference :<<ECMA-376, Fourth Edition, Part 1 - Fundamentals And Markup Language Reference>>

    in section 17.7.2 Style Hierarchy, it described the hierarchy of the style definition.

    as the rules is complicated, I'm thinking is there any API have implemented this merge processing?

    I want to retrieve exactly the styles(rPr,pPr...) apply to one paragraph or one run.

    Is there any out-of-box API I can use?

    what I need is like:

    when I got the style define like this

    <w:style w:styleId="Strong">
    <w:basedOn w:val="Underline"/>

    <w:rPr>
    <w:b/>
    </w:rPr>
    </w:style>
    <w:style w:styleId="Underline">
    <w:basedOn w:val="Emphasis"/>

    <w:rPr>
    <w:u/>
    </w:rPr>
    </w:style>
    <w:style w:styleId="Emphasis">

    <w:rPr>
    <w:i/>
    </w:rPr>
    </w:style>

    the API function can give me a merged rpr,tell me the "strong" style is:

     Bold
     Underline (inherited from Underline)
     Italics (inherited from Emphasis)

    and if the certain "run" got it's own style define, then merge it too.

    thanks a lot!

    Tuesday, April 21, 2015 1:07 PM

All replies

  • Hi gujj001,

    For this requirement, you could add a new style to StyleDefinitionsPart, then apply the style to the run.

    Simple code:

     StyleDefinitionsPart stylePart = mainPart.AddNewPart<StyleDefinitionsPart>();
                Style style = new Style()
                {
                    Type = StyleValues.Character,
                    CustomStyle = true,
                    StyleId = "CharacterStyle1"
                };
                LinkedStyle linkedStyle1 = new LinkedStyle() { Val = "LinkedStyle" };
                style.Append(linkedStyle1);
                style.Append(new Name() { Val = "CharacterStyle1" });
                StyleRunProperties styleRunProperties1 = new StyleRunProperties();
                Color color = new Color() { Val = "FF0000" };
                RunFonts font1 = new RunFonts() { ComplexScript = "Tahoma" };
                styleRunProperties1.Append(color);
                styleRunProperties1.Append(font1);
                styleRunProperties1.Append(new Bold());
                styleRunProperties1.Append(new FontSize() { Val = "48" });
                style.Append(styleRunProperties1);
                stylePart.Styles = new Styles();
    
                stylePart.Styles.Append(style);
    
    
    

     Paragraph heading = new Paragraph();
                ParagraphProperties headingPPr = new ParagraphProperties();
                heading.Append(headingPPr);
    
                Run run1 = new Run();
                Text textRun1 = new Text("THIS IS TEST RUN 1");
                run1.Append(textRun1);
                RunProperties rprRun1 = new RunProperties {RunStyle = new RunStyle() {Val = "CharacterStyle1"}};
                run1.Append(rprRun1);
                heading.Append(run1);
                body.Append(heading);

    Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, April 22, 2015 8:03 AM
    Moderator
  • Hi Starain

    thanks for the reply!

    I think you mean that I can ignore the style definition and make my own style define for a certain run.

    What I want to do is analyze the users' input word file.

    Users may use a style that pre-defined in word(Microsoft office), like: title, heading1...

    but the "title" may based on "normal", and other styles may have a "based on tree" more than 1 level

    and users can also change the font, size, color... based on the "title" style.

    So, I must merge up all the styles(by based on tree) and the direct style for the certain run to confirm the way it looks like in word file.

    What I want to know is, is there any API in SDK can let me get a merged up result for each style or for the certain run.

    Thanks!

    Best Regards

    Gujj

    Thursday, April 23, 2015 12:26 AM
  • Hi Gujj,

    There is the API to accomplish that directly, you could get the specify paragraphs through open xml API, then analyze them.

    The simple code of getting paragraphs:

     using (var wordprocessingDocument = WordprocessingDocument.Open(documentFileName, false))
            {
                paragraphs = wordprocessingDocument.MainDocumentPart.Document.Body
                    .OfType<Paragraph>()
                    .Where(p => p.ParagraphProperties != null && 
                                p.ParagraphProperties.ParagraphStyleId != null && 
                                p.ParagraphProperties.ParagraphStyleId.Val.Value.Contains("Heading1")).ToList();
            }
    

    Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, April 23, 2015 3:06 AM
    Moderator