Exemplo n.º 1
0
        public Word.ParagraphFormat CreateParagraphTemplate(Word.Range range)
        {
            Word.ParagraphFormat paragraphFormat = range.ParagraphFormat;

            paragraphFormat.LeftIndent = _application.CentimetersToPoints(0);
            // отсуп первой строки
            paragraphFormat.FirstLineIndent = _application.CentimetersToPoints(1.27f);
            return(paragraphFormat);
        }
Exemplo n.º 2
0
        // 复制到WORD.ParagraphFormat同名参数值
        public void copy2(Word.ParagraphFormat dstParaFmt)
        {
            dstParaFmt.Alignment = this.Alignment;                         // 复制到WORD.ParagraphFormat同名参数值

            dstParaFmt.AutoAdjustRightIndent = this.AutoAdjustRightIndent; // 复制到WORD.ParagraphFormat同名参数值
            dstParaFmt.BaseLineAlignment     = this.BaseLineAlignment;     // 复制到WORD.ParagraphFormat同名参数值

            Word.Borders bds = dstParaFmt.Borders;                         // 复制到WORD.ParagraphFormat同名参数值
            //this.Borders.copy2(ref bds);

            dstParaFmt.CharacterUnitFirstLineIndent = this.CharacterUnitFirstLineIndent;       // 复制到WORD.ParagraphFormat同名参数值
            dstParaFmt.CharacterUnitLeftIndent      = this.CharacterUnitLeftIndent;            // 复制到WORD.ParagraphFormat同名参数值
            dstParaFmt.CharacterUnitRightIndent     = this.CharacterUnitRightIndent;           // 复制到WORD.ParagraphFormat同名参数值
            dstParaFmt.DisableLineHeightGrid        = this.DisableLineHeightGrid;              // 复制到WORD.ParagraphFormat同名参数值
            dstParaFmt.FarEastLineBreakControl      = this.FarEastLineBreakControl;            // 复制到WORD.ParagraphFormat同名参数值
            dstParaFmt.FirstLineIndent = this.FirstLineIndent;                                 // 复制到WORD.ParagraphFormat同名参数值
            dstParaFmt.HalfWidthPunctuationOnTopOfLine = this.HalfWidthPunctuationOnTopOfLine; // 复制到WORD.ParagraphFormat同名参数值
            dstParaFmt.HangingPunctuation = this.HangingPunctuation;                           // 复制到WORD.ParagraphFormat同名参数值
            dstParaFmt.Hyphenation        = this.Hyphenation;                                  // 复制到WORD.ParagraphFormat同名参数值
            dstParaFmt.KeepTogether       = this.KeepTogether;                                 // 复制到WORD.ParagraphFormat同名参数值
            dstParaFmt.KeepWithNext       = this.KeepWithNext;                                 // 复制到WORD.ParagraphFormat同名参数值
            dstParaFmt.LeftIndent         = this.LeftIndent;                                   // 复制到WORD.ParagraphFormat同名参数值
            dstParaFmt.LineSpacingRule    = this.LineSpacingRule;                              // 复制到WORD.ParagraphFormat同名参数值
            dstParaFmt.LineSpacing        = this.LineSpacing;                                  // 复制到WORD.ParagraphFormat同名参数值
            dstParaFmt.LineUnitAfter      = this.LineUnitAfter;                                // 复制到WORD.ParagraphFormat同名参数值
            dstParaFmt.LineUnitBefore     = this.LineUnitBefore;                               // 复制到WORD.ParagraphFormat同名参数值
            dstParaFmt.MirrorIndents      = this.MirrorIndents;                                // 复制到WORD.ParagraphFormat同名参数值
            dstParaFmt.OutlineLevel       = this.OutlineLevel;                                 // 复制到WORD.ParagraphFormat同名参数值
            dstParaFmt.NoLineNumber       = this.NoLineNumber;                                 // 复制到WORD.ParagraphFormat同名参数值
            dstParaFmt.PageBreakBefore    = this.PageBreakBefore;                              // 复制到WORD.ParagraphFormat同名参数值
            dstParaFmt.ReadingOrder       = this.ReadingOrder;                                 // 复制到WORD.ParagraphFormat同名参数值
            dstParaFmt.RightIndent        = this.RightIndent;                                  // 复制到WORD.ParagraphFormat同名参数值

            // dstParaFmt.Shading = this.Shading;
            Word.Shading shd = dstParaFmt.Shading;// 复制到WORD.ParagraphFormat同名参数值
            //this.Shading.copy2(ref shd);

            dstParaFmt.SpaceAfter      = this.SpaceAfter;      // 复制到WORD.ParagraphFormat同名参数值
            dstParaFmt.SpaceAfterAuto  = this.SpaceAfterAuto;  // 复制到WORD.ParagraphFormat同名参数值
            dstParaFmt.SpaceBefore     = this.SpaceBefore;     // 复制到WORD.ParagraphFormat同名参数值
            dstParaFmt.SpaceBeforeAuto = this.SpaceBeforeAuto; // 复制到WORD.ParagraphFormat同名参数值

            // dstParaFmt.TabStops = this.TabStops;

            dstParaFmt.TextboxTightWrap = this.TextboxTightWrap; // 复制到WORD.ParagraphFormat同名参数值
            dstParaFmt.WidowControl     = this.WidowControl;     // 复制到WORD.ParagraphFormat同名参数值
            dstParaFmt.WordWrap         = this.WordWrap;         // 复制到WORD.ParagraphFormat同名参数值

            return;
        }
