0

I am attempting to modify innerxml of paragraph of a word document but the innerxml property refuses to be set. Following is the code I have tried:

static void Main(string[] args)
{
    string destinationWordFile = @"C:\Users\Testing\Modified Files\1216085_01012013.docx";
    using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(destinationWordFile, true, new OpenSettings { AutoSave = true }))
    {
        OpenXmlWdProcessing.RunProperties rp = new OpenXmlWdProcessing.RunProperties();
        MainDocumentPart mainPart = wordDoc.MainDocumentPart;
        var invoiceDocument = wordDoc.MainDocumentPart.Document;
        var po = (from body in invoiceDocument.Body
                               where body.InnerText.Contains("PO:")
                               select body).FirstOrDefault();
        string poInnerXml = po.InnerXml;
        string modifiedXML = poInnerXml.Remove(poInnerXml.IndexOf("w:w=\""), 10);
        po.InnerXml.Remove(0);
        po.InnerXml.Insert(0,modifiedXML);
        mainPart.Document.Save();
    }

Following is the XML I am targetting:

<w:pPr xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
    <w:pStyle w:val="Normal" />
    <w:framePr w:w="516" w:x="6881" w:y="7175" />
    <w:widowControl w:val="off" />
    <w:autoSpaceDE w:val="off" />
    <w:autoSpaceDN w:val="off" />
    <w:spacing w:before="0" w:after="0" w:line="179" w:lineRule="exact" />
    <w:ind w:left="0" w:right="0" w:first-line="0" />
    <w:jc w:val="left" />
    <w:rPr>
        <w:rFonts w:ascii="AATWWN+Helvetica" />
        <w:color w:val="000000" />
        <w:sz w:val="16" />
    </w:rPr>
</w:pPr>
<w:r xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
    <w:rPr>
        <w:rFonts w:ascii="AATWWN+Helvetica" />
        <w:color w:val="000000" />
        <w:sz w:val="16" />
    </w:rPr>
    <w:t>PO:                             111111111111</w:t>
</w:r>

I want to remove w:w="516" from <w:framePr w:w="516" w:x="6881" w:y="7175" />. Can anybody please advise me on this ? Many thanks in advance.

Ahmad Khan
  • 2,655
  • 19
  • 25
Vipul
  • 107
  • 2
  • 4
  • 13

2 Answers2

1

Vipul,

This is a piece of code i had lying around that I've modified to explain to you how to remove the width property.

//Get all paragraphs in the document
IEnumerable<Paragraph> paragraphs = doc.MainDocumentPart.Document.Body.OfType<Paragraph>();
foreach (Paragraph paragraph in paragraphs)
{
    FrameProperties framePr = paragraph.OfType<FrameProperties>().FirstOrDefault();
    framePr.Width = null;
}

You can change your code on similar lines. Setting the property value to null should work, I haven't tried it. Let me know if you face any problems.

Varun Rathore
  • 7,730
  • 3
  • 27
  • 30
0

Use this code to remove FrameProperties attributes.

//paragraph is the Paragraph object that needs to be processed.

FrameProperties frameProperties = pTag.Descendants<FrameProperties>().FirstOrDefault();
if (frameProperties != null)
{
    frameProperties.Width = null; //give string value if you want to set one say "516"
    // To set/remove height attribute use this.
    frameProperties.Height = null;
    //To Remove frame properties itself from paragraph
    frameProperties.Remove();
}

And save the package finally using this code.

//package is the object of WordProcessingDocument
package.MainDocumentPart.Document.Save();

Hope this helps!

Mohamed Alikhan
  • 1,315
  • 11
  • 14