We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to Replace or Modify Hyperlinks

i want to know how can i add the packages to my java library so that i can execute the programs in my computer. i want to execute the program “How to Replace or Modify Hyperlinks” please help me to do that… actually i want to remove all links in a word document…

please tell me anyone how can i do it… i want to remove all links from a word document except texts using java…

Hi there,

Thanks for considering Aspose.

You can follow the visual steps on how to reference Aspose.Words in your Java program by watching the following video: https://docs.aspose.com/words/java/convert-a-document-to-pdf/

Once you have Aspose.Words up and running you can simply include the code into one of your classes and run it on any document loaded into Aspose.Words.

If we can help with anything else, please feel free to ask.

Thanks,

Thanks for the information… now i can compile the code from Aspose and i can also execute it. As i want to remove all hyperlinks from the word document i simply replace the NEW_NAME with nothing as “”. but now the problem is that after execution the given output document has only

Evaluation Only. Created with Aspose.Words. Copyright 2003-2011 Aspose Pty Ltd.

This document was truncated here because it was created using Aspose.Words in Evaluation Mode.

these two lines… nothing else…

how can i fix this please help…

Hi Banhisikha,

Thanks for your inquiry. Yes, this is a limitation of evaluation version. Evaluation version of Aspose.Words injects an evaluation watermark at the top of the document on open and save and limits the maximum document size to several hundred paragraphs.

If you want to test Aspose.Words without evaluation version limitations, you can request a 30-Day Temporary License. See the following link for more information:

https://purchase.aspose.com/temporary-license

If we can help you with anything else, please feel free to ask.

Best Regards,

I have now a temporary license named “Aspose.Total.Java.lic”. Please tell me now, how do i apply it to my program…
i downloaded the Aspose.Total for java before getting the license. Would it be a problem applying the license? Let me know…

Hi Banhisikha,

Thanks for the inquiry.

You can find suitable articles on applying a license and when to apply a license here:
https://docs.aspose.com/words/java/licensing/

For testing purposes, you can try setting the license right before generating document. It is not recommended to set license several times, but this should work if the license is valid even with Aspose.Total for java which you have already download.

I hope this will help. In case of any ambiguity, please let me know.

public class testpro {

    public static void main(String[] args){
        try{
            tstrmHyp trh=new tstrmHyp();
            String s=trh.rmHyper("a.doc","a1.doc");
            System.out.println("Program Executed.");
        }
        catch ( java.lang.Exception ex ){
            ex.printStackTrace ( );
        }
        //catch ( java.lang.ClassCastException ex ){
        // ex.printStackTrace( );
        //}
    }

}
class tstrmHyp{
    public String rmHyper(String infn, String outfn){
        try{
            String path="F:/Program Files/Java/jdk1.6.0_10/bin/";
            Document doc = new Document(path + infn);
            NodeList fieldStarts = doc.selectNodes("//FieldStart");
            for (FieldStart fieldStart : (Iterable) fieldStarts){
                if (fieldStart.getFieldType() == FieldType.FIELD_HYPERLINK){
                    Hyperlink hyperlink = new Hyperlink(fieldStart);
                    if (hyperlink.isLocal())
                        continue;
                    hyperlink.setName(NEW_NAME);
                }
            }
            doc.save(path + outfn);
        }
        catch( java.io.IOException ex ){
            ex.printStackTrace();
        }
        catch ( java.lang.Exception ex ){
            ex.printStackTrace ( );
        }
        String str="Program executed Successfully. Goto Next Page.";
        return(str);
    }
    private static final String NEW_URL = "<http://www.aspose.com>";
    private static final String NEW_NAME = "BANHISIKHA";
}

