By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,792 Members | 1,306 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,792 IT Pros & Developers. It's quick & easy.

How to hide the data in between the two tags(<hidden> .. </hidden>) in Word document

P: 1
I am able to Read the input document using Apache POI and also able to find the data between the tags(What to be hidden) but the problem is i'm unable to write the data in the output file. How can i do the same to write the data and hide it in the output generated file..

For more check the following attachments where the input file has the data with the tags(<hidden>..... </hidden>) and whereas the output file has the data hidden (to check it use Hide/Show toggle box in the toolbar)


Expand|Select|Wrap|Line Numbers
  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import org.apache.poi.hwpf.HWPFDocument;
  5. import  org.apache.poi.hwpf.extractor.WordExtractor;
  6. import org.apache.poi.hwpf.usermodel.CharacterRun;
  7. import org.apache.poi.hwpf.usermodel.Range;
  8.  
  9.  
  10. public class Hidden {
  11.     public static void main(String args[]) throws Exception
  12.     {
  13.         File file = new File("D://me1.doc");
  14.         FileInputStream fin = new FileInputStream(file);
  15.         FileOutputStream fout = new FileOutputStream("D://Test.doc");
  16.         HWPFDocument doc = new HWPFDocument(fin);
  17.         Range range = doc.getRange();    
  18.         WordExtractor extractor = new WordExtractor(doc);
  19.         String para[] = extractor.getParagraphText();    
  20.         String output="";
  21.         String hidden="";
  22.         for (String p : para) {
  23.             String[] w = p.split("[<\\>]");             
  24.             for(int k=0 ;k<w.length;k++){
  25.                 if(w[k]!=null && !"".equalsIgnoreCase(w[k])){
  26.                     if("hidden".equalsIgnoreCase(w[k])){                            
  27.                         k++;
  28.                         CharacterRun  run = range.getCharacterRun(k);
  29.                         hidden= w[k];
  30.                         k++;                       
  31.                         System.out.println(hidden);
  32.                         run.setVanished(true);
  33.                         doc.write(fout); 
  34.                     }else{
  35.  
  36.                     }
  37.                 }
  38.             } 
  39.         }
  40.         fout.close();
  41.         fin.close();
  42.     }
  43.  
  44.  
Attached Files
File Type: doc INPUTFILE.doc (22.5 KB, 138 views)
File Type: doc OUTPUTFILE.doc (23.0 KB, 164 views)
May 16 '13 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.