private static bool IsTitleParagraph(Word.Paragraph para)
        {
            var style = para.get_Style() as Word.Style;

            if ((string.Equals(style.NameLocal, TitleDocumentStyleName, StringComparison.OrdinalIgnoreCase)) &&
                style.BuiltIn == true)
            {
                return(true);
            }

            return(false);
        }
Beispiel #2
0
        private void FormatNormalParagraphs(string DocToFormat)
        {
            var application = new Word.Application();

            Word.Document document = application.Documents.Open(DocToFormat);

            int PCount = document.Content.Paragraphs.Count;

            if (PCount > 0)
            {
                for (int i = 1; i < document.Content.Paragraphs.Count; i++)
                {
                    Word.Paragraph para  = document.Content.Paragraphs[i];
                    Word.Style     style = para.get_Style() as Word.Style;
                    Word.Range     range = para.Range;

                    lblProcessing.Text = "Processing All Paragraphs...";
                    //document.RemoveNumbers();

                    if (style.NameLocal == "Normal")
                    {
                        {
                            para.Range.Font.Color = Word.WdColor.wdColorBlack;
                            para.Range.Font.Size  = 11;
                            para.Range.Font.Name  = "Arial";
                            para.Range.Bold       = 0;

                            para.Range.Paragraphs.LeftIndent      = 1;
                            para.Range.Paragraphs.RightIndent     = 0;
                            para.Range.Paragraphs.OutlineLevel    = Word.WdOutlineLevel.wdOutlineLevelBodyText;
                            para.Range.Paragraphs.Alignment       = Word.WdParagraphAlignment.wdAlignParagraphJustify;
                            para.Range.Paragraphs.SpaceAfter      = 10;
                            para.Range.Paragraphs.SpaceBefore     = 0;
                            para.Range.Paragraphs.LineSpacingRule = Word.WdLineSpacing.wdLineSpaceExactly;
                        }
                    }
                }
            }
            document.Save();
            document.Close();
            application.Quit();
        }
Beispiel #3
0
        private void FormatParagraphHeadings_LibertyBrand(string DocToFormat)
        {
            var application = new Word.Application();

            Word.Document document = application.Documents.Open(DocToFormat);

            try
            {
                int PCount = document.Content.Paragraphs.Count;

                if (PCount > 0)
                {
                    for (int i = 1; i < document.Content.Paragraphs.Count; i++)
                    {
                        Word.Paragraph para  = document.Content.Paragraphs[i];
                        Word.Style     style = para.get_Style() as Word.Style;
                        lblProcessing.Text = "Processing All Paragraphs...";

                        if (style.NameLocal == "Heading 1")
                        {
                            para.Range.Font.Color = Word.WdColor.wdColorDarkTeal;
                            para.Range.Font.Size  = 12;
                            para.Range.Font.Name  = "Arial";
                            para.Range.Font.Bold  = 1;

                            //para.Range.Paragraphs.KeepWithNext = 1;
                            para.Range.Paragraphs.LeftIndent      = 0;
                            para.Range.Paragraphs.RightIndent     = 0;
                            para.Range.Paragraphs.OutlineLevel    = Word.WdOutlineLevel.wdOutlineLevel1;
                            para.Range.Paragraphs.Alignment       = Word.WdParagraphAlignment.wdAlignParagraphJustify;
                            para.Range.Paragraphs.SpaceAfter      = 12;
                            para.Range.Paragraphs.SpaceBefore     = 0;
                            para.Range.Paragraphs.LineSpacing     = 14;
                            para.Range.Paragraphs.LineSpacingRule = Word.WdLineSpacing.wdLineSpaceExactly;
                        }
                        if (style.NameLocal == "Heading 2")
                        {
                            para.Range.Font.Color = Word.WdColor.wdColorBlueGray;
                            para.Range.Font.Size  = 11;
                            para.Range.Font.Name  = "Arial";
                            para.Range.Font.Bold  = 1;
                            para.Alignment        = Word.WdParagraphAlignment.wdAlignParagraphJustify;
                        }

                        if (style.NameLocal == "Heading 3")
                        {
                            para.Range.Font.Color = Word.WdColor.wdColorBlueGray;
                            para.Range.Font.Size  = 11;
                            para.Range.Font.Name  = "Arial";
                            para.Range.Font.Bold  = 1;
                            para.Alignment        = Word.WdParagraphAlignment.wdAlignParagraphJustify;
                        }

                        if (style.NameLocal == "Heading 4")
                        {
                            para.Range.Font.Color = Word.WdColor.wdColorBlueGray;
                            para.Range.Font.Size  = 11;
                            para.Range.Font.Name  = "Arial";
                            para.Range.Font.Bold  = 1;
                            para.Alignment        = Word.WdParagraphAlignment.wdAlignParagraphJustify;
                        }



                        if (style.NameLocal == "Book Title")
                        {
                            para.Range.Font.Color = Word.WdColor.wdColorBlueGray;
                            para.Range.Font.Size  = 11;
                            para.Range.Font.Name  = "Arial";
                            para.Range.Bold       = 0;
                            para.Range.Italic     = 0;
                            para.LineSpacing      = 14;
                            para.SpaceAfter       = 18;
                            para.SpaceBefore      = 24;


                            //para.Range.Paragraphs.SpaceAfter = 18;
                            //para.Range.Paragraphs.SpaceBefore = 24;
                            //para.Range.Paragraphs.LineSpacing = 14;
                            //para.Range.Paragraphs.LineSpacingRule = Word.WdLineSpacing.wdLineSpaceDouble;
                        }
                    }

                    document.Save();
                    document.Close();
                    application.Quit();
                }
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message.ToString());
                document.Close();
                application.Quit();
            }
        }
Beispiel #4
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);
        }