Merged documents causing Word to crash when printing

I have created a “package” document by taking 3 separate document and combining them into one using Aspose.Word 3.0.1. When I open this document everything seems fine, but when I try to print it Word crashes.

I have created other package documents using different source docs and have not had this problem. Additionally, I can open each of the individual documents (before combining them) and print them just fine.

I have attached my combined document that is crashing Word for you to look at. Please let me know if you need any more information.

spotvader@yahoo.com

I just noticed that the issue I am having may be related to another issue in the forum… https://forum.aspose.com/t/127750

Hi,

Thank you, please check back for the reply in 1-2 days.

I see the “run off” effect in this document. This certainly did not happen on my 3.0.2 code base, it must have been related to copying text between documents and in 3.0.2 styles are copied correctly between documents.
I think printing might have also been affected by this so just wait until 3.0.2 is out. You will need to regenerate this document of course.

I have upgraded my Aspose.Word to version 3.0.3 and am having the exact same problem. The final combined document still causes Word to crash when printing. I have attached my final “combined” document for you to look at.

Open your original documents in MS Word, switch to the header/footer and delete that floating textboxes with two empty paragraphs that sits in the middle of the page. Aspose.Word does not deal with floating objects very well when they are duplicated or combined from several documents.

romank,

Thanks I will try that. I think the empty objects may be there because we have a mergefield called “watermark” in the header of the document. Depending on the status of the document (business rule), we either insert a watermark or don’t into that mergefield. Perhaps the empty object is there in the case where we don’t insert the watermark. If this is the case, is there a way for me to remove the watermark object from the header dynamically in code?

spotvader

In your case that floating textbox will be a Shape object inside a HeaderFooter object. Shape derives from InlineStory and can contain Paragraph and Table objects. For that particular document you can find the Shape (or all Shape objects) in your HeaderFooters, maybe check their text is one or two empty paragraphs and then delete them. It could looks something like this just to give you an idea:

foreach (Section section in myDoc.Sections)
{
foreach (HeaderFooter headerFooter in section.HeadersFooters)
{
NodeList shapes = headerFooter.SelectNodes(".//Shape");
for (int i = 0; i < shapes.Count; i++)
{
if (shape.GetText() == “\r\r”)
{
//In this case it is safe to delete shapes in a loop because NodeList is a static snapshot of the selection.
shape.ParentNode.RemoveChild(shape);
}
}
}
}


alternatively, if you sure you are not supposed to have any floating shapes in your headers footers do this:
foreach (Section section in myDoc.Sections)
{
section.DeleteHeaderFooterShapes();
}

romank,

I tried to do the first scenario you supplied but get an error when tyring to execute the code (everything compiles correctly, only get the error at runtime).

Here is my code…

foreach (Aspose.Word.Section thisSection in myPackageDoc.Sections)
{
foreach (Aspose.Word.HeaderFooter headerFooter in thisSection.HeadersFooters)
{
NodeList shapes = headerFooter.SelectNodes(".//Shape");
for (int i = 0; i < shapes.Count; i++)
{
Aspose.Word.Shape myShape = (Aspose.Word.Shape)shapesIdea;
if (myShape.GetText() == “\r\r”)
{
//In this case it is safe to delete shapes in a loop because NodeList is a static snapshot of the selection.
myShape.ParentNode.RemoveChild(myShape);
}
}
}
}


The error I am getting is:

Operation is not valid due to the current state of the object.
foreach (Aspose.Word.HeaderFooter headerFooter in thisSection.HeadersFooters)

Hi,

Please try to use for loops instead of foreach:

for (int sectionIndex = 0; sectionIndex < doc.Sections.Count; sectionIndex++)
{

Section thisSection = doc.Sections[sectionIndex];
for (int hfIndex = 0; hfIndex < thisSection.HeadersFooters.Count; hfIndex++)
{
HeaderFooter headerFooter = thisSection.HeadersFooters[hfIndex];
NodeList shapes = headerFooter.SelectNodes(".//Shape");

for (int i = 0; i < shapes.Count; i++)
{
Aspose.Word.Shape myShape = (Aspose.Word.Shape)shapesIdea;
if (myShape.GetText() == “\r\r”)
{
//In this case it is safe to delete shapes in a loop because NodeList is a static snapshot of the selection.
myShape.ParentNode.RemoveChild(myShape);
}
}
}
}