For a Word document that contains more hyperlinks, if you delete them very often, this article provides a way to delete the hyperlinks in Word in bulk. Hyperlinks here can be hyperlinks at the header footer, hyperlinks in the body, hyperlinks in tables, text hyperlinks, picture hyperlinks, and so on. Here's how to do the code operation.
The tools you need
- Free Spire.doc for. NET 6.3 (Freeware edition)
PS: After downloading the installation, note that the Add Reference Spire.doc.dll,dll file in the project program can be obtained in the Bin folder under the installation path.
Test hyperlinks in a document, such as:
C # code sample (for reference)
Step 1: Add a using directive
using Spire.doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using Spire.Doc.Formatting; using System.Drawing;
Step 2: Load the file
New Document ();d ocument. LoadFromFile ("sample.docx");
Step 3: Traverse the document, delete the hyperlink
foreach (Section sections in document. Sections) { // delete hyperlinks in body foreach (documentobject obj in section. body.childobjects) {removelinks (obj, document); // delete hyperlink in header footer foreach (HeaderFooter hf in Section. HeadersFooters) { foreach (documentobject hfobj in HF. childobjects) {removelinks (hfobj, document); } }}
PS: Here you need to customize two ways to delete text hyperlinks in paragraphs and delete picture hyperlinks in paragraphs
Custom Method 1:
Private Static voidremovelinks (documentobject obj,document Document) {//Delete a hyperlink in a paragraphRemovelinksinpara (obj,document); //Delete a hyperlink in a table if(obj. Documentobjecttype = =documentobjecttype.table) {foreach(TableRow rowinch(obj asTable). Rows) {foreach(TableCell cellinchrow. Cells) {foreach(DocumentObject CObjinchcell. childobjects) {Removelinksinpara (cobj,document); } } } } }
Custom Method 2:
Private Static voidRemovelinksinpara (documentobject obj,document Document) {//Traverse all child objects in a document paragraph if(obj. Documentobjecttype = =documentobjecttype.paragraph) {varObjs = (obj asParagraph). childobjects; for(inti =0; I < Objs. Count; i++) { if(Objs[i]. Documentobjecttype = =Documentobjecttype.field) {//Get Hyperlink fieldField field = Objs[i] asField; if(field. Type = =fieldtype.fieldhyperlink) {//gets the text or picture object of the hyperlinkDocumentObject dobj = field. Nextsibling.nextsibling asDocumentObject; //Delete text hyperlinks, keep text and styles if(Dobj isTextRange) { //Get hyperlink Text styleCharacterformat format = (dobj asTextRange). Characterformat; Format. Underlinestyle=Underlinestyle.none; Format. TextColor=Color.Black; //Create a TextRange and assign the text of the hyperlink to itTextRange TR =NewTextRange (document); Tr. Text=field. Fieldtext; //Apply Stylestr. Applycharacterformat (format); //Delete a text hyperlink fieldObjs. RemoveAt (i); //re-insert textObjs. Insert (i, TR); } //Delete picture hyperlinks, keep pictures if(Dobj isdocpicture) { //Delete a picture hyperlink fieldObjs. RemoveAt (i); //re-insert PictureObjs. Insert (i, dobj); } } } } } }
Step 4: Save the document
Document. SaveToFile ("removelinks.docx", Fileformat.docx); System.Diagnostics.Process.Start ("removelinks.docx");
Debug run program, generate document (e.g.). The original hyperlinks are all deleted in the generated document.
All code:
usingSpire.doc;usingSpire.Doc.Documents;usingSpire.Doc.Fields;usingSpire.Doc.Formatting;usingSystem.Drawing;namespaceremovehyperlink_doc{classProgram {Static voidMain (string[] args) { //creating a Word object and loading a documentDocument document =NewDocument (); Document. LoadFromFile ("Sample.docx"); foreach(Section sectioninchdocument. Sections) {//Delete a hyperlink in the body foreach(DocumentObject objinchSection . body.childobjects) {removelinks (obj, document); } //Delete a hyperlink in a header footer foreach(HeaderFooter HFinchSection . HeadersFooters) {foreach(DocumentObject hfobjinchHF. childobjects) {removelinks (hfobj, document); } } } //Save DocumentDocument. SaveToFile ("Removelinks.docx", Fileformat.docx); System.Diagnostics.Process.Start ("Removelinks.docx"); } Private Static voidremovelinks (documentobject obj,document Document) {//Delete a hyperlink in a paragraphRemovelinksinpara (obj,document); //Delete a hyperlink in a table if(obj. Documentobjecttype = =documentobjecttype.table) {foreach(TableRow rowinch(obj asTable). Rows) {foreach(TableCell cellinchrow. Cells) {foreach(DocumentObject CObjinchcell. childobjects) {Removelinksinpara (cobj,document); } } } } } Private Static voidRemovelinksinpara (documentobject obj,document Document) {//Traverse all child objects in a document paragraph if(obj. Documentobjecttype = =documentobjecttype.paragraph) {varObjs = (obj asParagraph). childobjects; for(inti =0; I < Objs. Count; i++) { if(Objs[i]. Documentobjecttype = =Documentobjecttype.field) {//Get Hyperlink fieldField field = Objs[i] asField; if(field. Type = =fieldtype.fieldhyperlink) {//gets the text or picture object of the hyperlinkDocumentObject dobj = field. Nextsibling.nextsibling asDocumentObject; //Delete text hyperlinks, keep text and styles if(Dobj isTextRange) { //Get hyperlink Text styleCharacterformat format = (dobj asTextRange). Characterformat; Format. Underlinestyle=Underlinestyle.none; Format. TextColor=Color.Black; //Create a TextRange and assign the text of the hyperlink to itTextRange TR =NewTextRange (document); Tr. Text=field. Fieldtext; //Apply Stylestr. Applycharacterformat (format); //Delete a text hyperlink fieldObjs. RemoveAt (i); //re-insert textObjs. Insert (i, TR); } //Delete picture hyperlinks, keep pictures if(Dobj isdocpicture) { //Delete a picture hyperlink fieldObjs. RemoveAt (i); //re-insert PictureObjs. Insert (i, dobj); } } } } } } }}
View Code
(End of this article)
If necessary, please indicate the source.