Example #1
0
        private void SaveExistingHighlight(Word.Range range)
        {
            //go through every character
            //since the whole word might not be highlighted
            Word.Characters characters     = range.Characters;
            int             chCt           = characters.Count;
            int             chCtUpperBound = chCt + 1;
            int             chCtLowerBound = 1;

            for (int i = chCtLowerBound; i < chCtUpperBound; i++)
            {
                Word.Range r = characters[i];
                if (r.HighlightColorIndex != Colors.none)
                {
                    SavedHighlights.Add(Tuple.Create(r, r.HighlightColorIndex));
                }
            }
        }
Example #2
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);
        }