Exemplo n.º 3
0
        // 复制保存WORD.ParagraphFormat同名参数值
        public void clone(Word.ParagraphFormat srcParaFormat)
        {
            this.Alignment = srcParaFormat.Alignment;                         // 复制保存WORD.ParagraphFormat同名参数值

            this.AutoAdjustRightIndent = srcParaFormat.AutoAdjustRightIndent; // 复制保存WORD.ParagraphFormat同名参数值
            this.BaseLineAlignment     = srcParaFormat.BaseLineAlignment;     // 复制保存WORD.ParagraphFormat同名参数值

            //this.Borders.clone(srcParaFormat.Borders);// 复制保存WORD.ParagraphFormat同名参数值

            this.CharacterUnitFirstLineIndent = srcParaFormat.CharacterUnitFirstLineIndent;       // 复制保存WORD.ParagraphFormat同名参数值
            this.CharacterUnitLeftIndent      = srcParaFormat.CharacterUnitLeftIndent;            // 复制保存WORD.ParagraphFormat同名参数值
            this.CharacterUnitRightIndent     = srcParaFormat.CharacterUnitRightIndent;           // 复制保存WORD.ParagraphFormat同名参数值
            this.DisableLineHeightGrid        = srcParaFormat.DisableLineHeightGrid;              // 复制保存WORD.ParagraphFormat同名参数值
            this.FarEastLineBreakControl      = srcParaFormat.FarEastLineBreakControl;            // 复制保存WORD.ParagraphFormat同名参数值
            this.FirstLineIndent = srcParaFormat.FirstLineIndent;                                 // 复制保存WORD.ParagraphFormat同名参数值
            this.HalfWidthPunctuationOnTopOfLine = srcParaFormat.HalfWidthPunctuationOnTopOfLine; // 复制保存WORD.ParagraphFormat同名参数值
            this.HangingPunctuation = srcParaFormat.HangingPunctuation;                           // 复制保存WORD.ParagraphFormat同名参数值
            this.Hyphenation        = srcParaFormat.Hyphenation;                                  // 复制保存WORD.ParagraphFormat同名参数值
            this.KeepTogether       = srcParaFormat.KeepTogether;                                 // 复制保存WORD.ParagraphFormat同名参数值
            this.KeepWithNext       = srcParaFormat.KeepWithNext;                                 // 复制保存WORD.ParagraphFormat同名参数值
            this.LeftIndent         = srcParaFormat.LeftIndent;                                   // 复制保存WORD.ParagraphFormat同名参数值
            this.LineSpacing        = srcParaFormat.LineSpacing;                                  // 复制保存WORD.ParagraphFormat同名参数值
            this.LineSpacingRule    = srcParaFormat.LineSpacingRule;                              // 复制保存WORD.ParagraphFormat同名参数值
            this.LineUnitAfter      = srcParaFormat.LineUnitAfter;                                // 复制保存WORD.ParagraphFormat同名参数值
            this.LineUnitBefore     = srcParaFormat.LineUnitBefore;                               // 复制保存WORD.ParagraphFormat同名参数值
            this.MirrorIndents      = srcParaFormat.MirrorIndents;                                // 复制保存WORD.ParagraphFormat同名参数值
            this.OutlineLevel       = srcParaFormat.OutlineLevel;                                 // 复制保存WORD.ParagraphFormat同名参数值
            this.NoLineNumber       = srcParaFormat.NoLineNumber;                                 // 复制保存WORD.ParagraphFormat同名参数值
            this.PageBreakBefore    = srcParaFormat.PageBreakBefore;                              // 复制保存WORD.ParagraphFormat同名参数值
            this.ReadingOrder       = srcParaFormat.ReadingOrder;                                 // 复制保存WORD.ParagraphFormat同名参数值
            this.RightIndent        = srcParaFormat.RightIndent;                                  // 复制保存WORD.ParagraphFormat同名参数值

            // this.Shading = srcParaFormat.Shading;
            //this.Shading.clone(srcParaFormat.Shading);

            this.SpaceAfter      = srcParaFormat.SpaceAfter;      // 复制保存WORD.ParagraphFormat同名参数值
            this.SpaceAfterAuto  = srcParaFormat.SpaceAfterAuto;  // 复制保存WORD.ParagraphFormat同名参数值
            this.SpaceBefore     = srcParaFormat.SpaceBefore;     // 复制保存WORD.ParagraphFormat同名参数值
            this.SpaceBeforeAuto = srcParaFormat.SpaceBeforeAuto; // 复制保存WORD.ParagraphFormat同名参数值

            // this.TabStops = srcParaFormat.TabStops;

            this.TextboxTightWrap = srcParaFormat.TextboxTightWrap; // 复制保存WORD.ParagraphFormat同名参数值
            this.WidowControl     = srcParaFormat.WidowControl;     // 复制保存WORD.ParagraphFormat同名参数值
            this.WordWrap         = srcParaFormat.WordWrap;         // 复制保存WORD.ParagraphFormat同名参数值

            return;
        }
