From 86db94696c64e9687c5e2007b3ce938bacb9ceb6 Mon Sep 17 00:00:00 2001 From: GOPINATHSF4767 Date: Thu, 16 Apr 2026 18:05:26 +0530 Subject: [PATCH 1/2] Added java file --- .../fields/RemoveAllHyperlinks/Program.java | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 examples/fields/RemoveAllHyperlinks/Program.java diff --git a/examples/fields/RemoveAllHyperlinks/Program.java b/examples/fields/RemoveAllHyperlinks/Program.java new file mode 100644 index 0000000..e1c0470 --- /dev/null +++ b/examples/fields/RemoveAllHyperlinks/Program.java @@ -0,0 +1,79 @@ +import com.syncfusion.docio.*; +import com.syncfusion.javahelper.system.collections.generic.ListSupport; + +public class Program { + public static void main(String[] args) throws Exception { + //Open the Word template document. + WordDocument document = new WordDocument("Template.docx"); + //Find all fields in the Word document. + ListSupport fields = document.findAllItemsByProperty(EntityType.Field, null, null); + //Remove hyperlink fields and replace with text. + for(Object field : fields) + { + WField wField = (WField) field; + if(wField.getFieldType().getEnumValue()==FieldType.FieldHyperlink.getEnumValue()) + { + RemoveHyperlink(wField); + } + } + //Clear the temporary field collection. + fields.clear(); + //Saves the Word document. + document.save("Output.docx", FormatType.Docx); + document.close(); + System.out.println("Word document generated successfully"); + } + public static String GetHyperlinkText(int hyperlinkIndex, WParagraph paragraph) throws Exception { + + String text = ""; + //Add the hyperlink field in stack to get the textrange from nested fields. + Stack fieldStack = new Stack(); + fieldStack.push(paragraph.getChildEntities().get(hyperlinkIndex)); + //Flag to get the text from textrange between field separator and end. + boolean isFieldCode = true; + int i = (hyperlinkIndex + 1); + while (i < paragraph.getItems().getCount()) + { + Entity item = paragraph.getChildEntities().get(i); + //If it is nested field, maintain in stack. + if ((item instanceof WField)) + { + fieldStack.push(item); + //Set flag to skip getting text from textrange. + isFieldCode = true; + } + else if ((item instanceof WFieldMark) && (((WFieldMark)(item)).getType().getEnumValue() == FieldMarkType.FieldSeparator.getEnumValue())) + //If separator is reached, set flag to read text from textrange. + isFieldCode = false; + else if ((item instanceof WFieldMark) && (((WFieldMark)(item)).getType().getEnumValue() == FieldMarkType.FieldEnd.getEnumValue())) + { + //If field end is reached, check whether it is end of hyperlink field and skip the iteration. + if (fieldStack.size() == 1) + { + fieldStack.clear(); + return text; + } + else + fieldStack.pop(); + } + else if (!isFieldCode && (item instanceof WTextRange)) + text = (text + ((WTextRange)(item)).getText()); + i = (i + 1); + } + return text; + } + + //Method to replace a hyperlink field with a text value + public static void RemoveHyperlink(WField field) throws Exception { + WParagraph paragraph = field.getOwnerParagraph(); + int itemIndex = paragraph.getChildEntities().indexOf(field); + WTextRange textRange = new WTextRange(paragraph.getDocument()); + //Gets the text from hyperlink field. + textRange.setText(GetHyperlinkText(itemIndex, paragraph)); + //Removes the hyperlink field + paragraph.getChildEntities().removeAt(itemIndex); + //Inserts the hyperlink text + paragraph.getChildEntities().insert(itemIndex, textRange); + + } +} From a4d38b312c7fb1c8e1c7808e1f8efe5d959eb1b9 Mon Sep 17 00:00:00 2001 From: GOPINATHSF4767 Date: Tue, 5 May 2026 16:37:11 +0530 Subject: [PATCH 2/2] Modified sample --- .../fields/RemoveAllHyperlinks/Program.java | 124 +++++++++--------- 1 file changed, 59 insertions(+), 65 deletions(-) diff --git a/examples/fields/RemoveAllHyperlinks/Program.java b/examples/fields/RemoveAllHyperlinks/Program.java index e1c0470..b10cea7 100644 --- a/examples/fields/RemoveAllHyperlinks/Program.java +++ b/examples/fields/RemoveAllHyperlinks/Program.java @@ -4,76 +4,70 @@ public class Program { public static void main(String[] args) throws Exception { //Open the Word template document. - WordDocument document = new WordDocument("Template.docx"); - //Find all fields in the Word document. - ListSupport fields = document.findAllItemsByProperty(EntityType.Field, null, null); - //Remove hyperlink fields and replace with text. - for(Object field : fields) - { - WField wField = (WField) field; - if(wField.getFieldType().getEnumValue()==FieldType.FieldHyperlink.getEnumValue()) - { - RemoveHyperlink(wField); - } + WordDocument document = new WordDocument("Template.docx"); + // Find all hyperlink fields directly using findAllItemsByProperty + ListSupport hyperlinkFields = document.findAllItemsByProperty(EntityType.Field, "FieldType", FieldType.FieldHyperlink.toString()); + + // Iterate backward through hyperlink fields + for (int i = hyperlinkFields.getCount() - 1; i >= 0; i--) { + WField wField = (WField) hyperlinkFields.get(i); + removeHyperlinkFromField(wField); } - //Clear the temporary field collection. - fields.clear(); + + hyperlinkFields.clear(); //Saves the Word document. document.save("Output.docx", FormatType.Docx); document.close(); System.out.println("Word document generated successfully"); } - public static String GetHyperlinkText(int hyperlinkIndex, WParagraph paragraph) throws Exception { - - String text = ""; - //Add the hyperlink field in stack to get the textrange from nested fields. - Stack fieldStack = new Stack(); - fieldStack.push(paragraph.getChildEntities().get(hyperlinkIndex)); - //Flag to get the text from textrange between field separator and end. - boolean isFieldCode = true; - int i = (hyperlinkIndex + 1); - while (i < paragraph.getItems().getCount()) - { - Entity item = paragraph.getChildEntities().get(i); - //If it is nested field, maintain in stack. - if ((item instanceof WField)) - { - fieldStack.push(item); - //Set flag to skip getting text from textrange. - isFieldCode = true; - } - else if ((item instanceof WFieldMark) && (((WFieldMark)(item)).getType().getEnumValue() == FieldMarkType.FieldSeparator.getEnumValue())) - //If separator is reached, set flag to read text from textrange. - isFieldCode = false; - else if ((item instanceof WFieldMark) && (((WFieldMark)(item)).getType().getEnumValue() == FieldMarkType.FieldEnd.getEnumValue())) - { - //If field end is reached, check whether it is end of hyperlink field and skip the iteration. - if (fieldStack.size() == 1) - { - fieldStack.clear(); - return text; - } - else - fieldStack.pop(); - } - else if (!isFieldCode && (item instanceof WTextRange)) - text = (text + ((WTextRange)(item)).getText()); - i = (i + 1); - } - return text; - } + static void removeHyperlinkFromField(WField field) { + try { + WParagraph paragraph = field.getOwnerParagraph(); + int itemIndex = paragraph.getChildEntities().indexOf(field); + + WTextRange textRange = new WTextRange(paragraph.getDocument()); + textRange.setText(getHyperlinkText(itemIndex, paragraph)); + + paragraph.getChildEntities().removeAt(itemIndex); + paragraph.getChildEntities().insert(itemIndex, textRange); + } catch (Exception e) { + e.printStackTrace(); + } + } + + static String getHyperlinkText(int hyperlinkIndex, WParagraph paragraph) throws Exception { + String text = ""; + java.util.Stack fieldStack = new java.util.Stack(); + fieldStack.push(paragraph.getChildEntities().get(hyperlinkIndex)); - //Method to replace a hyperlink field with a text value - public static void RemoveHyperlink(WField field) throws Exception { - WParagraph paragraph = field.getOwnerParagraph(); - int itemIndex = paragraph.getChildEntities().indexOf(field); - WTextRange textRange = new WTextRange(paragraph.getDocument()); - //Gets the text from hyperlink field. - textRange.setText(GetHyperlinkText(itemIndex, paragraph)); - //Removes the hyperlink field - paragraph.getChildEntities().removeAt(itemIndex); - //Inserts the hyperlink text - paragraph.getChildEntities().insert(itemIndex, textRange); - - } + boolean isFieldCode = true; + int i = hyperlinkIndex + 1; + + while (i < paragraph.getItems().getCount()) { + Entity item = paragraph.getChildEntities().get(i); + + if (item instanceof WField) { + fieldStack.push(item); + isFieldCode = true; + } else if (item instanceof WFieldMark) { + WFieldMark mark = (WFieldMark) item; + if (mark.getType().getEnumValue() == FieldMarkType.FieldSeparator.getEnumValue()) { + isFieldCode = false; + } else if (mark.getType().getEnumValue() == FieldMarkType.FieldEnd.getEnumValue()) { + if (fieldStack.size() == 1) { + fieldStack.clear(); + return text; + } else { + fieldStack.pop(); + } + } + } else if (!isFieldCode && item instanceof WTextRange) { + text = text + ((WTextRange) item).getText(); + } + + i++; + } + + return text; + } }