Free Support Forum - aspose.com

Mail merge value with Page Break

Hi all,

Does anyone know if it is possible to just insert a specific character which will cause a page break? Currently, I have got an input field which implements a rich text format editor for the user to type in the content that would be then used as a mail merge field.

The user however would like the option to be able to specifically specify as to when they would like a page break within the content that they have input into that field.

I’ve tried inserting the control characters for pagebreak directly in the field that the user is typing into but it appears to be reading it as a text instead. Just wondering if there is any other way to go about this without having to process the user’s input to search/identify that particular pagebreak character and then manually force a page break when writing it into the document.

Thank you.

@eckc,

Please see these sample input/output Word documents (Docs.zip (17.4 KB)) and try running the following code:

Document doc = new Document("D:\\Temp\\in.docx");

doc.MailMerge.Execute(new string[] { "mf" }, 
            new object[] { "Page 1 " + ControlChar.PageBreak + "Page 2" });           

doc.Save("D:\\Temp\\18.7.docx");

Hi there @awais.hafeez ,

Thanks for the assistance.

I have managed to actually overcome this issue with the following:
Snap 2018-07-16 at 12.57.40.png (15.6 KB)

However, now I am plagued with a new issue. It seems whenever there is data with the same mergefield name, the program ends up getting confused and ends up referring back to the previous field.

For example, I have the following data:

  <APPLICANT>
  	<SALUTATION>MR</SALUTATION>
  	<FULLNAME>James</FULLNAME>
  	<ALIASNAME>Doe</ALIASNAME>
  </APPLICANT>
  <APPLICANT>
  	<SALUTATION>MR</SALUTATION>
  	<FULLNAME>John</FULLNAME>
  	<ALIASNAME>Archibald</ALIASNAME>
  </APPLICANT>
  <SALUTATION>Sir |pagebreak|</SALUTATION>

In the example, I’m expecting it to only insert the pagebreak in the last data, however it appears to be inserting it for all fields using as the tag.

Appears to be related to https://forum.aspose.com/t/movetomergefield-where-the-is-more-than-one-mergefield-with-the-same-name/58677 which supposedly is fixed but I still find it occurring in my case.

@eckc,

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

  • Your input Word document
  • XML file containing the data
  • Aspose.Words generated output DOCX file showing the undesired behavior.
  • Your expected Word document showing the correct output. Please create this document by using MS Word.
  • Please create a standalone simple Java 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 further investigation into your above issue and provide you more information. Thanks for your cooperation.

Hi @awais.hafeez,

Thanks for the prompt response.

For some reason unknown, I am unable to upload the zip file, no error prompted and the uploaded file does not show up. Anyway, I uploaded it to google drive and you can get it from:

https://drive.google.com/open?id=1yYe3Kje9vx39Caeq_oSWpjfxf9-dRSxx

@eckc,

You have shared the .jar files; but we need source code. Please also provide standalone simple Java application (source code without compilation errors) that helps us to reproduce your problem on our end. Thanks for your cooperation.

Ah, the source code is just within the jar. Only need to unzip it. Either way, here you go. word2pdf-sources.zip (3.6 KB)

@eckc,

Please check the following simple code to get the desired output:

com.aspose.words.net.System.Data.DataSet ds = new DataSet();
ds.readXml("D:\\Temp\\letterContent.xml");

Document doc = new Document("D:\\Temp\\LetterTemplate.docx");

doc.getMailMerge().executeWithRegions(ds);
doc.getRange().replace("|pagebreak|", ControlChar.PAGE_BREAK, new FindReplaceOptions());

doc.updateFields();
doc.updateListLabels();
doc.updatePageLayout();

PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.getOutlineOptions().setDefaultBookmarksOutlineLevel(2);
saveOptions.getOutlineOptions().setHeadingsOutlineLevels(2);

doc.save("D:\\Temp\\awjava-18.7.pdf", saveOptions);

Hi @awais.hafeez,

Thanks for the suggestion. So it is doing a full doc replacement after merge. Brilliant!