poi-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From supra_ch <supratim.chakrabo...@gmail.com>
Subject Re: How to read the value of bookmarks? (docx)
Date Tue, 19 Jun 2012 17:34:16 GMT
Please try within this document alsosend me the java file for that ? Is this
program can be run on doc file as well ? Also i want to get the value of
bookmark and then replace with some other te
http://apache-poi.1045710.n5.nabble.com/file/n5710244/BeforeBookMarkValuesReplacement.docx
BeforeBookMarkValuesReplacement.docx xt ?

I did a try and it was working for the doc which does not have table.

Code Snippet
===========

	public static void readDocXBookMarks(String filePath) throws IOException
	{
		File file = null; 
        FileInputStream fis = null;
        XWPFDocument document=null;
    

        
        try
        {
            file = new File(filePath); 
            fis = new FileInputStream(file); 
            document = new XWPFDocument(fis); 
            
            List<XWPFParagraph>  xwpfParas = document.getParagraphs();
        	for(XWPFParagraph xwpfPara:xwpfParas)
        	{
        		CTP ctp=xwpfPara.getCTP();

        		
        		for(CTBookmark bookmark : ctp.getBookmarkStartList()) 
        		{
        			System.out.println("bookmark name="+bookmark.getName());
        			System.out.println("bookmark id="+bookmark.getId());
        	
        			Node bmNode= bookmark.getDomNode().getNextSibling();
        			FileOutputStream fos = null; 
        			if(bmNode!=null)
        			{
        				
        				
            			NodeList childs= bmNode.getChildNodes();
            			int num=childs.getLength();
            			System.out.println("getChildNodes="+num);
            			for(int i=0;i<=num;i++)
            			{
            				Node node=childs.item(i);
            				if(node!=null)
            				{
                				System.out.println("node name="+node.getNodeName());
                				System.out.println("node type="+node.getNodeType());
                				System.out.println("node val="+node.getNodeValue());
                				node=node.getFirstChild();
                				if(node!=null)
                				{
                    				System.out.println("child node
name="+node.getNodeName());
                    				System.out.println("child node
type="+node.getNodeType());
                    				System.out.println("child node
val="+node.getNodeValue());
                    				                    				
                    				String bookmarkName = bookmark.getName();
                    				
                    				if(bookmarkName.equalsIgnoreCase("<Bookmark1>"))
                    				{
                    					 
                    		             file = new File("test_Test1.docx");
                    		    		 fos = new FileOutputStream(file); 
                    		    		 document.write(fos); 
                    				}
                    				                				}

            				}
            			}
        			}
        			        			
        			 
        			System.out.println("\n");
        			
        	    }
        		
        		
    	                    } 
        	
        	
        	
            
        } 
        catch(Exception ex) { 
            ex.printStackTrace(System.out); 
        } 
        finally { 
            if(fis != null) { 
                try { 
                    fis.close(); 
                    fis = null; 
                } 
                catch(Exception ex) { 
                    // I G N O R E 
                } 
            } 
        } 
        	
}

--
View this message in context: http://apache-poi.1045710.n5.nabble.com/How-to-read-the-value-of-bookmarks-docx-tp5710184p5710244.html
Sent from the POI - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Mime
View raw message