Exemplo n.º 4
0
        public void InsertPicture(string strFileName, MSWord.WdParagraphAlignment hAlign, string bookmark)
        {
            if (bookmark == "")
            {
                if (m_pDoc == null)
                {
                    m_pDoc = m_pApp.ActiveDocument;
                }
                if (m_pDoc == null)
                {
                    return;
                }

                m_pDoc.Select();
                m_pSelection = m_pApp.Selection;
                if (m_pSelection == null)
                {
                    return;
                }

                m_pSelection.Collapse(0);
            }
            else
            {
                m_pDoc.Select();
                m_pSelection = m_pApp.Selection;
                if (m_pSelection == null)
                {
                    return;
                }

                m_pSelection.GoTo(-1, Type.Missing, Type.Missing, bookmark);
            }

            MSWord.ParagraphFormat pParFormat = m_pSelection.ParagraphFormat;
            if (pParFormat == null)
            {
                return;
            }
            pParFormat.Alignment = hAlign;
            MSWord.InlineShapes shapes = m_pSelection.InlineShapes;
            if (shapes == null)
            {
                return;
            }
            shapes.AddPicture(strFileName, false, true);
        }
Exemplo n.º 5
0
        public void SetText(string strText, MSWord.WdParagraphAlignment hAlignment, FontParams font = null)
        {
            if (m_pSelection == null)
            {
                if (m_pDoc == null)
                {
                    m_pDoc = m_pApp.ActiveDocument;
                }
                if (m_pApp == null)
                {
                    return;
                }

                m_pDoc.Select();
                m_pSelection = m_pApp.Selection;
                if (m_pSelection == null)
                {
                    return;
                }
            }

            object varInfo = m_pSelection.get_Information(MSWord.WdInformation.wdWithInTable);

            if (Convert.ToBoolean(varInfo) == true)
            {
                m_pDoc.Select();
                m_pSelection = m_pApp.Selection;
                if (m_pSelection == null)
                {
                    return;
                }
                m_pSelection.Collapse(0);
            }

            MSWord.ParagraphFormat pf = m_pSelection.ParagraphFormat;
            pf.Alignment      = hAlignment;
            m_pSelection.Text = strText;
            if (font != null)
            {
                PutFont(font, m_pSelection);
            }

            m_pSelection.Collapse(0);
        }
