-
Notifications
You must be signed in to change notification settings - Fork 556
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Word file is corrupted after replacing the header in a word document which has section breaks using Open XML SDK #1864
Comments
Hi @sani-j, The section break is a red herring in this case. There are 2 issues with your code. First So, update your code to read: IEnumerable<DocumentFormat.OpenXml.Wordprocessing.SectionProperties> sectPrs = mainPart.Document.Body.Descendants<SectionProperties>(); Also, the sectPr.PrependChild<HeaderReference>(new HeaderReference() { Id = rId, Type = HeaderFooterValues.Default }); If you run into similar issues in the future, you can use the OpenXmlValidator Class to find these issues. Or if you prefer a UI. I created a wrapper for the Validator in VSCode: OOXML Validator. Below is the code from your earlier post updated with the fixes. Since this has been answered, I will close this issue. using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
string fromFile = @"C:\path\to\fromFile.docx";
string toFile = @"C:\path\to\toFile.docx";
AddHeaderFromTo(fromFile, toFile);
static void AddHeaderFromTo(string fromFile, string toFile)
{
// Replace header in target document with header of source document.
using (WordprocessingDocument wdDocTo = WordprocessingDocument.Open(toFile, true))
using (WordprocessingDocument wdDocFrom = WordprocessingDocument.Open(fromFile, true))
{
if (wdDocFrom.MainDocumentPart is null || wdDocFrom.MainDocumentPart.HeaderParts is null)
{
throw new ArgumentNullException("MainDocumentPart and/or HeaderParts is null.");
}
if (wdDocTo.MainDocumentPart is null)
{
throw new ArgumentNullException("MainDocumentPart is null.");
}
MainDocumentPart mainPart = wdDocTo.MainDocumentPart;
// Delete the existing header part.
mainPart.DeleteParts(mainPart.HeaderParts);
// Create a new header part.
DocumentFormat.OpenXml.Packaging.HeaderPart headerPart = mainPart.AddNewPart<HeaderPart>();
// Get Id of the headerPart.
string rId = mainPart.GetIdOfPart(headerPart);
// Feed target headerPart with source headerPart.
DocumentFormat.OpenXml.Packaging.HeaderPart? firstHeader = wdDocFrom.MainDocumentPart.HeaderParts.FirstOrDefault();
wdDocFrom.MainDocumentPart.HeaderParts.FirstOrDefault();
if (firstHeader is not null)
{
headerPart.FeedData(firstHeader.GetStream());
}
if (mainPart.Document.Body is null)
{
throw new ArgumentNullException("Body is null.");
}
// Get SectionProperties and Replace HeaderReference with new Id.
// Elements gets only direct children, use Descendants instead to find at any level
IEnumerable<DocumentFormat.OpenXml.Wordprocessing.SectionProperties> sectPrs = mainPart.Document.Body.Descendants<SectionProperties>();
foreach (var sectPr in sectPrs)
{
sectPr.RemoveAllChildren<HeaderReference>();
// Create the new header reference node.
sectPr.PrependChild<HeaderReference>(new HeaderReference() { Id = rId, Type = HeaderFooterValues.Default }); // This needs the Type to be valid
}
}
} |
Thank you. |
Describe the bug
Hi, I am using the replace the header in a word processing document code which uses two documents. The header in fromFile.docx to be replaced in toFile.docx. The file toFile.docx already have section breaks. The section breaks can be added using Layout tab in word, select Breaks --> Section Breaks--> Next Page and save the document. After the header replacement code execution the toFile.docx became corrupted. Facing this issue and how this can be fixed. The code is given below.
To Reproduce
Create fromFile.docx and toFile.docx files with headers.
Add section breaks to toFile.docx. The section breaks can be added using Layout tab in word, select Breaks --> Section Breaks--> Next Page and save the document. Execute the above code.
Observed behavior
A popup will be shown while opening the toFile.docx after the above code execution which says 'word found unreadable contents in toFile.docx. Do you want to recover the contents of this document? '.
Expected behavior
Open the file without corruption or any error,
Desktop (please complete the following information):
Thanks in advance.
The text was updated successfully, but these errors were encountered: