We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Aspose.Words for Java Regions breaking in PDF when Word document contains PAGE or NUMPAGES Merge Fields

We ran into an issue when using Aspose.Words for Java to save as a merged document as a PDF. If the document contains the PAGE or NUMPAGES field the main (getMailMerge().execute()) merge fields are populated but any fields contained in regions/tables simply show up as merge fields. If I simply change it to SaveFormat.DOC it will create the word document with everything properly filled out. Even using setRemoveEmptyRegions() it still shows the merge fields on the PDF. I’m attaching a basic test case to demonstrate the issue along with a word document.




Code:
import java.io.FileInputStream;
import java.io.FileOutputStream;

import com.aspose.words.Document;
import com.aspose.words.FontSettings;
import com.aspose.words.IMailMergeDataSource;
import com.aspose.words.SaveFormat;

public class AsposeTest {

	String pdfFolder = "";
	public AsposeTest() throws Exception {
		Document d = new Document(new FileInputStream("/Users/cking/Documents/TestMerge.docx"));
		d.getMailMerge().setRemoveEmptyRegions(true);
		
		d.getMailMerge().execute(new MainMailMerge());
		d.getMailMerge().executeWithRegions(new RegionMailMerge());
		
		FontSettings.setFontsFolder(pdfFolder, true);
		FileOutputStream fos = new FileOutputStream("/Users/cking/Documents/TestMergeOutput.pdf");
		d.save(fos, SaveFormat.PDF);
		fos.close();
	}
	public static void main(String[] args) throws Exception {
		new AsposeTest();
	}
	
	class MainMailMerge implements IMailMergeDataSource {

		private int index = 0;
		@Override
		public IMailMergeDataSource getChildDataSource(String arg0)
				throws Exception {
			return null;
		}

		@Override
		public String getTableName() throws Exception {
			return "Main";
		}

		@Override
		public boolean getValue(String fieldName, Object[] fieldValue) throws Exception {
			if (fieldName.equals("Test1")) fieldValue[0] = "Input for Test1";
			if (fieldName.equals("Test2")) fieldValue[0] = "Input for Test2";
			return true;
		}

		@Override
		public boolean moveNext() throws Exception {
			if (index++>0) return false;
			return true;
		}
		
	}
	
	class RegionMailMerge implements IMailMergeDataSource {
		private int index = 1;
		@Override
		public IMailMergeDataSource getChildDataSource(String arg0)
				throws Exception {
			return null;
		}

		@Override
		public String getTableName() throws Exception {
			return "Region";
		}

		@Override
		public boolean getValue(String fieldName, Object[] fieldValue) throws Exception {
			if (fieldName.equals("RegionTest1")) fieldValue[0] = "Input for RegionTest1";
			if (fieldName.equals("RegionTest2")) fieldValue[0] = "Input for RegionTest2";
			return true;
		}

		@Override
		public boolean moveNext() throws Exception {
			if (index++>2) return false;
			return true;
		}
		
	}
}

Hi Christian,

Thanks for your request. You should call Document.updatePageLayout before saving document to PDF to resolve the problem. Please see the following code:

doc.updatePageLayout();

doc.save("C:\\Temp\\out.pdf");

The problem occurs because when you execute mail merge, Aspose.Words automatically updates fields in the document. Updating fields also runs updating page layout and the layout is cached. So changes made after updating page layout does not appear in the output PDF. So you should simply rebuild document layout to resolve the problem.

Best regards,