Esempio n. 1
0
        private int CopyParagraph(Document output, Paragraph input, string styleName)
        {
            // select end of document.
            Section   section      = output.Sections[output.Sections.Count];
            Range     range        = output.Range(section.Range.End - 1);
            Paragraph newParagraph = null;

            var links = CollectLinks(input.Range);
            var terms = CollectTerms(input.Range);

            if (links.Count > 0 && links[0].Type == LinkType.Figure)
            {
                newParagraph = output.Paragraphs.Add(range);

                var text = input.Range.Text.Trim();

                string prefix = null;
                string number = $"{links[0].Target}";

                int index = text.IndexOf(number);

                if (index >= 0)
                {
                    prefix = text.Substring(0, index);
                    text   = text.Substring(index + number.Length + 1);
                }

                range.InsertCaption(WdCaptionLabelID.wdCaptionFigure, $" {text}", ExcludeLabel: false);
                range.set_Style("FIGURE-title");
                range.InsertParagraphAfter();

                Range titleRange = range.Words[2];
                titleRange.Text = prefix;

                return(input.Range.End);
            }

            foreach (InlineShape ii in input.Range.InlineShapes)
            {
                newParagraph = output.Paragraphs.Add(range);
                var previous = newParagraph.Previous(1).Range.Text;
                range.set_Style(styleName);

                if (ii.Type == WdInlineShapeType.wdInlineShapeEmbeddedOLEObject)
                {
                    ii.Range.Copy();
                    newParagraph.Range.Paste();
                }
                else
                {
                    Range shapeRange = input.Range.Document.Range(ii.Range.Start - 1, ii.Range.End);
                    shapeRange.Copy();
                    newParagraph.Range.Paste();
                }

                newParagraph.Range.set_Style("FIGURE");
                output.Range(output.Content.End - 1).Copy();

                return(input.Range.End);
            }

            if (links.Count > 0 && links[0].Type == LinkType.Table)
            {
                var text = input.Range.Text.Trim();

                string prefix = null;
                string number = $"{links[0].Target}";

                int index = text.IndexOf(number);

                if (index >= 0)
                {
                    prefix = text.Substring(0, index);
                    text   = text.Substring(index + number.Length + 1);
                }

                TableData data = new TableData();
                data.Title = text;

                range.InsertCaption(WdCaptionLabelID.wdCaptionTable, $" {text}", ExcludeLabel: false);
                range.set_Style("TABLE-title");

                Range titleRange = range.Words[1];
                titleRange.Text = prefix;

                input = input.Next(1);

                while (input.Range.Tables.Count == 0)
                {
                    input = input.Next(1);
                }

                range = output.Range(range.End - 1);
                range.set_Style("No Spacing");

                foreach (Table table in input.Range.Tables)
                {
                    Table newTable = output.Tables.Add(range, table.Rows.Count, table.Columns.Count);

                    newTable.Borders[WdBorderType.wdBorderRight].LineStyle      = WdLineStyle.wdLineStyleSingle;
                    newTable.Borders[WdBorderType.wdBorderLeft].LineStyle       = WdLineStyle.wdLineStyleSingle;
                    newTable.Borders[WdBorderType.wdBorderTop].LineStyle        = WdLineStyle.wdLineStyleSingle;
                    newTable.Borders[WdBorderType.wdBorderBottom].LineStyle     = WdLineStyle.wdLineStyleSingle;
                    newTable.Borders[WdBorderType.wdBorderHorizontal].LineStyle = WdLineStyle.wdLineStyleSingle;
                    newTable.Borders[WdBorderType.wdBorderVertical].LineStyle   = WdLineStyle.wdLineStyleSingle;

                    for (int ii = 1; ii <= table.Rows.Count; ii++)
                    {
                        var row    = table.Rows[ii];
                        var newRow = newTable.Rows[ii];

                        if (ii == 1)
                        {
                            newRow.Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleDouble;
                        }

                        for (int jj = 1; jj <= table.Columns.Count; jj++)
                        {
                            if (jj <= row.Cells.Count)
                            {
                                Cell cell    = row.Cells[jj];
                                var  newCell = newRow.Cells[jj];

                                links = CollectLinks(cell.Range);
                                terms = CollectTerms(cell.Range);

                                Paragraph contents = output.Paragraphs.Add(newCell.Range);
                                UpdateParagraph(output, contents, cell.Range.Text, terms, links);

                                if (ii == 1)
                                {
                                    data.AddHeading(cell);
                                    cell.Range.Bold = -1;
                                }
                                else
                                {
                                    data.SetCell(ii - 1, jj - 1, cell);
                                }
                            }
                            else
                            {
                                var       newCell  = newRow.Cells[jj];
                                Paragraph contents = output.Paragraphs.Add(newCell.Range);
                            }
                        }

                        for (int jj = newRow.Cells.Count; jj > row.Cells.Count; jj--)
                        {
                            newRow.Cells[jj - 1].Merge(newRow.Cells[jj]);
                        }

                        string cellStyle = (ii == 1) ? "TABLE-col-heading" : "TABLE-cell";

                        for (int jj = 1; jj <= newRow.Cells.Count; jj++)
                        {
                            string cellText = newRow.Cells[jj].Range.Text;

                            for (int kk = cellText.Length - 1; kk >= 0; kk--)
                            {
                                if (cellText[kk] == '\a')
                                {
                                    continue;
                                }

                                if (cellText[kk] != '\r')
                                {
                                    cellText  = cellText.Substring(0, kk + 1);
                                    cellText += '\a';
                                    break;
                                }

                                if (kk == 0)
                                {
                                    cellText = "\a";
                                    break;
                                }
                            }

                            newRow.Cells[jj].Range.Text = cellText;
                            newRow.Cells[jj].Range.set_Style(cellStyle);
                        }
                    }

                    range        = output.Range(section.Range.End - 1);
                    newParagraph = output.Paragraphs.Add(range);
                    range.set_Style("No Spacing");

                    Paragraph next = input.Next(1);

                    while (next.Range.End <= table.Range.End)
                    {
                        next = next.Next(1);
                    }

                    return(next.Range.End);
                }
            }

            newParagraph = output.Paragraphs.Add(range);
            UpdateParagraph(output, newParagraph, input.Range.Text, terms, links);
            range.set_Style(styleName);

            if (String.IsNullOrWhiteSpace(input.Range.Text))
            {
                return(input.Range.End);
            }

            int offset = 0;

            foreach (Bookmark ii in input.Range.Bookmarks)
            {
                var name = ii.Name;
                var text = ii.Range.Text;

                int index = newParagraph.Range.Text.IndexOf(text, offset);

                if (index >= 0)
                {
                    Range wordRange = output.Range(newParagraph.Range.Start + index, newParagraph.Range.Start + index + text.Length);
                    range.Bookmarks.Add(name, wordRange);
                    range.set_Style("ReferenceText");
                }
            }

            Style priorStyle = newParagraph.Previous(1).get_Style();

            if (priorStyle.NameLocal == "ReferenceText")
            {
                range.set_Style("ReferenceLink");
            }

            if (input.Range.ListFormat.ListType == WdListType.wdListBullet)
            {
                if (priorStyle.NameLocal == "TERM-note")
                {
                    range.set_Style("TERM-note");
                }

                range.ListFormat.ApplyBulletDefault();
            }

            if (m_sectionType == SectionType.Abbreviations)
            {
                range.set_Style("PARAGRAPH-Compressed");
            }

            range.InsertParagraphAfter();

            return(input.Range.End);
        }