コード例 #1
0
        public void ReplaceHyperlinks()
        {
            //ExStart:ReplaceHyperlinks
            Document doc = new Document(MyDir + "Hyperlinks.docx");

            foreach (Field field in doc.Range.Fields)
            {
                if (field.Type == FieldType.FieldHyperlink)
                {
                    FieldHyperlink hyperlink = (FieldHyperlink)field;

                    // Some hyperlinks can be local (links to bookmarks inside the document), ignore these.
                    if (hyperlink.SubAddress != null)
                    {
                        continue;
                    }

                    hyperlink.Address = "http://www.aspose.com";
                    hyperlink.Result  = "Aspose - The .NET & Java Component Publisher";
                }
            }

            doc.Save(ArtifactsDir + "WorkingWithFields.ReplaceHyperlinks.docx");
            //ExEnd:ReplaceHyperlinks
        }
コード例 #2
0
        public static void Run()
        {
            //ExStart:ReplaceHyperlinks
            // The path to the documents directory.
            string   dataDir = RunExamples.GetDataDir_WorkingWithHyperlink();
            string   NewUrl  = @"http://www.aspose.com";
            string   NewName = "Aspose - The .NET & Java Component Publisher";
            Document doc     = new Document(dataDir + "ReplaceHyperlinks.doc");

            // Hyperlinks in a Word documents are fields.
            foreach (Field field in doc.Range.Fields)
            {
                if (field.Type == FieldType.FieldHyperlink)
                {
                    FieldHyperlink hyperlink = (FieldHyperlink)field;

                    // Some hyperlinks can be local (links to bookmarks inside the document), ignore these.
                    if (hyperlink.SubAddress != null)
                    {
                        continue;
                    }

                    hyperlink.Address = NewUrl;
                    hyperlink.Result  = NewName;
                }
            }

            dataDir = dataDir + "ReplaceHyperlinks_out_.doc";
            doc.Save(dataDir);
            //ExEnd:ReplaceHyperlinks
            Console.WriteLine("\nHyperlinks replaced successfully.\nFile saved at " + dataDir);
        }
コード例 #3
0
        public static void Run()
        {
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir_WorkingWithDocument();

            string   fileName = "TOC.doc";
            Document doc      = new Document(dataDir + fileName);

            foreach (Field field in doc.Range.Fields)
            {
                if (field.Type.Equals(Aspose.Words.Fields.FieldType.FieldHyperlink))
                {
                    FieldHyperlink hyperlink = (FieldHyperlink)field;
                    if (hyperlink.SubAddress != null && hyperlink.SubAddress.StartsWith("_Toc"))
                    {
                        Paragraph tocItem = (Paragraph)field.Start.GetAncestor(NodeType.Paragraph);
                        Console.WriteLine(tocItem.ToString(SaveFormat.Text).Trim());
                        Console.WriteLine("------------------");
                        if (tocItem != null)
                        {
                            Bookmark bm = doc.Range.Bookmarks[hyperlink.SubAddress];
                            // Get the location this TOC Item is pointing to
                            Paragraph pointer = (Paragraph)bm.BookmarkStart.GetAncestor(NodeType.Paragraph);
                            Console.WriteLine(pointer.ToString(SaveFormat.Text));
                        }
                    } // End If
                }     // End If
            }         // End Foreach
        }
コード例 #4
0
        public void ExtractTableOfContents()
        {
            Document doc = new Document(MyDir + "Table of contents.docx");

            foreach (Field field in doc.Range.Fields)
            {
                if (field.Type.Equals(FieldType.FieldHyperlink))
                {
                    FieldHyperlink hyperlink = (FieldHyperlink)field;
                    if (hyperlink.SubAddress != null && hyperlink.SubAddress.StartsWith("_Toc"))
                    {
                        Paragraph tocItem = (Paragraph)field.Start.GetAncestor(NodeType.Paragraph);

                        Console.WriteLine(tocItem.ToString(SaveFormat.Text).Trim());
                        Console.WriteLine("------------------");

                        Bookmark  bm      = doc.Range.Bookmarks[hyperlink.SubAddress];
                        Paragraph pointer = (Paragraph)bm.BookmarkStart.GetAncestor(NodeType.Paragraph);

                        Console.WriteLine(pointer.ToString(SaveFormat.Text));
                    }
                }
            }
        }