public static string GetInformationAboutRichEditDocumentLayout(Document currentDocument, DocumentLayout currentDocumentLayout)
        {
            SubDocument      subDocument = currentDocument.CaretPosition.BeginUpdateDocument();
            DocumentPosition docPosition = subDocument.CreatePosition(currentDocument.CaretPosition.ToInt() == 0 ? 0 : currentDocument.CaretPosition.ToInt() - 1);

            ReadOnlyShapeCollection         shapes = subDocument.Shapes.Get(subDocument.CreateRange(docPosition, 1));
            ReadOnlyDocumentImageCollection images = subDocument.Images.Get(subDocument.CreateRange(docPosition, 1));

            if (shapes.Count == 0 && images.Count == 0)
            {
                docPosition = subDocument.CreatePosition(currentDocument.CaretPosition.ToInt());
            }

            string returnedInformation = "";

            // get infromation about a current document element
            returnedInformation += GetInformationAboutCurrentDocumentElement(currentDocument, currentDocumentLayout, subDocument, docPosition);

            // collect information about CURRENT PAGE
            RangedLayoutElement layoutPosition = currentDocumentLayout.GetElement <RangedLayoutElement>(docPosition);

            if (layoutPosition != null)
            {
                int currentPageIndex = currentDocumentLayout.GetPageIndex(layoutPosition);
                returnedInformation += PageLayoutHelper.GetInformationAboutCurrentPage(currentDocumentLayout, currentDocumentLayout.GetPage(currentPageIndex), docPosition);
            }

            currentDocument.CaretPosition.EndUpdateDocument(subDocument);

            return(returnedInformation);
        }
示例#2
0
        private void richEditControl1_MouseClick(object sender, MouseEventArgs e)
        {
            PageLayoutPosition pageLayoutPosition = richEditControl1.ActiveView.GetDocumentLayoutPosition(e.Location);

            if (pageLayoutPosition == null)
            {
                return;
            }
            int                   pageIndex  = pageLayoutPosition.PageIndex;
            Point                 point      = pageLayoutPosition.Position;
            LayoutPage            layoutPage = richEditControl1.DocumentLayout.GetPage(pageIndex);
            HitTestManager        hitTest    = new HitTestManager(richEditControl1.DocumentLayout);
            RichEditHitTestResult result     = hitTest.HitTest(layoutPage, point);

            if (result.LayoutElement is CharacterBox && richEditControl1.Document.Selection.Length == 0)
            {
                CharacterBox     character     = (CharacterBox)result.LayoutElement;
                DocumentPosition caretPosition = richEditControl1.Document.CaretPosition;
                SubDocument      document      = caretPosition.BeginUpdateDocument();
                if (document.GetSubDocumentType() == GetLocation(character.Parent))
                {
                    DocumentRange characterRange = document.CreateRange(character.Range.Start, 1);
                    UpdateCheckState(document, characterRange, character.Text);
                }
                caretPosition.EndUpdateDocument(document);
            }
        }
示例#3
0
        static void EditEndnote(Document document)
        {
            #region #EditEndnote
            document.LoadDocument("Documents//Grimm.docx");

            //Access the first endnote's content:
            SubDocument endnote = document.Endnotes[0].BeginUpdate();

            //Exclude the reference mark and the space after it from the range to be edited:
            DocumentRange noteTextRange = endnote.CreateRange(endnote.Range.Start.ToInt() + 2, endnote.Range.Length
                                                              - 2);

            //Access the range's character properties:
            CharacterProperties characterProperties = endnote.BeginUpdateCharacters(noteTextRange);

            characterProperties.ForeColor = System.Drawing.Color.Red;
            characterProperties.Italic    = true;

            //Finalize the character options update:
            endnote.EndUpdateCharacters(characterProperties);

            //Finalize the endnote update:
            document.Endnotes[0].EndUpdate(endnote);
            #endregion #EditEndnote
        }
        private void SaveSelectedRange()
        {
            DocumentRange selection   = richEditControl.Document.Selection;
            SubDocument   subDocument = selection.BeginUpdateDocument();

            sourceSelectedRange = subDocument.CreateRange(selection.Start, richEditControl.Document.Selection.Length);
            selection.EndUpdateDocument(subDocument);
        }
        static void buttonCustomAction_ItemClick_Replace(object sender, ItemClickEventArgs e)
        {
            RichEditControl richEdit  = e.Item.Tag as RichEditControl;
            DocumentRange   range     = richEdit.Document.Selection;
            int             selLength = range.Length;
            string          s         = new String('*', selLength);
            SubDocument     doc       = range.BeginUpdateDocument();

            doc.InsertSingleLineText(range.Start, s);
            DocumentRange rangeToRemove = doc.CreateRange(range.Start, selLength);

            doc.Delete(rangeToRemove);
            range.EndUpdateDocument(doc);
        }
        private void UpdateInfoAndSelection()
        {
            LayoutElement element = layoutIterator.Current;

            infoElement.Caption = String.Empty;
            if (element != null)
            {
                RangedLayoutElement rangedElement = element as RangedLayoutElement;
                infoElement.Caption = element.Type.ToString();
                if (rangedElement != null)
                {
                    DocumentRange r = doc.CreateRange(rangedElement.Range.Start, rangedElement.Range.Length);
                    richEditControl1.Document.ChangeActiveDocument(doc);
                    richEditControl1.Document.Selection = r;
                }
            }
        }
        private static void AppendFooter(Section sourceSection, Section targetSection, HeaderFooterType headerFooterType)
        {
            if (!sourceSection.HasFooter(headerFooterType))
            {
                return;
            }

            SubDocument source = sourceSection.BeginUpdateFooter(headerFooterType);
            SubDocument target = targetSection.BeginUpdateFooter(headerFooterType);

            target.Delete(target.Range);
            target.InsertDocumentContent(target.Range.Start, source.Range, InsertOptions.KeepSourceFormatting);

            // Delete empty paragraphs
            DocumentRange emptyParagraph = target.CreateRange(target.Range.End.ToInt() - 1, 1);

            target.Delete(emptyParagraph);

            sourceSection.EndUpdateFooter(source);
            targetSection.EndUpdateFooter(target);
        }