class Hyperlink{
    Hyperlink(FieldStart fieldStart) throws Exception{
        if (fieldStart == null)
            throw new IllegalArgumentException("fieldStart");
        if (fieldStart.getFieldType() != FieldType.FIELD_HYPERLINK)
            throw new IllegalArgumentException("Field start type must be FieldHyperlink.");
        mFieldStart = fieldStart;
        mFieldSeparator = findNextSibling(mFieldStart, NodeType.FIELD_SEPARATOR);
        if (mFieldSeparator == null)
            throw new IllegalStateException("Cannot find field separator.");
        mFieldEnd = findNextSibling(mFieldSeparator, NodeType.FIELD_END);
        String fieldCode = getTextSameParent(mFieldStart.getNextSibling(), mFieldSeparator);
        Matcher matcher = G_REGEX.matcher(fieldCode.trim());
        matcher.find();
        mIsLocal = (matcher.group(1) != null) && (matcher.group(1).length() > 0);
        mTarget = matcher.group(2).toString();
    }
    String getName() throws Exception{
        return getTextSameParent(mFieldSeparator, mFieldEnd);
    }
    void setName(String value) throws Exception{
        Run fieldResult = (Run)mFieldSeparator.getNextSibling();
        fieldResult.setText(value);
        removeSameParent(fieldResult.getNextSibling(), mFieldEnd);
    }
    String getTarget() throws Exception{
        return mTarget;
    }
    void setTarget(String value) throws Exception{
        mTarget = value;
        updateFieldCode();
    }
    boolean isLocal() throws Exception{
        return mIsLocal;
    }
    void isLocal(boolean value) throws Exception{
        mIsLocal = value;
        updateFieldCode();
    }
    private void updateFieldCode() throws Exception{
        Run fieldCode = (Run)mFieldStart.getNextSibling();
        fieldCode.setText(java.text.MessageFormat.format("HYPERLINK {0}"{1}"", ((mIsLocal) ? "\l " : ""), mTarget));
        removeSameParent(fieldCode.getNextSibling(), mFieldSeparator);
    }
    private static Node findNextSibling(Node startNode, int nodeType) throws Exception{
        for (Node node = startNode; node != null; node = node.getNextSibling()){
            if (node.getNodeType() == nodeType)
                return node;
        }
        return null;
    }
    private static String getTextSameParent(Node startNode, Node endNode) throws Exception{
        if ((endNode != null) && (startNode.getParentNode() != endNode.getParentNode()))
            throw new IllegalArgumentException("Start and end nodes are expected to have the same parent.");
        StringBuilder builder = new StringBuilder();
        for (Node child = startNode; !child.equals(endNode); child = child.getNextSibling())
            builder.append(child.getText());
        return builder.toString();
    }
    private static void removeSameParent(Node startNode, Node endNode) throws Exception{
        if ((endNode != null) && (startNode.getParentNode() != endNode.getParentNode()))
            throw new IllegalArgumentException("Start and end nodes are expected to have the same parent.");

        Node curChild = startNode;
        while ((curChild != null) && (curChild != endNode)){
            Node nextChild = curChild.getNextSibling();
            curChild.remove();
            curChild = nextChild;
        }
    }
    private final Node mFieldStart;
    private final Node mFieldSeparator;
    private final Node mFieldEnd;
    private boolean mIsLocal;
    private String mTarget;
    private static final Pattern G_REGEX = Pattern.compile(
            "\S+" + // one or more non spaces HYPERLINK or other word in other languages
                    "\s+" + // one or more spaces
                    "(?:""\s+)?" + // non capturing optional "" and one or more spaces, found in one of the customers files.
                    "(\\l\s+)?" + // optional \l flag followed by one or more spaces
                    """ + // one apostrophe
                    "([^"]+)" + // one or more chars except apostrophe (hyperlink target)
                    """ // one closing apostrophe
    );
}

hi there,
on executing a program written above I am getting exception written below…

java.lang.ClassCastException: com.aspose.words.Shape cannot be cast to com.aspose.words.Run
at Hyperlink.setName(testpro.java:88)
at tstrmHyp.rmHyper(testpro.java:49)
at testpro.main(testpro.java:25)

but when i change the input file name then there is no error or exception… how can i fix it… please help. File containing hyperlinks to www.

Hi Banhisikha,

Thanks for the inquiry.

First, please note that DocumentExplorer is a very useful tool which easily enables us to see the entire document structure. You can find DocumentExplorer in the folder where you installed Aspose.Words e.g. C:\Program Files (x86)\Aspose\Aspose.Words for .NET\Demos\CSharp\DocumentExplorer\bin\DocumentExplorer.exe.

Furthermore, I will suggest you to follow up the exact code which is available in our documentation:

https://docs.aspose.com/words/java/update-field/

I hope this will help. In case any ambiguity, please let us share appropriate input Word document.

The issues you have found earlier (filed as WORDSNET-6188) have been fixed in this .NET update and this Java update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(2)