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

Free Support Forum - aspose.com

删除外链另存docx格式的时候报错

提供附件:
1.doc.zip (5.9 KB)
代码:
private static void SaveToDocxTeset() {
URL mainClassURL = Main.class.getProtectionDomain().getCodeSource().getLocation();
String current = “/” + FilenameUtils.getPath(mainClassURL.getPath());
String lincenseFile = current + “license.xml”;
if (!Utils.setLicense(lincenseFile)) {
System.out.println(“license set error!”);
return;
}
try {
Document mInWordDoc = new Document("/data/temp/1.doc");

        ArrayList<FieldStart> hyperlinkStarts = new ArrayList<FieldStart>();
        NodeCollection starts = mInWordDoc.getChildNodes(NodeType.FIELD_START, true);
        for (FieldStart start : (Iterable<FieldStart>) starts) {
            if (start.getFieldType() == FieldType.FIELD_HYPERLINK) {
                hyperlinkStarts.add(start);
                Node currentNode = start.getNextSibling();
                while (currentNode.getNodeType() != NodeType.FIELD_SEPARATOR) {
                    currentNode = currentNode.getNextSibling();
                    currentNode.getPreviousSibling().remove();
                }

                Node fieldSeparator = currentNode;
                while (currentNode.getNodeType() != NodeType.FIELD_END) {
                    if (currentNode.getNodeType() == NodeType.RUN) {
                        Run run = (Run) currentNode;
                        run.getFont().setColor(Color.BLACK);
                        run.getFont().setUnderline(Underline.NONE);
                    }
                    currentNode = currentNode.getNextSibling();
                }
                fieldSeparator.remove();
                currentNode.remove();
            }
        }
        for (FieldStart start : hyperlinkStarts) {
            start.remove();
        }
        mInWordDoc.save("/data/temp/1.docx", SaveFormat.DOCX);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

错误信息:
java.lang.IllegalStateException: Invalid document model. Operation can not be completed.
at com.aspose.words.zz3D.zzZZ6(Unknown Source)
at com.aspose.words.zz3D.zzZ(Unknown Source)
at com.aspose.words.zz3D.zzZ(Unknown Source)
at com.aspose.words.zz3D.zzZ(Unknown Source)
at com.aspose.words.FieldChar.getField(Unknown Source)
at com.aspose.words.zzZEY.zzU(Unknown Source)
at com.aspose.words.zzZEY.zzW(Unknown Source)
at com.aspose.words.zz8C.visitFieldEnd(Unknown Source)
at com.aspose.words.FieldEnd.accept(Unknown Source)
at com.aspose.words.CompositeNode.acceptChildren(Unknown Source)
at com.aspose.words.CompositeNode.acceptCore(Unknown Source)
at com.aspose.words.Paragraph.accept(Unknown Source)
at com.aspose.words.CompositeNode.acceptChildren(Unknown Source)
at com.aspose.words.CompositeNode.acceptCore(Unknown Source)
at com.aspose.words.Body.accept(Unknown Source)
at com.aspose.words.zz8C.zzW(Unknown Source)
at com.aspose.words.zz8D.zz3t(Unknown Source)
at com.aspose.words.zz8C.write(Unknown Source)
at com.aspose.words.zz72.zzZ(Unknown Source)
at com.aspose.words.zzZHA.zzZ(Unknown Source)
at com.aspose.words.Document.zzZ(Unknown Source)
at com.aspose.words.Document.zzZ(Unknown Source)
at com.aspose.words.Document.save(Unknown Source)
at com.aspose.words.Document.save(Unknown Source)

如果代码改成下面这种,就不会报错:
try {
Document mInWordDoc = new Document("/data/temp/1.doc");
mInWordDoc.save("/data/temp/1.docx", SaveFormat.DOCX);
} catch (Exception e) {
e.printStackTrace();
}

@jcing,

请尝试使用以下代码:

Document doc = new Document("D:\\temp\\1doc\\1.doc");

NodeCollection starts = doc.getChildNodes(NodeType.FIELD_START, true);
for (FieldStart start : (Iterable<FieldStart>) starts) {
    if (start.getFieldType() == FieldType.FIELD_HYPERLINK) {
        FieldHyperlink hyperlink = (FieldHyperlink)start.getField();
        if (hyperlink.getSubAddress() != null && hyperlink.getSubAddress().startsWith("_Toc"))
        {
            hyperlink.unlink();
        }
    }
}

doc.save("D:\\temp\\1doc\\awjava-18.7.docx");

试过了,错是不会报了,但是外链没有去掉!
image.png (20.5 KB)

@jcing,

以下代码应取消链接Word文档中的所有超链接字段。

Document doc = new Document("D:\\temp\\1doc\\1.doc");

NodeCollection starts = doc.getChildNodes(NodeType.FIELD_START, true);
for (FieldStart start : (Iterable<FieldStart>) starts) {
    if (start.getFieldType() == FieldType.FIELD_HYPERLINK) {
        FieldHyperlink hyperlink = (FieldHyperlink) start.getField();
        hyperlink.unlink();
    }
}

doc.save("D:\\temp\\1doc\\awjava-18.7.docx");

谢谢,当我有空了试试看!