Splitting and including word document replaces section new page break with section continuous break

Hi Team,

I’m using Aspose.Words For Java v14.5.
Splitting and including word document replaces section new page break with section continuous break.
Take look at ref_1.doc and ref_2.doc. These two docs gets included into main.doc. The section new page break in ref_1.doc get’s replace with section continuous break in the final main.doc.

Thanks,
Kumar

public class TestMultipart1
{
public static void main(String[] args) throws Exception
{
Utils.setupLicense();
Utils.printAsposeVersion();
Document doc = new Document();
DocumentBuilder docBuilder = new DocumentBuilder(doc);

writeDocWithBreaks(docBuilder);

int count = doc.getSections().getCount();

int currentSectionIndex = 0;
List serializedParts = new ArrayList();
List sectionList = new ArrayList();
boolean continueLoop = true;
while (continueLoop)
{
Section section = doc.getFirstSection();
if (count == 1)
{
docBuilder.moveToDocumentEnd();
docBuilder.insertBreak(BreakType.SECTION_BREAK_CONTINUOUS);
docBuilder.getCurrentSection().getPageSetup().setOrientation(Orientation.PORTRAIT);
continueLoop = false;
}

sectionList.add(section);
if (count == 1 || count == 3)
{
Document partDocument = new Document();
partDocument.getFirstSection().remove();
addSectionsToPartDocument(sectionList, partDocument);
String partPath = “c:/ref_” + currentSectionIndex++ + “.doc”;
partDocument.save(partPath);
serializedParts.add(partPath);
sectionList = new ArrayList();
}

doc.getFirstSection().remove();
count = doc.getSections().getCount();
}

for (String path : serializedParts)
{
insertIncludeTextField(docBuilder, “file:/” + path);
docBuilder.writeln();
}

doc.updateFields();
doc.save(“C:/main.doc”);

System.out.println(“done”);
}

private static void addSectionsToPartDocument(List sectionsList, Document partDocument) throws Exception
{
for (Section section : sectionsList)
{
Node dstSection = partDocument.importNode(section, true, ImportFormatMode.KEEP_SOURCE_FORMATTING);
partDocument.getSections().add(dstSection);
}
}

private static void insertIncludeTextField(DocumentBuilder docBuilder, String path) throws Exception
{
String field = “INCLUDETEXT “” \* MERGEFORMAT”; //$NON-NLS-1$
field = field.replace("", path); //$NON-NLS-1$
docBuilder.insertField(field, “”); //$NON-NLS-1$
}

private static void writeDocWithBreaks(DocumentBuilder docBuilder) throws Exception
{
// section 1 - ref_0.doc
docBuilder.write(“1A”);
docBuilder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);

// section 2
docBuilder.write(“2”);
docBuilder.writeln();
docBuilder.write(“3”);

// section 3 - ref_1.doc
docBuilder.insertBreak(BreakType.SECTION_BREAK_CONTINUOUS);
docBuilder.write(“4”);

// section 4
docBuilder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
}
}

Hi Kumar,

Thanks for your inquiry. In your case, I suggest you please insert the section break continues after inserting INCLUDETEXT fields as shown below.

Please let us know if you have any more queries.


for (String path : serializedParts)

{

insertIncludeTextField(docBuilder, "file:/" + path);

docBuilder.insertBreak(BreakType.SECTION_BREAK_CONTINUOUS);

}

doc.updateFields();

doc.save(MyDir + "Out.doc");

Hi Tahir,

I found a case where adding section break continuous results in an incorrect output. However, if I add docBuilder.println(), the result doc is good.

Thanks,
Kumar

private static void writeDocWithBreaks(DocumentBuilder docBuilder) throws Exception
{
// section 1 - ref_1.doc
docBuilder.write(“Text 1”);
docBuilder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);

// section 2
docBuilder.write(“Text 3”);

// section 3 - ref_2.doc
docBuilder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
docBuilder.write(“Text 2”);
}

Hi Kumar,

Thanks for your inquiry. Aspose.Words shows the correct output in both cases. Please note that Aspose.Words mimics the same behaviour as MS Word does. If you repeat the both scenario using MS Word, you will get the same output.

Hi Tahir,

The issue here is, when should I used section break continuous and when should I use docBuilder.println()? Is there a generic solution that works that retains the structure of the reference document within the main document after INCLUDETEXT field code?

