Results 1 to 13 of 13

Thread: Edit documents

  1. #1
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

    Default Edit documents

    Hello all !

    I am working on project in which i want to extract reports in docx format or just doc
    to make this a have a form (in doc) which i want to open and then only fill the gaps only with additional informations but when i open the doc and then make the changes then when i save it and try to open it i got an error.. so i guess the way i try to edit the word.doc is wrong or not 100% corrent..
    below is my code :

    Java Code:
    ArrayList<String> results = new ArrayList<>();
    		Scanner inputStream = null;
    		try{
    			inputStream = new Scanner(new FileInputStream(PATH + "word.doc"));
    		}catch(FileNotFoundException e){
    			e.printStackTrace();
    		}
    		String line = null;
                    int k = 0;
    		while(inputStream.hasNext()){
    			line = inputStream.nextLine();
    			String [] words = line.split(" ");
    			for (int i=0;i<words.length;i++){
    				String [] letters = words[i].split("");
    				if (letters[0].equals("$") && letters[words[i].length()-1].equals("$")){
    				        words[i]  = words[i].replace(words[i], replacements.get(k));
    					k++;
    				}
    			}
    			line = "";
    			for(int i = 0 ; i < words.length ; i++){
    				line = line + words[i] + " ";
    			}
    			results.add(line);
    		}
    		
    		inputStream.close();
    		PrintWriter outputStream = null;
    		try{
    			outputStream = new PrintWriter(new FileOutputStream("C:/users/USERNAME/Desktop/" + "report.doc"));
    			for(int i = 0 ; i < results.size() ; i++){
    				outputStream.write(results.get(i));
    			}
    			outputStream.close();
    		}catch(FileNotFoundException e){
    			e.printStackTrace();
    		}
    this is not the full code .. it's just a part of it..

    if i try to open a txt and save it in doc all are good but if i try to open the doc and then save it as doc it's not working.

    thx in advance
    Last edited by ShadowWalker; 05-04-2014 at 02:28 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,585
    Rep Power
    25

    Default Re: Edit documents

    Can you make a small, complete program that compiles, executes and shows the problem?

    Where are the packages and classes that know how to handle the contents of a doc file?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

    Default Re: Edit documents

    Java Code:
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.PrintWriter;
    import java.util.ArrayList;
    import java.util.Scanner;
    
    
    public class Report {
    	private String name = "Alex";
    	private String surname = "XXXXX";
    	private String fathersName = "Something";
    	private String AMKA = "1234567890";
    	private String illness = "just ill";
    	private String birthDay = "2";
    	private String birthMonth = "12";
    	private String birthYear = "1994";
    	private String surgeryDate = "19";
    	private String surgeryMonth = "6";
    	private String surgeryYear = "2013";
    	private String surgeryType = "Dunno";
    	private String exportDay = "31";
    	private String exportMonth = "12";
    	private String exportYear = "2013";
    	private String instructions = "some instructions";
    	private String pharmacyType = "Zinadol";
    	private ArrayList<String> replacements = new ArrayList <String>();
    	private final String PATH = "C:/users/ShadowWalker/Desktop/Files/";
    	
    	
    	public static void main(String args []){
    		new Report();
    	}
    	
    	public Report(){		
    		replacements.add(name);
    		replacements.add(surname);
    		replacements.add(fathersName);
    		replacements.add(AMKA);
    		replacements.add(illness);
    		replacements.add(birthDay);
    		replacements.add(birthMonth);
    		replacements.add(birthYear);
    		replacements.add(surgeryDate);
    		replacements.add(surgeryMonth);
    		replacements.add(surgeryYear);
    		replacements.add(surgeryType);
    		replacements.add(exportDay);
    		replacements.add(exportMonth);
    		replacements.add(exportYear);
    		replacements.add(instructions);
    		replacements.add(pharmacyType);
    		
    		ArrayList<String> results = new ArrayList<>();
    		Scanner inputStream = null;
    		try{
    			inputStream = new Scanner(new FileInputStream(PATH + "word.txt"));
    		}catch(FileNotFoundException e){
    			e.printStackTrace();
    		}
    		String line = null;
    		int k = 0;
    		while(inputStream.hasNext()){
    			line = inputStream.nextLine();
    			String [] words = line.split(" ");
    			for (int i=0;i<words.length;i++){
    				String [] letters = words[i].split("");
    				if (letters[0].equals("$") && letters[words[i].length()-1].equals("$")){
    					words[i] = words[i].replace(words[i], replacements.get(k));
    					k++;
    				}
    			}
    			line = "";
    			for(int i = 0 ; i < words.length ; i++){
    				line = line + words[i] + " ";
    			}
    			results.add(line);
    		}
    		
    		inputStream.close();
    		PrintWriter outputStream = null;
    		try{
    			outputStream = new PrintWriter(new FileOutputStream("C:/users/ShadowWalker/Desktop/" + "report.doc"));
    			for(int i = 0 ; i < results.size() ; i++){
    				outputStream.write(results.get(i));
    				System.out.println("yes");
    			}
    			outputStream.close();
    		}catch(FileNotFoundException e){
    			e.printStackTrace();
    		}
    	}
    }
    i hope this example it's quit clean :P
    my Report class is something like this.. i don't have something to handle the content of doc file like
    StyledDocument doc, i just try to open it and read line by line and make changes.. and then just same it with new lines..

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default Re: Edit documents

    I don't think .docx (or .doc) files are ordinary text files; they have a lousy Microsoft format; Apache has a 'poi' project that can handle them.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

    Default Re: Edit documents

    Apache POI ??! hmmm i hope the libraries are easy in use :P or i will have problems :P hehehe

  6. #6
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

    Default Re: Edit documents

    As my problem it's still on the report i don't want to open new Thread on forum.
    I Use Apache POI to open and modify word documents for reports of my program.. but then i find out that in different machines my word document display was quite different.. any one has face this problem again and any way to solve it?


    and my Report Class (just in case ) is :

    Java Code:
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    
    import org.apache.poi.poifs.filesystem.*;
    import org.apache.poi.hwpf.*;
    
    public class Report {
    	
    	private final String PATH = System.getProperty("user.home")+"/Desktop/Files/Reports/";
    	
    	
    	public Report(Patient patient) throws IOException{
    		File file = new File(getClass().getResource("ReportPrototype.doc").getPath());
    		POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file));
    		HWPFDocument doc = new HWPFDocument(fs);
    
    		doc.getRange().replaceText("$Name$", patient.getName());
    		doc.getRange().replaceText("$Surname$", patient.getSurname());
    		doc.getRange().replaceText("$FathersName$", patient.getFathersName());
    		doc.getRange().replaceText("$birthDay$", (String)patient.birthdayDate.getDayComboBox().getSelectedItem());
    		doc.getRange().replaceText("$birthMonth$",(String)patient.birthdayDate.getMonthComboBox().getSelectedItem());
    		doc.getRange().replaceText("$birthYear$",(String)patient.birthdayDate.getYearComboBox().getSelectedItem());
    		doc.getRange().replaceText("$insertionDay$", (String)patient.insertionDate.getDayComboBox().getSelectedItem());
    		doc.getRange().replaceText("$insertionMonth$",(String)patient.insertionDate.getMonthComboBox().getSelectedItem());
    		doc.getRange().replaceText("$insertionYear$ ", (String)patient.insertionDate.getYearComboBox().getSelectedItem());
    		
    		doc.getRange().replaceText("$SurgeryDay$", (String)patient.getSurgeryDate().getDayComboBox().getSelectedItem());
    		doc.getRange().replaceText("$SurgeryMonth$", (String)patient.getSurgeryDate().getMonthComboBox().getSelectedItem());
    		doc.getRange().replaceText("$SurgeryYear$", (String)patient.getSurgeryDate().getYearComboBox().getSelectedItem());
    
    		OutputStream out = new FileOutputStream(new File(PATH+ patient.getName()+"."+patient.getSurname()+".doc"));
    		
    		doc.write(out);
    		out.flush();
    		out.close();
    	}
    }
    Last edited by ShadowWalker; 05-05-2014 at 03:03 PM.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,585
    Rep Power
    25

    Default Re: Edit documents

    word document display was quite different.
    Please post some samples of the differences and add comments saying what was wrong.
    Last edited by Norm; 05-05-2014 at 04:03 PM.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

    Default Re: Edit documents

    this is what i am trying to display (of course the black lines are some information which i want to hide)
    and this picture is what i have on my Laptop
    Edit documents-screenshot_1.png

    and this is on desktop :

    Edit documents-screenshot_2.png

    i send the word to my friend and his display was different two.. (of course all PC with same windows )

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,585
    Rep Power
    25

    Default Re: Edit documents

    What was wrong? Can you describe what the problem is?

    Can you create a special file for testing without anything personal? Update it and then copy and post here the different versions saying what is different and what is wrong.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

    Default Re: Edit documents

    I want to believe that my problem it's quite clear on this photo.. the diplay is not the same..
    some parts of the text is not on the correct location and dunno why..

    this is the correct one
    Edit documents-screenshot_1.png

    this is the wrong one
    Edit documents-screenshot_2.png\

    don't check about the text...cause i have remove some txt on the second image just to emulate the results on onother computer ...
    Last edited by ShadowWalker; 05-05-2014 at 04:24 PM.

  11. #11
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

    Default Re: Edit documents

    hmmm after some googling.. i found this .. :
    Why has my page layout changed when I open my document on a different machine?
    so if i want to have the same display on all platforms and all machines i have to make my report on PDF.. pfffffffffffffff :'(

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,585
    Rep Power
    25

    Default Re: Edit documents

    This question should be asked on the apache site.
    This isn't about java programming. It's about using some 3rd party software.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

    Default Re: Edit documents

    I am probably going to make a post on their forum too..
    but before i go there there is any way to convert a a doc or just a txt file into PDF without any 3th party software? (i guess no :P )

Similar Threads

  1. Replies: 2
    Last Post: 02-22-2012, 10:09 AM
  2. Edit jar, see the code, edit code and back it as a jar
    By RichersooN in forum New To Java
    Replies: 1
    Last Post: 10-10-2011, 05:33 AM
  3. download API documents
    By srinivasmallabathula in forum New To Java
    Replies: 1
    Last Post: 01-20-2011, 11:15 AM
  4. scjp documents-help me
    By ahamedirshad123 in forum Java Certification
    Replies: 0
    Last Post: 05-03-2010, 09:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •