Apache的POI删除CTHyperlink [低级code] [英] Apache POI Remove CTHyperlink [Low Level code]

查看:846
本文介绍了Apache的POI删除CTHyperlink [低级code]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真正的Bug继续:删除XWPFHyperlinkRun的Apache POI

Continue with my actual bug: Remove XWPFHyperlinkRun Apache POI

有了这个code:

public void eliminarHyperLink(XWPFDocument doc){
    XWPFHeaderFooterPolicy policy= doc.getHeaderFooterPolicy();
    XWPFFooter footer = policy.getDefaultFooter();
    List<XWPFParagraph> paragraphs = footer.getParagraphs();
    boolean primero = false;
    for (XWPFParagraph xwpfParagraph : paragraphs) {
        List<XWPFRun> runs = xwpfParagraph.getRuns();
        if(primero == true){
            for (XWPFRun run : runs) {
                if(run instanceof XWPFHyperlinkRun) {
                    XWPFHyperlinkRun linkRun = (XWPFHyperlinkRun)run;
                    ((XWPFHyperlinkRun) run).getCTHyperlink();
                    System.out.println(((XWPFHyperlinkRun) run).getCTHyperlink()); //Here is the output xml showen below
                }
            }
        }
        primero = true;
    }
}

达包含一个超链接比我需要删除XML,是有办法去除?

Reaches the xml that contains an hyperlink than I need to remove, is there a way to remove that?

<xml-fragment r:id="rId1" w:history="1" xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape">
<w:r w:rsidRPr="003A2DF6">
    <w:rPr>
        <w:rFonts w:ascii="Adobe Caslon Pro" w:hAnsi="Adobe Caslon Pro"/>
        <w:b/>
        <w:sz w:val="16"/>
        <w:szCs w:val="16"/>
    </w:rPr>
    <w:t>www.cnbv.gob.mx</w:t> //This is what I need to remove!!!!!!!!!!!
</w:r>
</xml-fragment>

请,如果有人有任何答案,我将非常gratefull!问候。

Please if someone had any answers I will be really gratefull! Regards.

推荐答案

以下code应该从一个段落列表中删除所有超链接。

The following code should remove all hyperlinks from a paragraphs list.

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHyperlink;

...
  for (XWPFParagraph paragraph : paragraphs) {
   List<CTHyperlink> hyperlinks = paragraph.getCTP().getHyperlinkList();
   CTHyperlink[] hyperlinksarr = hyperlinks.toArray(new CTHyperlink[0]);
   for (int i = 0; i < hyperlinksarr.length; i++ ) {
    CTHyperlink hyperlink = hyperlinksarr[i];
System.out.println(hyperlink.getRArray(0).getTArray(0).getStringValue());
    paragraph.getCTP().removeHyperlink(0);
   }
  }
...

列表 paragraph.getCTP产生的()。getHyperlinkList()似乎不是正确的。既不是的Iterator 也不每个似乎与它的工作。所以,我创建了一个阵列从它。

The List generated by paragraph.getCTP().getHyperlinkList() seems not to be correct. Neither a Iterator nor for each seems to work with it. So I've created an Array from it.

编辑:

当然的列表 paragraph.getCTP()。getHyperlinkList()生成也是使用正确的的Iterator 每个。但我不正确;-)。如果我们从列表在遍历该列表中删除的元素,我们不应该感到惊讶,如果的Iterator 将被搞乱。

Of course the List generated by paragraph.getCTP().getHyperlinkList() is also correct for using Iterator and for each. But I am not correct ;-). If we remove elements from the list while iterating over this list, we should not be surprised if the Iterator will be messed.

这篇关于Apache的POI删除CTHyperlink [低级code]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