Document merge issue

dear team,

we are merging docx using aspose java, but in this case we facing some special character please below source code we are using

source code :

 public static void main(String[] args) throws Exception {
    try {
      License license = new License();
      license.setLicense(MergeFile.class.getResourceAsStream("/Aspose.Total.Java.lic"));
    } catch (Exception e) {
      System.out.println("License File Not Accessed\nError in Word Document");
      System.exit(0);
    } 
    try {
      json = args[0];
      int count = 0;
      String firstFile = "";
      JSONParser parser = new JSONParser();
      try {
        logger.info("*---------START---------*");
        JSONArray a = (JSONArray)parser.parse(new FileReader(json));
        for (Object o : a) {
          JSONObject order = (JSONObject)o;
          if (count == 0) {
            firstFile = FilenameUtils.getName((String)order.get("FilePath"));
            firstFile = FilenameUtils.removeExtension(firstFile);
          } 
          String filePath = (String)order.get("FilePath");
          Path path = Paths.get(filePath, new String[0]);
          String directory = path.getParent().toString();
          mergedFilePath = String.valueOf(directory) + fs + firstFile + "_Merged.docx";
          File finalFile = new File(mergedFilePath);
          if (finalFile.exists() && count == 0) {
            finalFile.delete();
            finalFile.createNewFile();
          } else if (count == 0) {
            finalFile.createNewFile();
          } 
          Document finalDoc = new Document(mergedFilePath);
          Document doc = new Document(filePath);
          finalDoc.appendDocument(doc, 0);
          finalDoc.save(mergedFilePath, 20);
          count++;
        } 
        Document removeFirstPageDoc = new Document(mergedFilePath);
        removeBlankPages(removeFirstPageDoc);
        logger.info("Successfully file merged");
        logger.info("*---------END---------*");
      } catch (FileNotFoundException e) {
        System.out.println("File not Found :" + e);
      } catch (IOException e) {
        System.out.println("IOException :" + e);
      } catch (ParseException e) {
        System.out.println("Parse Exception :" + e);
      } 
    } catch (ArrayIndexOutOfBoundsException e) {
      System.out.println("Please enter valid 1 Arguments");
    } catch (Exception e) {
      System.out.println("Please enter valid FileName ");
    } 
  }
  
  public static void removeBlankPages(Document doc) throws Exception {
    for (Section s : doc.getSections()) {
      if (s.toString(70).trim().isEmpty())
        s.remove(); 
      doc.save(mergedFilePath);
    } 
  }
}

input files : Revised Manuscript.docx (44.8 KB)
Table 1 Attributes of LTCFs and the number of participants.docx (9.7 KB)
Table 2 Characteristics of the participants.docx (19.3 KB)
Table 3 Analysis application of Colaizzi’s seven-step.docx (13.2 KB)
Title Page.docx (27.3 KB)
Title Page_Merged.docx (43.2 KB)

Issue : File Name : Table 3 Analysis application of Colaizzi’s seven-step.docx
What aspose Reading : Table 3 Analysis application of Colaizzi’s seven-step.docx

@e503824 Aspose.Words does not deal with file names. As I can see from your code you are reading file names from JSON. It looks like the problem occurs either when file name is put into JSON, or when it is read from JSON.

dear team,

any think possible to help me on this issue

@e503824 I suspect the problem occurs in the following part of your code:

JSONObject order = (JSONObject)o;
if (count == 0) {
    firstFile = FilenameUtils.getName((String)order.get("FilePath"));
    firstFile = FilenameUtils.removeExtension(firstFile);
} 
String filePath = (String)order.get("FilePath");

Could you please debug this part of code and make sure this line String filePath = (String)order.get("FilePath") returns correct value. If not, make sure the file path is properly put into JSON.

thanks a lot

1 Like