Hi,
We are currently evaluating Aspose for Word, and one “simple” usecase is that we need to update a given document based on a new(er) template. Section.getBody.removeAllChildren() is supposed to clear body of document, but retain header/footer and other information. However, the headers, although apparently in the document, don’t appear when document is opened.
Code is almost ridiculously simple (the beauty of Aspose, naturally), but something doesn’t seem to work.
System.out.println("Opening template");
Document docTemplate = new Document(m_templateFile);
// Make a new copy
Document newDocument = docTemplate.deepClone();
// Clear sections, except headers
for (Section sec : newDocument.getSections()) {
if ( sec.getBody() != null ) {
sec.getBody().removeAllChildren();
}
}
// Headers are still here, but don't appear in Document
System.out.println(newDocument.getFirstSection().getHeadersFooters().getCount());
// Add content of old document here.
newDocument.appendDocument(doc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
newDocument.save(m_outputFile, com.aspose.words.SaveFormat.DOCX);
This is a bit puzzling, I have to admit, so any pointer are highly appreciated
Thx
@jkrauseno
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.
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.
Aspose problem.zip (199.7 KB)
Aspose problem (input docx).zip (173.7 KB)
Hi Tahir,
See attached Source, Target and Template documents. It doesn’t make any difference if I’m using a doc or docx file as input, but I added them anyway.
As I mentioned, we are currently evaluating Aspose, but I’m pretty sure we’ll be able to convince manager as well as customer to purchase it, since we do have some old POI 3.x code with needs to be replaced as soon as possible
Regards
Jørg
@jkrauseno
We have tested the scenario using the latest version of Aspose.Words for Java 19.5 and have not found the shared issue. So, please use Aspose.Words for Java 19.5. We have attached the output document with this post for your kind reference. output.zip (83.3 KB)
Thanks for your reply. Unfortunately, I am already using the 19.5 version, see attached screenshots.
Screenshots.zip (151.8 KB)
Apparently, the Document.appendDoc is appending entire document, so you may just leave out that code at this point, as I don’t want to header to be copied from the old document. I probably have to write some getBody() parts, but as of know, the headers are not appearing in my document
Quick update, downloaded jar file from your link, and the result is exactly the same. Not surprising since the jar file is rather identical
@jkrauseno
Please download the output document from my previous post and take the screenshots of problematic header/footer and share them.
Please manually create your expected Word document using Microsoft Word and attach it here for our reference. We will investigate as to how you want your final Word output be generated like. We will then provide you more information on this along with code.
Hi Tahir,
In your output, the header from template is showing up, which is perfect indeed. It doesn’t in mine, unfortunately.
The use case is very simple:
- Input document in doc or docx format, needs to be converted to new template in
- in docx format. (Modified header, custom properties etc)
- Remove content from template, preserving headers and properties
- Copy content (source.getBody) to new doc based on template
Manually created target.zip (89.0 KB)
@jkrauseno
We suggest you please read the following article.
Joining Documents with Headers and Footers
Please use the following code example to get the desired output.
Document doc = new Document(MyDir + "Source.doc");
Document docTemplate = new Document(MyDir + "Template.docx");
// Make a new copy
Document newDocument = docTemplate.deepClone();
// Clear sections, except headers
for (Section sec : newDocument.getSections()) {
if ( sec.getBody() != null ) {
sec.getBody().removeAllChildren();
}
}
// Headers are still here
System.out.println("Number of headers after removeAllChildren: " + newDocument.getFirstSection().getHeadersFooters().getCount());
doc.getFirstSection().getPageSetup().setSectionStart(SectionStart.CONTINUOUS);
newDocument.appendDocument(doc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
newDocument.getSections().get(1).getHeadersFooters().linkToPrevious(true);
newDocument.save(MyDir + "output.docx", com.aspose.words.SaveFormat.DOCX);
Hi Tahir,
thanks for the code, which certainly is looking great, apart that it doesn’t work Again, the document itself is appended correctly, but the header is entirely missing when open it in Word, even though console output looks like
Opening document C:\Development\Tieto\Source.doc in Aspose
Opening template C:\Development\Tieto\Template.docx
Number of headers after removeAllChildren: 2
Saving document as new file C:\Development\Tieto\Target.docx
It puzzles me quite a bit that your result is looking differently, as long as we are running with the same jar files. Only difference is that I’m in evaluation modus.
Target.zip (83.5 KB)
@jkrauseno
Please make sure that you are using the same documents. Moreover, we suggest you please get the temporary license and apply it. Please read the following article about applying the license.
Applying the License
Hi Tahir
Of course I am using exactly the same files when testing.
I even tried this now,
private static void makeNewFromTemplate2(Document doc) throws Exception {
System.out.println("Opening template " + m_templateFile);
Document docTemplate = new Document(m_templateFile);
// Make a new copy
Document newDocument = docTemplate.deepClone();
// Save of headers/footers
List<HeaderFooter> hfList = new ArrayList<HeaderFooter>();
for ( HeaderFooter hf : newDocument.getFirstSection().getHeadersFooters())
{
hfList.add(hf);
}
// Clear sections, except headers
for (Section sec : newDocument.getSections()) {
if ( sec.getBody() != null ) {
sec.getBody().removeAllChildren();
}
}
// Headers are still here, let's remove them
System.out.println("Number of headers after removeAllChildren: " + newDocument.getFirstSection().getHeadersFooters().getCount());
for ( HeaderFooter hf : newDocument.getFirstSection().getHeadersFooters())
{
hf.remove();
}
System.out.println("Number of headers after removing headers: " + newDocument.getFirstSection().getHeadersFooters().getCount());
// Adding them again
for ( HeaderFooter hf : hfList ){
newDocument.getFirstSection().getHeadersFooters().add(hf);
}
System.out.println("Number of headers after adding them again: " + newDocument.getFirstSection().getHeadersFooters().getCount());
doc.getFirstSection().getPageSetup().setSectionStart(SectionStart.CONTINUOUS);
newDocument.appendDocument(doc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
newDocument.getSections().get(1).getHeadersFooters().linkToPrevious(true);
// Add content of old document here. Must be implemented
System.out.println ("Saving document as new file " + m_outputFile);
newDocument.save(m_outputFile, com.aspose.words.SaveFormat.DOCX);
}
Simply removing and re-adding headers and footers, and the result is still the same, meaning no headers or footers. I will try the path with a temporary license, once I can convince manager to do so
Jørg
Tried it with same files, and temporary license, both with and without remove/add headers, still same result. With temporary license.zip (160.9 KB)
Really weird
@jkrauseno
We have opened the shared documents in MS Word 2016 and can see the header and footer. Please check the attached image for detail.
Hi Tahir,
this is either very weird, or extremely embarrassing, or probably both
Apparently, the newly created documents were opened in some unexpected View mode, NOT displaying the headers.
It looks perfect so far, thanks a ton for your help.
Jørg
@jkrauseno
It is nice to hear from you that your problem has been solved. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.
Hi Tahir,
Just almost solved, it seems The original problem is certainly working fine indeed, but I detected now that there is a difference between appending a doc and and docx document
The template is still exactly the same, and if you take a closer look at it, you’ll find that the header is actually protected with a password. Converting any docx source document works without any issues, meaning that header from template is still protected, and appended document in target is editable.
However, using exactly the same code (unchanged) on a doc document, locks the entire document
See attached input 22001.docx and 22004.doc, and the generated 22001 02.docx and 22004.docx.
Difference between doc and docx input.zip (289.0 KB)
Hi Tahir,
and I obviously tried to convert the doc to docx format as well, without any particular luck
Modified code to
if (doc.getOriginalLoadFormat() != com.aspose.words.LoadFormat.DOCX) {
doc.save(m_outputDir + fName + " 00.docx",
com.aspose.words.SaveFormat.DOCX);
Document docx = new Document(m_outputDir + fName + " 00.docx");
docx.getFirstSection().getPageSetup()
.setSectionStart(SectionStart.CONTINUOUS);
newDocument.appendDocument(docx,
ImportFormatMode.USE_DESTINATION_STYLES);
newDocument.getSections().get(1).getHeadersFooters()
.linkToPrevious(false);
} else {
doc.getFirstSection().getPageSetup()
.setSectionStart(SectionStart.CONTINUOUS);
newDocument.appendDocument(doc,
ImportFormatMode.USE_DESTINATION_STYLES);
newDocument.getSections().get(1).getHeadersFooters()
.linkToPrevious(true);
}
// Add content of old document here. Must be implemented
System.out.println("Saving document as new file " + m_outputDir + fName
+ " 02.docx");
newDocument.save(m_outputDir + fName + " 02.docx",
com.aspose.words.SaveFormat.DOCX);
But document content is still locked with same password as header.
@jkrauseno
We have tested the scenario and have managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-18693. You will be notified via this forum thread once this issue is resolved.
We apologize for your inconvenience.