示例#8
0
        static void EditFootnote(Document document)
        {
            #region #EditFootnote
            document.LoadDocument("Documents//Grimm.docx");

            //Access the first fottnote's content:
            SubDocument footnote = document.Footnotes[0].BeginUpdate();

            //Exclude the reference mark and the space after it from the range to be edited:
            DocumentRange noteTextRange = footnote.CreateRange(footnote.Range.Start.ToInt() + 2, footnote.Range.Length
                                                               - 2);

            //Clear the range:
            footnote.Delete(noteTextRange);

            //Append a new text:
            footnote.AppendText("the text is removed");

            //Finalize the update:
            document.Footnotes[0].EndUpdate(footnote);
            #endregion #EditFootnote
        }
        void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (!(e.Node.IsSelected) || e.Node.Tag == null)
            {
                return;
            }
            // Rebuild the tree if required.
            rebuild = true;

            richEditControl1.Document.ChangeActiveDocument(richEditControl1.Document);
            LayoutElement       element       = layoutTreeDictionary[e.Node];
            RangedLayoutElement rangedElement = element as RangedLayoutElement;

            // Select a range or scroll to a document position, according to the type of the layout element.
            if ((ContentDisplayAction)e.Node.Tag == ContentDisplayAction.ScrollTo)
            {
                LayoutPage     page            = element.GetParentByType <LayoutPage>();
                LayoutPageArea nearestPageArea = page.PageAreas[0];

                if ((element.Type == LayoutType.Header) || (element.GetParentByType <LayoutHeader>() != null))
                {
                    ScrollToPosition(nearestPageArea.Range.Start);
                }
                if ((element.Type == LayoutType.Footer) || (element.GetParentByType <LayoutFooter>() != null))
                {
                    ScrollToPosition(nearestPageArea.Range.Start + nearestPageArea.Range.Length);
                }
                LayoutFloatingObject layoutFloatingObject = element as LayoutFloatingObject;
                if (layoutFloatingObject != null)
                {
                    FloatingObjectAnchorBox anchor = layoutFloatingObject.AnchorBox;
                    ScrollToPosition(anchor.Range.Start);
                    richEditControl1.Document.Selection = richEditControl1.Document.CreateRange(anchor.Range.Start, anchor.Range.Length);
                }
                LayoutComment layoutComment = element as LayoutComment;
                if (layoutComment != null)
                {
                    Comment comment = layoutComment.GetDocumentComment();
                    ScrollToPosition(comment.Range.Start.ToInt());
                }

                LayoutTextBox textBox = element.GetParentByType <LayoutTextBox>();
                if (textBox != null)
                {
                    // Do not rebuild the tree.
                    rebuild = false;
                    ScrollToPosition(textBox.AnchorBox.Range.Start);
                    SubDocument doc = richEditControl1.DocumentLayout.BeginUpdateDocument(textBox);
                    richEditControl1.Document.ChangeActiveDocument(doc);
                    richEditControl1.Document.Selection = doc.CreateRange(rangedElement.Range.Start, rangedElement.Range.Length);
                    richEditControl1.DocumentLayout.EndUpdateDocument(doc);
                }
            }
            else
            {
                if (rangedElement == null)
                {
                    return;
                }
                ScrollToPosition(rangedElement.Range.Start);
                richEditControl1.Document.Selection = richEditControl1.Document.CreateRange(rangedElement.Range.Start, rangedElement.Range.Length);
            }
        }
        private void richEditControl1_MouseMove(object sender, MouseEventArgs e)
        {
            Point            docPoint = Units.PixelsToDocuments(e.Location, richEditControl1.DpiX, richEditControl1.DpiY);
            DocumentPosition pos      = richEditControl1.GetPositionFromPoint(docPoint);

            if (pos == null)
            {
                return;
            }

            DocumentPosition start = pos;
            DocumentPosition end   = pos;

            SubDocument doc = richEditControl1.Document.CaretPosition.BeginUpdateDocument();

            for (int i = 0; i < doc.Range.End.ToInt(); i++)
            {
                if (start.ToInt() - i < 0)
                {
                    start = doc.Range.Start;
                    break;
                }

                DocumentRange range = doc.CreateRange(start.ToInt() - i, i + 1);
                string        str   = doc.GetText(range);

                if (ContainsPunctuationOrWhiteSpace(str))
                {
                    start = doc.CreatePosition(range.Start.ToInt() + 1);
                    break;
                }
            }

            for (int i = 0; i < doc.Range.End.ToInt(); i++)
            {
                if (end.ToInt() + i > doc.Range.End.ToInt())
                {
                    end = doc.Range.End;
                    break;
                }

                DocumentRange range = doc.CreateRange(end.ToInt(), i + 1);
                string        str   = doc.GetText(range);

                if (ContainsPunctuationOrWhiteSpace(str))
                {
                    end = doc.CreatePosition(range.End.ToInt() - 1);
                    break;
                }
            }

            if (end.ToInt() < start.ToInt())
            {
                return;
            }

            DocumentRange wordRange = doc.CreateRange(start, end.ToInt() - start.ToInt());
            string        word      = doc.GetText(wordRange);

            richEditControl1.Document.CaretPosition.EndUpdateDocument(doc);

            ShowToolTip(word);
        }