I try to use the method appendDocument to merge two docx files using the ImportFormatMode KeepSourceFormatting. But merging doesn’t keep the format of the second docx file. Text without explicit formating (default formatting) takes the format of the text in the first file. Then the rendering is not correct in the final file.
You’re correct that this issue is happening because the first document does not have any default formatting defined. We have tried to improve the behaviour of documents in this situation being appended to one another but it appears the same issue with your documents is still occuring even in the latest version of Aspose.Words for .NET.
Please find attached an explanation of this issue and some potential work arounds. This article snippet is part of a larger article about appending documents which will be available with the next release of Aspose.Words.
In your case you either need to set default formatting for doc1.docx or prepend doc1 to doc2 instead. I have logged this issue. Hopefully there will be some improvement to the way documents with no default formatting are joined. We will keep you informed of any developments.
But for the moment, the only working solution is to modify all our documents and we have about 500 documents ! Then, can you say when the future release will be available ? Is it possible to obtain a bug fixed before ?
Thank you,
François
This message was posted using Email2Forum by GeorgeClark. (attachment)
I’m afraid this issue is very complex and it needs a lot more investigation before a fix can be produced. We will inform you as soon as a fix is ready. We apologise for any inconvenience.
Did you try prepending the documents instead? The output is the same but you may avoid these issues. Also, if the documents which have this problem are all using no line spacing before or after you can reset them in each document. Please see the code below.
foreach (Paragraph para in srcDoc.GetChildNodes(NodeType.Paragraph, true))
{
if(para.ParagraphFormat.SpaceBefore == 0)
para.ParagraphFormat.SpaceBefore = 0.0001;
if (para.ParagraphFormat.SpaceAfter == 0)
para.ParagraphFormat.SpaceAfter = 0.0001;
}
Thanks for your answer.
The solution may be to force the format of all paragraphs as you write. But when a paragraph uses the default value of SpaceAfter for example, the value of SpaceAfter is 0 even if the default value is set at 10.
Then, the format of paragraphs might be forced to default value of document. But I don’t find any method for getting the default values. How can we get the default values of ParagraphFormat in a document ?
Actually the issue occurs because your source document has no default paragraph spacing at all, If your source document did define a defaut SpaceAfter setting then I don’t think this issue would occur. Because of this I don’t think a property to get the default spacing in a document would help.
The above workaround shouldn’t affect other paragraphs in the document, as it only changes paragraphs which have no spacing set on the paragraph at all (which results in SpaceAfter = 0) or “0 pts” spacing directly set onto the paragraph (which also results in SpaceAfter = 0).
In either case the formatting is set directly onto the paragraph (by specifying a value different from zero but very close to it) which should avoid any problems when joining the documents.
If you run into any documents that don’t append properly using this please attach them and we will see if there any other work around that can be done in the mean time.
I attach example of two files I want to join with Aspose Words.
I read the SpaceAfter value of paragraphs with Aspose Words : all are set to 0 in both documents. But if you open the files in Word, in doc1 most of values are set to 10px and in doc2 values are set to 0. That’s why I can’t force the value to 0 : it’s correct for doc2 but not for doc1. I think there is a default value in documents and this is this value I have to applied for all paragraphs which don’t have any values.
Have you a solution to force all paragraphs in these documents to their correct values ?
The work around provided above should work as it is only changing the spacing of paragraphs in the source document (is called before the documents are joined).
If you have no sure idea which documents being processed contain this issue then you can try one more solution. Please see the code below and the methods attached to this post which will open up the original DOCX and extract the default spacing from styles.xml. To use this you will need SharpZipLib from here. You can call it in your code like below. It should be called on each document before joining.
Document doc = new Document("doc1.docx");
ResetMissingSpacingDefaultsOnParagraphs(doc);
I don’t think this is a fulll solution though as I there can be defaults set on paragraph styles as well so I’m not sure if they will cause an issue sometime as well
I can read the default values in the docx file and apply it on all
paragraphs. But I can’t see the difference between a paragraph with no
values an a paragraph with value 0 using
ParagraphFormat.getSpaceAfter(), in both cases the method return 0. Is
it possible to see the difference whith Aspose Word ? Have you a
solution for that ?
Secondly, does this problem exist with other properties in the docx file
? For example, I have documents with left text align (default value)
and documents with center text align. When I join these documents, left
align text sometimes becomes center align text. Is there a method to
avoid this problem ?
I attach my Java code to force paragraph format (I work with Aspose Word Java). I use the library jDom to read xml files.
No currently there is no method to find this, that is why the value needs to be found manually in the xml as achieved in the code attached. I have logged a request for this property. You will be informed as soon as it’s avaliable.
I have not yet seen any documents where this occurs on alignment. Could you please attach your documents here for testing?
Thank you for additional information. If I do the same operation using MS Word I get the same result as using Aspose.Words, so I think there is no problem here. But I logged your request to out defect database for further investigation.
Thanks for your help.
I force the 0 value only in documents without default values. All other of my documents share sames default values then this solve my problem for the moment.
For the alignment problem, I have few documents with a Normal style centered. Then I only correct them.
Thanks to inform me about corrections concerning these problems.