Free Support Forum - aspose.com

File Merge

File Merge.zip (279.7 KB)
Hi Team,
In file merging i had merged the multiple file into a docx file . Here there is an issue like Text format is getting changed while merging.For your further details i have attached the Screenshort . Kindly give the solution for it.
Thank you.

@jan.kathir

To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word documents.
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.
  • Please create a standalone console application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

@tahir.manzoor

Input file will be json file.According to the json file, the file must be get append. The output merged file name should be 1st file in json followed _merged (eg.filename_Merged) must place in same location.
The Issue was Style of manuscript original file is normal+biblography after appending the _merged file is completely is in normal style. This issue i have attached as a screenshort. While appending there is no issue only thing is after appending the style getting changes under the topic of (References).
package com.sps;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.apache.commons.io.FilenameUtils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import com.aspose.words.Document;
import com.aspose.words.ImportFormatMode;
import com.aspose.words.SaveFormat;
import com.aspose.words.Section;

public class MergeFile {

static String json = null;
static String fs = File.separator;
static String mergedFilePath =null;
public static void main(String[] args) throws Exception {
	try {
		com.aspose.words.License license = new com.aspose.words.License();
		license.setLicense(MergeFile.class.getResourceAsStream("/Aspose.Total.Java.lic"));
		com.aspose.pdf.License license1 = new com.aspose.pdf.License();
		license1.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 {
			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);
				String directory = path.getParent().toString();
				mergedFilePath = 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, ImportFormatMode.KEEP_SOURCE_FORMATTING);
				finalDoc.save(mergedFilePath, SaveFormat.DOCX);
				count++;
			}
			Document test =new Document(mergedFilePath);
			removeBlankPages(test);
			System.out.println("Successfull");

		} 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(SaveFormat.TEXT).trim().isEmpty()) {
		s.remove();
	}
	doc.save(mergedFilePath );
}

}
}
Before Merge::(original)
image.png (118.4 KB)

After Merge::
image.png (118.4 KB)

I need the style which present in the original file should remains same after merging.Merge.zip (102.3 KB)

@jan.kathir

You are loading and saving final document (finalDoc) in the for loop. You need to load and save finalDoc outside the for loop. Please check the first and last line of code in following code snippet.

Document finalDoc = new Document(mergedFilePath);
for (Object o : a) {
   //Your code to get the file name  				
   Document doc = new Document(filePath);
  finalDoc.appendDocument(doc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
}

finalDoc.save(mergedFilePath, SaveFormat.DOCX);

You can also test this case by using the following code example. There is no issue with output document generated by this code example. We have attached it with this post for your kind reference. output.zip (43.9 KB)

Document finalDoc = new Document(MyDir + "Manuscript.docx");

Document doc = new Document(MyDir + "Manuscript_Duplicate_Report.html");
finalDoc.appendDocument(doc, ImportFormatMode.KEEP_SOURCE_FORMATTING);

doc = new Document(MyDir + "Table 1.docx");
finalDoc.appendDocument(doc, ImportFormatMode.KEEP_SOURCE_FORMATTING);

doc = new Document(MyDir + "Table 1_Duplicate_Report.html");
finalDoc.appendDocument(doc, ImportFormatMode.KEEP_SOURCE_FORMATTING);

doc = new Document(MyDir + "Table 2.docx");
finalDoc.appendDocument(doc, ImportFormatMode.KEEP_SOURCE_FORMATTING);

doc = new Document(MyDir + "Table 2_Duplicate_Report.html");
finalDoc.appendDocument(doc, ImportFormatMode.KEEP_SOURCE_FORMATTING);

doc = new Document(MyDir + "Table 3.docx");
finalDoc.appendDocument(doc, ImportFormatMode.KEEP_SOURCE_FORMATTING);

doc = new Document(MyDir + "Table 3_Duplicate_Report.html");
finalDoc.appendDocument(doc, ImportFormatMode.KEEP_SOURCE_FORMATTING);

finalDoc.save(MyDir + "output.docx", SaveFormat.DOCX);

@tahir.manzoor
1.As you said , if i try to save the finalDoc outside of the loop it not getting append its overriding only last file getting saved.
2.Here we are reading and appending only the “filepath” which present in the json file so we can’t save the file outside of the loop and also we don’t want to bother about rest of the files in the json.
3.There is no problem in appending a file externally, only thing is not to change a style of the content present in the _merged file.
4.After appending ,In the Manuscript file under References the style is in biblography but it turns into normal this was the issues.
5.As per your code , each json file have different content and different filepath so we cant direct write the file name we want to read the json file then only we can append them.
Kindly provide the solution for this .Thankyou

@jan.kathir

We are working over your query and will get back to you soon.

@jan.kathir

You are appending documents into main document (Manuscript.docx). You can read the document’s path for .json file in for loop that you want to append to main document.

For path of main document (Manuscript.docx), please read its path separately from the .json file.

You can also append all document to empty document as shown below. Hope this helps you.

Document finalDoc = new Document();
finalDoc.removeAllChildren();;

for (Object o : a) {
    Document doc = new Document(filePath from .json file);
    finalDoc.appendDocument(doc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
}
finalDoc.save(mergedFilePath);