Exemplo n.º 6
0
        public int SelCopy2(Word.ParagraphFormat wParaFmt)
        {
            if (setMembers == null)
            {
                return(0);
            }

            euMembers euItem = euMembers.Zero;

            foreach (int nItem in setMembers)
            {
                euItem = (euMembers)nItem;

                switch (euItem)
                {
                case euMembers.AddSpaceBetweenFarEastAndAlpha:
                    wParaFmt.AddSpaceBetweenFarEastAndAlpha = AddSpaceBetweenFarEastAndAlpha;
                    break;

                case euMembers.AddSpaceBetweenFarEastAndDigit:
                    wParaFmt.AddSpaceBetweenFarEastAndDigit = AddSpaceBetweenFarEastAndDigit;
                    break;

                case euMembers.Alignment:
                    wParaFmt.Alignment = Alignment;
                    break;

                case euMembers.AutoAdjustRightIndent:
                    wParaFmt.AutoAdjustRightIndent = AutoAdjustRightIndent;
                    break;

                case euMembers.BaseLineAlignment:
                    wParaFmt.BaseLineAlignment = BaseLineAlignment;
                    break;

                case euMembers.Borders:
                    // wParaFmt.Borders = Borders;
                    break;

                case euMembers.CharacterUnitFirstLineIndent:
                    wParaFmt.CharacterUnitFirstLineIndent = CharacterUnitFirstLineIndent;
                    break;

                case euMembers.CharacterUnitLeftIndent:
                    wParaFmt.CharacterUnitLeftIndent = CharacterUnitLeftIndent;
                    break;

                case euMembers.CharacterUnitRightIndent:
                    wParaFmt.CharacterUnitRightIndent = CharacterUnitRightIndent;
                    break;

                case euMembers.DisableLineHeightGrid:
                    wParaFmt.DisableLineHeightGrid = DisableLineHeightGrid;
                    break;

                case euMembers.Duplicate:
                    // wParaFmt.Duplicate = Duplicate;
                    break;

                case euMembers.FarEastLineBreakControl:
                    wParaFmt.FarEastLineBreakControl = FarEastLineBreakControl;
                    break;

                case euMembers.FirstLineIndent:
                    wParaFmt.FirstLineIndent = FirstLineIndent;
                    break;

                case euMembers.HalfWidthPunctuationOnTopOfLine:
                    wParaFmt.HalfWidthPunctuationOnTopOfLine = HalfWidthPunctuationOnTopOfLine;
                    break;

                case euMembers.HangingPunctuation:
                    wParaFmt.HangingPunctuation = HangingPunctuation;
                    break;

                case euMembers.Hyphenation:
                    wParaFmt.Hyphenation = Hyphenation;
                    break;

                case euMembers.KeepTogether:
                    wParaFmt.KeepTogether = KeepTogether;
                    break;

                case euMembers.KeepWithNext:
                    wParaFmt.KeepWithNext = KeepWithNext;
                    break;

                case euMembers.LeftIndent:
                    wParaFmt.LeftIndent = LeftIndent;
                    break;

                case euMembers.LineSpacing:
                    wParaFmt.LineSpacing = LineSpacing;
                    break;

                case euMembers.LineSpacingRule:
                    wParaFmt.LineSpacingRule = LineSpacingRule;
                    break;

                case euMembers.LineUnitAfter:
                    wParaFmt.LineUnitAfter = LineUnitAfter;
                    break;

                case euMembers.LineUnitBefore:
                    wParaFmt.LineUnitBefore = LineUnitBefore;
                    break;

                case euMembers.MirrorIndents:
                    wParaFmt.MirrorIndents = MirrorIndents;
                    break;

                case euMembers.NoLineNumber:
                    wParaFmt.NoLineNumber = NoLineNumber;
                    break;

                case euMembers.OutlineLevel:
                    wParaFmt.OutlineLevel = OutlineLevel;
                    break;

                case euMembers.PageBreakBefore:
                    wParaFmt.PageBreakBefore = PageBreakBefore;
                    break;

                case euMembers.ReadingOrder:
                    wParaFmt.ReadingOrder = ReadingOrder;
                    break;

                case euMembers.RightIndent:
                    wParaFmt.RightIndent = RightIndent;
                    break;

                case euMembers.Shading:
                    //wParaFmt.Shading = Shading;
                    break;

                case euMembers.SpaceAfter:
                    wParaFmt.SpaceAfter = SpaceAfter;
                    break;

                case euMembers.SpaceAfterAuto:
                    wParaFmt.SpaceAfterAuto = SpaceAfterAuto;
                    break;

                case euMembers.SpaceBefore:
                    wParaFmt.SpaceBefore = SpaceBefore;
                    break;

                case euMembers.SpaceBeforeAuto:
                    wParaFmt.SpaceBeforeAuto = SpaceBeforeAuto;
                    break;

                case euMembers.TabStops:
                    wParaFmt.TabStops = TabStops;
                    break;

                case euMembers.TextboxTightWrap:
                    wParaFmt.TextboxTightWrap = TextboxTightWrap;
                    break;

                case euMembers.WidowControl:
                    wParaFmt.WidowControl = WidowControl;
                    break;

                case euMembers.WordWrap:
                    wParaFmt.WordWrap = WordWrap;
                    break;

                default:
                    break;
                }
            }

            return(setMembers.Count);
        }
        private void CreateStyle_IndexHeading()
        {
            Common.WordHelper.DeleteStyle(cINDEX_HEADING_STYLE);

            Document activeDoc = Globals.ThisAddIn.Application.ActiveDocument;

            Style newStyle = activeDoc.Styles.Add(Name: cINDEX_HEADING_STYLE, Type: WdStyleType.wdStyleTypeParagraph);

            newStyle.AutomaticallyUpdate = false;
            newStyle.NoSpaceBetweenParagraphsOfSameStyle = false;
            newStyle.ParagraphFormat.TabStops.ClearAll();
            newStyle.LanguageID = WdLanguageID.wdEnglishUS;
            newStyle.NoProofing = 0;
            newStyle.Frame.Delete();

            //activeDoc.Styles[cINDEX_HEADING_SYTLE].AutomaticallyUpdate = false;

            Microsoft.Office.Interop.Word.Font font = activeDoc.Styles[cINDEX_HEADING_STYLE].Font;

            font.Name                 = "+Body";
            font.Size                 = 12;
            font.Italic               = 0;
            font.Underline            = WdUnderline.wdUnderlineNone;
            font.UnderlineColor       = WdColor.wdColorAutomatic;
            font.StrikeThrough        = 0;
            font.Outline              = 0;
            font.Emboss               = 0;
            font.Shadow               = 0;
            font.Hidden               = 0;
            font.SmallCaps            = 0;
            font.AllCaps              = 0;
            font.Color                = WdColor.wdColorBlue;
            font.Engrave              = 0;
            font.Subscript            = 0;
            font.Scaling              = 100;
            font.Kerning              = 0;
            font.Animation            = WdAnimation.wdAnimationNone;
            font.Ligatures            = WdLigatures.wdLigaturesNone;
            font.NumberSpacing        = WdNumberSpacing.wdNumberSpacingDefault;
            font.NumberForm           = WdNumberForm.wdNumberFormDefault;
            font.StylisticSet         = WdStylisticSet.wdStylisticSetDefault;
            font.ContextualAlternates = 0;

            Microsoft.Office.Interop.Word.ParagraphFormat paragraphFormat = activeDoc.Styles[cINDEX_HEADING_STYLE].ParagraphFormat;

            paragraphFormat.LeftIndent      = Globals.ThisAddIn.Application.InchesToPoints(0);
            paragraphFormat.RightIndent     = Globals.ThisAddIn.Application.InchesToPoints(0);
            paragraphFormat.SpaceBefore     = 0;
            paragraphFormat.SpaceBeforeAuto = 0;
            paragraphFormat.SpaceAfter      = 6;
            paragraphFormat.SpaceAfterAuto  = 0;
            paragraphFormat.LineSpacingRule = WdLineSpacing.wdLineSpaceMultiple;
            paragraphFormat.LineSpacing     = Globals.ThisAddIn.Application.LinesToPoints((float)1.15);
            paragraphFormat.Alignment       = WdParagraphAlignment.wdAlignParagraphCenter;
            // This throws exception.
            //paragraphFormat.WidowControl = 1;
            paragraphFormat.KeepTogether                 = 0;
            paragraphFormat.KeepWithNext                 = 0;
            paragraphFormat.PageBreakBefore              = 0;
            paragraphFormat.NoLineNumber                 = 0;
            paragraphFormat.Hyphenation                  = 0;
            paragraphFormat.FirstLineIndent              = Globals.ThisAddIn.Application.InchesToPoints(0);
            paragraphFormat.OutlineLevel                 = WdOutlineLevel.wdOutlineLevelBodyText;
            paragraphFormat.CharacterUnitLeftIndent      = 0;
            paragraphFormat.CharacterUnitRightIndent     = 0;
            paragraphFormat.CharacterUnitFirstLineIndent = 0;
            paragraphFormat.LineUnitBefore               = 0;
            paragraphFormat.LineUnitAfter                = 0;
            paragraphFormat.MirrorIndents                = 0;
            paragraphFormat.TextboxTightWrap             = WdTextboxTightWrap.wdTightNone;

            paragraphFormat.Shading.Texture = WdTextureIndex.wdTextureNone;
            paragraphFormat.Shading.ForegroundPatternColor = WdColor.wdColorAutomatic;
            paragraphFormat.Shading.BackgroundPatternColor = WdColor.wdColorAutomatic;

            paragraphFormat.Borders[WdBorderType.wdBorderLeft].LineStyle   = WdLineStyle.wdLineStyleNone;
            paragraphFormat.Borders[WdBorderType.wdBorderRight].LineStyle  = WdLineStyle.wdLineStyleNone;
            paragraphFormat.Borders[WdBorderType.wdBorderTop].LineStyle    = WdLineStyle.wdLineStyleNone;
            paragraphFormat.Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleNone;

            paragraphFormat.Borders.DistanceFromTop    = 1;
            paragraphFormat.Borders.DistanceFromLeft   = 4;
            paragraphFormat.Borders.DistanceFromBottom = 1;
            paragraphFormat.Borders.DistanceFromRight  = 4;
        }
        private static void AddMarkingHeaderToDocument(ProtectiveMarking marking, Word.Document document)
        {
            Debug.WriteLine("PspfMarkingsAddIn: AddMarkingHeaderToDocument");
            Debug.WriteLine("==============================================================================");

            Word.Range           range  = null;
            Word.Font            font   = null;
            Word.ParagraphFormat format = null;

            try
            {
                object start = 0;
                object end   = 0;

                // Move to start
                range = document.Range(ref start, ref end);

                // Insert Paragraph break
                range.InsertParagraphAfter();

                Marshal.ReleaseComObject(range);

                // Move to start
                range = document.Range(ref start, ref end);

                range.Text = marking.MailBodyHeaderText;

                font = range.Font;

                if (!string.IsNullOrWhiteSpace(marking.MailBodyHeaderColour))
                {
                    var colorConverter = new ColorConverter();

                    var color = (Color)colorConverter.ConvertFromString(marking.MailBodyHeaderColour);

                    font.Color = (Word.WdColor)(color.R + 0x100 * color.G + 0x10000 * color.B);
                }

                if (!string.IsNullOrWhiteSpace(marking.MailBodyHeaderSizePoints))
                {
                    font.Size = float.Parse(marking.MailBodyHeaderSizePoints);
                }

                if (!string.IsNullOrWhiteSpace(marking.MailBodyHeaderFont))
                {
                    font.Name = marking.MailBodyHeaderFont;
                }

                if (!string.IsNullOrWhiteSpace(marking.MailBodyHeaderAlign))
                {
                    format = range.ParagraphFormat;

                    switch (marking.MailBodyHeaderAlign.ToLowerInvariant())
                    {
                    case "center":
                    case "centre":
                    case "middle":
                        format.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
                        break;

                    case "left":
                    case "normal":
                        format.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
                        break;

                    case "right":
                        format.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
                        break;

                    default:
                        Debug.WriteLine("Unexpected text alignment: " + marking.MailBodyHeaderAlign);
                        break;
                    }
                }

                // Force the edit to persist:
                document.Activate();
            }
            finally
            {
                if (format != null)
                {
                    Marshal.ReleaseComObject(format);
                }

                if (font != null)
                {
                    Marshal.ReleaseComObject(font);
                }

                if (range != null)
                {
                    Marshal.ReleaseComObject(range);
                }
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// Process a paragraph, converting its contents into jasper reports XML
        /// </summary>
        /// <param name="paragraph">A word paragraph</param>
        public void ProcessParagraph(Word.Paragraph paragraph)
        {
            XmlElement band;
            String     text = paragraph.Range.Text;

            text = text.Substring(0, text.Length - 1);
            Word.Style           style      = paragraph.get_Style();
            Word.ParagraphFormat paraFormat = paragraph.Format;

            int spaceBefore = (int)paraFormat.SpaceBefore;
            int spaceAfter  = (int)paraFormat.SpaceAfter;
            int fontSize    = (int)style.Font.Size;
            int bandHeight  = spaceBefore + spaceAfter + fontSize;

            Debug.WriteLine("Processing paragraph: " + text);
            int parseFromChar = 0;

            if (text.StartsWith("$"))
            {
                String tagName, tagValue;
                int    nextChar;

                if (ParseTagText(text, out tagName, out tagValue, out nextChar))
                {
                    if (tagName.Equals("bandtype"))
                    {
                        switch (tagValue.ToLower())
                        {
                        case "background":
                            currentBandType = BandType.Background;
                            break;

                        case "title":
                            currentBandType = BandType.Title;
                            break;

                        case "pageheader":
                            currentBandType = BandType.PageHeader;
                            break;

                        case "columnheader":
                            currentBandType = BandType.ColumnHeader;
                            break;

                        case "detail":
                            currentBandType = BandType.Detail;
                            break;

                        case "columnfooter":
                            currentBandType = BandType.ColumnFooter;
                            break;

                        case "pagefooter":
                            currentBandType = BandType.PageFooter;
                            break;

                        case "lastpagefooter":
                            currentBandType = BandType.LastPageFooter;
                            break;

                        case "summary":
                            currentBandType = BandType.Summary;
                            break;

                        case "nodata":
                            currentBandType = BandType.NoData;
                            break;

                        default:
                            Debug.WriteLine("Invalid band type " + tagValue);
                            break;
                        }
                        Debug.WriteLine("Changed band type to " + currentBandType.ToString());
                        return;
                    }
                }
            }
            if (currentBandType == BandType.Detail)
            {
                // Add a new band for each paragraph
                band = jDoc.CreateElement("band");
                jDetail.AppendChild(band);

                // Set the band attributes
                // Set the height based upon the font size - it will stretch
                band.SetAttribute("height", fontSize.ToString());
                // Split type
                band.SetAttribute("splitType", "Stretch");
            }
            else
            {
                // There is only one band of these types, so all paragraphs have to fit
                XmlElement bandElement = GetBandElement(currentBandType);

                // If there is a child element, it will be the band. If not, create it
                if (bandElement.HasChildNodes)
                {
                    XmlNode node = bandElement.GetElementsByTagName("band")[0];
                    band = (XmlElement)node;
                }
                else
                {
                    // Add a new band covering all paragraphs
                    band = jDoc.CreateElement("band");
                    bandElement.AppendChild(band);
                    band.SetAttribute("height", bandHeight.ToString());
                    band.SetAttribute("splitType", "Stretch");
                }
            }

            // Add the text field, report element, text element, and text field expression
            XmlElement textField    = jDoc.CreateElement("textField");
            XmlElement reportElt    = jDoc.CreateElement("reportElement");
            XmlElement box          = jDoc.CreateElement("box");
            XmlElement textElt      = jDoc.CreateElement("textElement");
            XmlElement textEltParag = jDoc.CreateElement("paragraph");
            XmlElement textFont     = jDoc.CreateElement("font");
            XmlElement textFieldExp = jDoc.CreateElement("textFieldExpression");

            band.AppendChild(textField);
            textField.AppendChild(reportElt);
            textField.AppendChild(box);
            textField.AppendChild(textElt);
            textField.AppendChild(textFieldExp);
            textElt.AppendChild(textFont);
            textElt.AppendChild(textEltParag);

            textField.SetAttribute("isStretchWithOverflow", "true");
            textField.SetAttribute("isBlankWhenNull", "true");

            reportElt.SetAttribute("stretchType", "RelativeToBandHeight");
            reportElt.SetAttribute("x", "0");
            reportElt.SetAttribute("width", ((int)columnWidth).ToString());
            // TODO: If not detail band, then y and height need to be calculated
            // TODO: y position should consider the paragraph spacing before
            reportElt.SetAttribute("y", "0");
            reportElt.SetAttribute("height", fontSize.ToString());

            // Left and right indents - use the box, so we have flexibility to use the
            // first line indent with negative values for hanging indents.
            box.SetAttribute("leftPadding", ((int)paragraph.LeftIndent).ToString());
            box.SetAttribute("rightPadding", ((int)paragraph.RightIndent).ToString());
            textEltParag.SetAttribute("firstLineIndent", ((int)paragraph.FirstLineIndent).ToString());
            // Top and bottom - paragraph spacing before and after
            box.SetAttribute("topPadding", spaceBefore.ToString());
            box.SetAttribute("bottomPadding", spaceAfter.ToString());

            // Paragraph alignment
            {
                String alignment = "";
                switch (paragraph.Alignment)
                {
                case Word.WdParagraphAlignment.wdAlignParagraphLeft:
                    alignment = "Left";
                    break;

                case Word.WdParagraphAlignment.wdAlignParagraphRight:
                    alignment = "Right";
                    break;

                case Word.WdParagraphAlignment.wdAlignParagraphCenter:
                    alignment = "Center";
                    break;

                case Word.WdParagraphAlignment.wdAlignParagraphJustify:
                case Word.WdParagraphAlignment.wdAlignParagraphJustifyHi:
                case Word.WdParagraphAlignment.wdAlignParagraphJustifyMed:
                case Word.WdParagraphAlignment.wdAlignParagraphJustifyLow:
                case Word.WdParagraphAlignment.wdAlignParagraphDistribute:
                case Word.WdParagraphAlignment.wdAlignParagraphThaiJustify:
                    alignment = "Justified";
                    break;

                default:
                    break;
                }
                textElt.SetAttribute("textAlignment", alignment);
            }

            // Markup type
            textElt.SetAttribute("markup", "styled");

            // Get the format of the first character we are parsing. This becomes
            // our 'base' format.
            int textLength = text.Length;

            Word.Characters chars = paragraph.Range.Characters;

            Word.Range refRange = chars[parseFromChar + 1];

            textFont.SetAttribute("fontName", refRange.Font.Name);
            textFont.SetAttribute("size", refRange.Font.Size.ToString());
            reportElt.SetAttribute("forecolor", RGBHex(refRange.Font.TextColor.RGB));
            Word.Range lastRange = refRange;

            String openStyle, closeStyle;

            GetStyleTags(refRange, refRange, out openStyle, out closeStyle);
            StringBuilder jText = new StringBuilder(openStyle, 2048);

            for (int c = parseFromChar; c < textLength; c++)
            {
                // Current character
                String ch = text.Substring(c, 1);
                // Previous character
                String prevCh = text.Substring((c > 0 ? c - 1 : 0), 1);

                // Check if we are processing style changes
                if (c > parseFromChar && // Must be past the first character to parse
                    (
                        // Either every character
                        styleCheck == StyleCheck.CheckEveryCharacter ||
                        // Or every word boundary (whitespace, punctuation)
                        (styleCheck == StyleCheck.CheckEveryWordBoundary &&
                         (
                             Char.IsWhiteSpace(ch, 0) || Char.IsWhiteSpace(prevCh, 0) ||
                             Char.IsPunctuation(ch, 0) || Char.IsPunctuation(prevCh, 0) ||
                             Char.IsSeparator(ch, 0) || Char.IsSeparator(prevCh, 0)
                         )
                        )
                    )
                    )
                {
                    // Compare the style to refRange, and if style is different,
                    // close previous style tag and create a new one
                    Word.Range curRange = chars[c + 1];
                    if (!AreStylesEqual(lastRange, curRange))
                    {
                        // Close the previous style
                        jText.Append(closeStyle);
                        // Start the new style
                        GetStyleTags(curRange, refRange, out openStyle, out closeStyle);
                        jText.Append(openStyle);
                        lastRange = curRange;
                    }
                }

                // TODO: Cater for fields, etc
                if (ch == "<")
                {
                    ch = "&lt;";
                }
                else if (ch == ">")
                {
                    ch = "&gt;";
                }
                else if (ch == "&")
                {
                    ch = "&amp;";
                }
                else if (ch == "\"")
                {
                    ch = "&quot;";
                }
                else if (ch == "'")
                {
                    ch = "&apos;";
                }
                else if (ch == "\r" || ch == "\n" || ch == "\v")
                {
                    ch = "<br/>";
                }
                jText.Append(ch);
            }
            // Close the style
            jText.Append(closeStyle);

            XmlCDataSection styledText = jDoc.CreateCDataSection("\"" + jText + "\"");

            textFieldExp.AppendChild(styledText);
        }