{INCLUDETEXT “file:///c:/ref_0.doc” * MERGEFORMAT}
{INCLUDETEXT “file:///c:/ref_1.doc” * MERGEFORMAT}

Thanks,
Kumar

Hi Kumar,

Thanks for your inquiry. Please refer to the following section of documentation which outlines how to insert a Document into another Document:
http://www.aspose.com/docs/display/wordsjava/How+to++Insert+a+Document+into+another+Document

Please insert the documents (mentioned in INCLUDETEXT field) into main document using the code shared in above documentation link. Hope this helps you.

Hi Tahir,

Looking at the code in the link you provided, it seems that the sections are manually inserted from dest doc to src doc but not as FIELDs.

The target is to link/include reference documents into the source document as FIELD {INCLUDETEXT file}. But this is causing difference when done from Aspose. Is this a defect or is there a right to to achieve the same?

Thanks,
Kumar

Hi Kumar,

Thanks for your inquiry. I have worked again with both cases shared by you and like to share with you that you are facing the correct behavior of Aspose.Words. Please note that Aspose.Words
mimics the same behavior as MS Word does. If you perform the both
scenario using MS Word, you will get the same output.


Please insert the INCLUDETEXT field with value ref_0.doc and ref_1.doc in Word document using MS Word. After inserting { INCLUDETEXT ref_0.doc }, insert the paragraph or section break and check the behavior of MS Word. In both cases, Aspose.Words and MS Word behavior is same.

Hope this answers your query. Please let us know if you have any more queries.

Hi Tahir,

Let’s make it simple.

In the first case, you asked me to use the following code.
"docBuilder.insertBreak(BreakType.SECTION_BREAK_CONTINUOUS);"

For for the 2nd case, if I use the above code, the final doc is not same as ref_0.doc and ref_1.doc. Instead, if I use docBuilder.writeln(), the final doc matches ref_0.doc and ref_1.doc. Why is this difference happening?

Thanks,
Kumar

Hi Kumar,

Thanks for your inquiry. I have worked with your code again and have found a small issue which causes this new behavior of MS Word.

You are removing the first section of ref_xx.doc document and importing new sections from another document. In this case, please set the SectionStart (the type of break at the beginning of the section) of first imported section. Please check the highlighted code below.


Document
doc = new Document();

DocumentBuilder docBuilder = new DocumentBuilder(doc);

writeDocWithBreaks(docBuilder);

int count = doc.getSections().getCount();

int currentSectionIndex = 0;

java.util.List<String> serializedParts = new ArrayList<String>();

java.util.List<Section> sectionList = new ArrayList<Section>();

boolean continueLoop = true;

while (continueLoop)

{

Section section = doc.getFirstSection();

if (count == 1)

{

docBuilder.moveToDocumentEnd();

docBuilder.insertBreak(BreakType.SECTION_BREAK_CONTINUOUS);

docBuilder.getCurrentSection().getPageSetup().setOrientation(Orientation.PORTRAIT);

continueLoop = false;

}

sectionList.add(section);

if (count == 1 || count == 3)

{

Document partDocument = new Document();

addSectionsToPartDocument(sectionList, partDocument);

String partPath = "c:/ref_" + currentSectionIndex++ + ".doc";

int sectionstart = partDocument.getFirstSection().getPageSetup().getSectionStart();

partDocument.getFirstSection().remove();

partDocument.getFirstSection().getPageSetup().setSectionStart(sectionstart);

partDocument.save(partPath);

serializedParts.add(partPath);

sectionList = new ArrayList<Section>();

}

doc.getFirstSection().remove();

count = doc.getSections().getCount();

}

for (String path : serializedParts)

{

insertIncludeTextField(docBuilder, path);

docBuilder.writeln();

}

doc.updateFields();

doc.save(MyDir + "Out.doc");

Thanks very much Tahir. I’ll verify this and get back sooner.

Hi Tahir,

I found another failure case. I’ve attached standalone code for the same.

The difference happens with what is inserted after including a INCLUDETEXT field.
insertIncludeTextField(docBuilder, “file:/” + path);
docBuilder.writeln();
// docBuilder.insertBreak(BreakType.SECTION_BREAK_CONTINUOUS);


TestMultipart1 - If empty line is inserted, the output is incorrect.
TestMultipart2 - If continuous section break is inserted, the output is incorrect.

Thanks
Kumar

Hi Kumar,

Thanks for your inquiry. I
have checked both scenario and in both cases, Aspose.Words generated the correct output. For TestMultipart1, please check the attached image (case1.png).

For TestMultipart2, the section break continues changes to page break (see case2.png). This is the expected behavior of Aspose.Words. Please repeat this scenario using MS Word, you will get the same output.

In your case, I suggest you please do not insert section breaks after INCLUDETEXT fields. Use the section start of first section of documents used in INCLUDETEXT fields according to your requirements.

Hi Tahir,

>>> In your case, I suggest you please do not
insert section breaks after INCLUDETEXT fields. Use the section start of
first section of documents used in INCLUDETEXT fields according to your
requirements.

Did you mean the following code? It doesn’t help.

for (String path : serializedParts)
{
insertIncludeTextField(docBuilder, “file:/” + path);
Document partDocument = new Document(path);

docBuilder.getCurrentSection().getPageSetup().setSectionStart(partDocument.getFirst/LastSection().getPageSetup().getSectionStart());
}

Thanks,
KUmar

Hi Kumar,

Thanks for your inquiry. I
have logged a ticket as WORDSNET-10786 for this issue in our issue tracking system. Our development team will look into this issue. We will update you via this forum thread once there is any update available on this scenario.

Please let us know if you have any more queries.

The issues you have found earlier (filed as WORDSNET-10786) have been fixed in this .NET update and this Java update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.