Free Support Forum - aspose.com

How to remove the *"$" *sign and *","* Comma only TotalCost in footer section. No need to remove other sections in footer?

we are using aspose-word version 14.12.0.0. Our requirement is existing word document in footer section, we want to remove “$”, “,” only Total Cost in footer section.No need to other sections in footer. How can i achieve this and also we attached the sample project and screen shots for your review.

Please review the our code in attached project and give me the solution asap.

Note: Please include the dll aspose-word version 14.12.0.0.

FooterIssue.zip (640.9 KB)
FooterIssue1.png (23.2 KB)
FooterIssue2.png (23.9 KB)

We are using following code for this requirements.

protected void btn_Click(object sender, EventArgs e)
{
string File = “C:\FooterIssue.Docx”;
Aspose.Words.Document doc = new Aspose.Words.Document(File);

        List<string> Tags = new List<string>();

        Dictionary<string, string> aProductCost = new Dictionary<string, string>();
        aProductCost.Add("PL_PRODUCT_COST", "$2,500");
        aProductCost.Add("PL_ORDER_COST", "$2,000");
        aProductCost.Add("PL_QUANTITY", "3");
        aProductCost.Add("PL_TOTAL_COST", "$7,500");

        List<Aspose.Words.Markup.StructuredDocumentTag> sdtNodes = doc.GetChildNodes(NodeType.StructuredDocumentTag, true).Cast<Aspose.Words.Markup.StructuredDocumentTag>().ToList<Aspose.Words.Markup.StructuredDocumentTag>();

        string Value = string.Empty;
        foreach (Aspose.Words.Markup.StructuredDocumentTag cc in sdtNodes)
        {
            Value = aProductCost[cc.Title];
            if (Value != null)  //when found, apply the value. 
            {
                mergeDataToContentControl(cc, Value);
            }
        }

        foreach (Section sect in doc.Sections)
        {
            ProcessFooter(sect, HeaderFooterType.FooterFirst, "$");
            ProcessFooter(sect, HeaderFooterType.FooterPrimary, "$");
            ProcessFooter(sect, HeaderFooterType.FooterEven, "$");

            ProcessFooter(sect, HeaderFooterType.FooterFirst, ",");
            ProcessFooter(sect, HeaderFooterType.FooterPrimary, ",");
            ProcessFooter(sect, HeaderFooterType.FooterEven, ",");
        }
                    
        System.IO.MemoryStream outStream = new System.IO.MemoryStream();
        doc.Save(outStream, Aspose.Words.SaveFormat.Docx);
        byte[] docBytes = null;
         docBytes = outStream.ToArray();
         FileStream output = new FileStream("C:\\FooterIssue1.Docx", FileMode.Create,
         FileAccess.Write);
         // Write byte array contents in the output file stream**
         output.Write(docBytes, 0, docBytes.Length);
        // Close output file
         output.Close();
    }

    private static void ProcessFooter(Section sect, HeaderFooterType headerType, string pattern)
    {
        HeaderFooter footer = sect.HeadersFooters[headerType];

        if (footer != null)
        {
            // Replace
            footer.Range.Replace(pattern, "", false, false);
        }
    }

    private void mergeDataToContentControl(Aspose.Words.Markup.StructuredDocumentTag tag, string value)
    {
        var paragraphs = tag.GetChildNodes(NodeType.Paragraph, true).ToArray();
        if (paragraphs != null && paragraphs.Length > 0)
        {

            Run run = null;
            Paragraph par = (Paragraph)paragraphs[0];

            if (par.Runs != null && par.Runs.Count > 0)
            {
                run = (Run)par.Runs[0].Clone(true);
                run.Text = value;
            }
            else
            {
                run = new Run(tag.Document, value);
            }

            par.Runs.Clear();
            par.Runs.Add(run);
        }
        else  //case: when there is no paragraph as part of the Content Control.
        {
            var runs = tag.GetChildNodes(NodeType.Run, true).ToArray();
            if (runs != null && runs.Length > 0)
            {
                Run r = (Run)runs[0];
                r.Text = string.Empty;
                r.Text = value;
            }
            else
            {
                tag.AppendChild(new Run(tag.Document, value));
            }
        }
    }

@smani

Thanks for your inquiry. In your case we suggest you following solution.

  1. Implement IReplacingCallback interface to find the text “Total Cost”.
  2. In IReplacingCallback.Replacing method, get the Run nodes after this text.
  3. Replace the “$” and “,” in these Run nodes.

Hope this helps you.