コード例 #1
0
ファイル: StyleHelper.cs プロジェクト: xw-fighting/DocxToPdf
        /// <summary>
        /// Get paragraph's numbering level object by searching abstractNums with the numbering level ID.
        /// </summary>
        /// <param name="pg"></param>
        /// <returns></returns>
        public Word.Level GetNumbering(Word.Paragraph pg)
        {
            Word.Level ret = null;

            if (pg == null || this.numbering == null)
            {
                return(ret);
            }

            Word.ParagraphProperties pgpr = pg.Elements <Word.ParagraphProperties>().FirstOrDefault();
            if (pgpr == null)
            {
                return(ret);
            }

            // direct numbering
            Word.NumberingProperties numPr = pgpr.Elements <Word.NumberingProperties>().FirstOrDefault();
            if (numPr != null && numPr.NumberingId != null)
            {
                int numId = numPr.NumberingId.Val;
                if (numId > 0) // numId == 0 means this paragraph doesn't have a list item
                {
                    int?   ilvl         = null;
                    String refStyleName = null;

                    if (numPr.NumberingLevelReference != null)
                    { // ilvl included in NumberingProperties
                        ilvl = numPr.NumberingLevelReference.Val;
                    }
                    else
                    { // doesn't have ilvl in NumberingProperties, search by referenced style name
                        Word.Style st = pgpr.Elements <Word.Style>().FirstOrDefault();
                        if (st != null && st.StyleName != null)
                        {
                            refStyleName = st.StyleName.Val;
                        }
                    }

                    // find abstractNumId by numId
                    Word.NumberingInstance numInstance = this.numbering.Elements <Word.NumberingInstance>().FirstOrDefault(c => c.NumberID.Value == numId);
                    if (numInstance != null)
                    {
                        // find abstractNum by abstractNumId
                        Word.AbstractNum abstractNum = (Word.AbstractNum) this.numbering.Elements <Word.AbstractNum>().FirstOrDefault(c =>
                                                                                                                                      c.AbstractNumberId.Value == numInstance.AbstractNumId.Val);
                        if (abstractNum != null)
                        {
                            if (ilvl != null) // search by ilvl
                            {
                                ret = abstractNum.Elements <Word.Level>().FirstOrDefault(c => c.LevelIndex == ilvl);
                            }
                            else if (refStyleName != null) // search by matching referenced style name
                            {
                                ret = abstractNum.Elements <Word.Level>().FirstOrDefault(c =>
                                {
                                    return((c.ParagraphStyleIdInLevel != null) ? c.ParagraphStyleIdInLevel.Val == refStyleName : false);
                                });
                            }
                        }
                    }
                }
            }

            // TODO: linked style
            if (ret == null)
            {
                ;
            }

            return(ret);
        }
コード例 #2
0
        // Create content of numberingDefinitionsPart.
        private static void CreateNumberingDefinitionsPartContent(NumberingDefinitionsPart numberingDefinitionsPart)
        {
            Numbering numbering = new Numbering();

            AbstractNum abstractNum = new AbstractNum()
            {
                AbstractNumberId = 0
            };
            MultiLevelType multiLevelType1 = new MultiLevelType()
            {
                Val = MultiLevelValues.HybridMultilevel
            };

            DocumentFormat.OpenXml.Wordprocessing.Level level1 = new DocumentFormat.OpenXml.Wordprocessing.Level()
            {
                LevelIndex = 0
            };
            StartNumberingValue startNumberingValue1 = new StartNumberingValue()
            {
                Val = 1
            };
            NumberingFormat numberingFormat1 = new NumberingFormat()
            {
                Val = NumberFormatValues.Decimal
            };
            LevelText levelText1 = new LevelText()
            {
                Val = "%1."
            };
            LevelJustification levelJustification1 = new LevelJustification()
            {
                Val = LevelJustificationValues.Left
            };

            PreviousParagraphProperties previousParagraphProperties1 = new PreviousParagraphProperties();
            Indentation indentation1 = new Indentation()
            {
                Left = "720", Hanging = "360"
            };

            previousParagraphProperties1.Append(indentation1);

            level1.Append(startNumberingValue1);
            level1.Append(numberingFormat1);
            level1.Append(levelText1);
            level1.Append(levelJustification1);
            level1.Append(previousParagraphProperties1);

            DocumentFormat.OpenXml.Wordprocessing.Level level2 = new DocumentFormat.OpenXml.Wordprocessing.Level()
            {
                LevelIndex = 1
            };
            StartNumberingValue startNumberingValue2 = new StartNumberingValue()
            {
                Val = 1
            };
            NumberingFormat numberingFormat2 = new NumberingFormat()
            {
                Val = NumberFormatValues.LowerLetter
            };
            LevelText levelText2 = new LevelText()
            {
                Val = "(%2)"
            };
            LevelJustification levelJustification2 = new LevelJustification()
            {
                Val = LevelJustificationValues.Left
            };

            PreviousParagraphProperties previousParagraphProperties2 = new PreviousParagraphProperties();
            Indentation indentation2 = new Indentation()
            {
                Left = "1440", Hanging = "360"
            };

            previousParagraphProperties2.Append(indentation2);

            level2.Append(startNumberingValue2);
            level2.Append(numberingFormat2);
            level2.Append(levelText2);
            level2.Append(levelJustification2);
            level2.Append(previousParagraphProperties2);

            abstractNum.Append(multiLevelType1);
            abstractNum.Append(level1);
            abstractNum.Append(level2);

            NumberingInstance numberingInstance1 = new NumberingInstance()
            {
                NumberID = 1
            };
            AbstractNumId abstractNumId1 = new AbstractNumId()
            {
                Val = 0
            };

            numberingInstance1.Append(abstractNumId1);

            numbering.Append(abstractNum);
            numbering.Append(numberingInstance1);

            numberingDefinitionsPart.Numbering = numbering;
        }