Hello Team,
I am trying to append one by one pages from one document to another document then output document in some paragraph line alignment getting disturb. Its by default set to justify alignment.
Snippet :
Aspose.Words.Document outputDoc = new Aspose.Words.Document();
outputDoc.RemoveAllChildren();
byte[] sectionData;
Aspose.Words.Document sourceDoc = (Aspose.Words.Document)outputDoc.Clone(false);
sourceDoc.RemoveAllChildren();
sectionData = Encoding.UTF8.GetBytes(@"C:\\sourceDoc.docx");
sourceDoc = new Aspose.Words.Document(new MemoryStream(sectionData));
for (int j = 0; j < sourceDoc.PageCount; j++)
{
Aspose.Words.Document Page = (Aspose.Words.Document)sourceDoc.Clone(false);
Page.RemoveAllChildren();
Page = sourceDoc.ExtractPages(j, 1);
var pageText = Page.FirstSection.Body.ToString(SaveFormat.Text) + Page.LastSection.Body.ToString(SaveFormat.Text);
var shapeCount = Page.FirstSection.Body.GetChildNodes(Aspose.Words.NodeType.Shape, true).Count + Page.LastSection.Body.GetChildNodes(Aspose.Words.NodeType.Shape, true).Count;
if (string.IsNullOrEmpty(pageText.Trim()) && shapeCount == 0)
continue;
outputDoc.AppendDocument(Page, ImportFormatMode.KeepSourceFormatting);
}
outputDoc.Save(@"C:\\outputDoc.docx");
sourceDoc.docx (192.9 KB)
outputDoc.docx (47.5 KB)
Please refer below screen shorts:
@AlpeshChaudhari12345 This is a similar issue as you have reported in another your thread.
If optimize the destination document for MS Word 2019, the output looks correct:
Aspose.Words.Document outputDoc = new Aspose.Words.Document();
outputDoc.RemoveAllChildren();
outputDoc.CompatibilityOptions.OptimizeFor(MsWordVersion.Word2019);
Document sourceDoc = new Aspose.Words.Document(@"C:\Temp\sourceDoc.docx");
for (int j = 0; j < sourceDoc.PageCount; j++)
{
Document Page = sourceDoc.ExtractPages(j, 1);
var pageText = string.Join("", Page.GetChildNodes(NodeType.Body, true).Select(b => b.ToString(SaveFormat.Text).Trim()));
var shapeCount = Page.GetChildNodes(NodeType.Shape, true).Where(s => s.GetAncestor(NodeType.Body) != null).Count();
if (string.IsNullOrEmpty(pageText.Trim()) && shapeCount == 0)
continue;
outputDoc.AppendDocument(Page, ImportFormatMode.KeepSourceFormatting);
}
outputDoc.Save(@"C:\Temp\out.docx");
Also, I have refactored the code a bit.
@alexey.noskov yes thanks for your response
1 Like
can i check the MS Word version of document ?
@AlpeshChaudhari12345 You can use BuiltInDocumentProperties.Version property. This property represents the version number of the application that created the document. Following are the version numbers for MS Word.
11 = Word 2003
12 = Word 2007
14 = Word 2010
15 = Word 2013
16 = Word 2016
Document doc = new Document();
int MSWVersion = doc.BuiltInDocumentProperties.Version >> 16;
Console.WriteLine(MSWVersion);