How to remove the *"$" *sign and *","* Comma in footer section and again insert the same text in footer section?

Hi,

we are using aspose-word version 14.12.0.0. Our requirement is existing word document in footer section, we want to remove Order Cost and Total Cost “$”, “,” and again we insert the same text(without “$”, “,”) in same footer section. How can i achieve this and also we attached the sample project and screen shots for your review.

We are using following code for this requirements.

string File = "C:\\FooterIssue.Docx";
Aspose.Words.Document doc = new Aspose.Words.Document(File);

System.IO.MemoryStream outStream = new System.IO.MemoryStream();
doc.Save(outStream, Aspose.Words.SaveFormat.Docx);

foreach (Section section in doc)
{
    HeaderFooter footer;

    footer = section.HeadersFooters[HeaderFooterType.FooterFirst];
    if (footer != null)
    {
        string Text = footer.ToTxt();
    }

    footer = section.HeadersFooters[HeaderFooterType.FooterPrimary];
    if (footer != null)
    {
        string Text = footer.ToTxt();
        Text = Text.Replace("$", "");
        Text = Text.Replace(",", "");
    }


    footer = section.HeadersFooters[HeaderFooterType.FooterEven];
    if (footer != null)
    {

        string Text = footer.ToTxt();
    }

}

byte[] docBytes = null;

//pdfDocument.Save(pdfOoutStream);
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();

AsposeIssue.zip (526.8 KB)

Another Attachments,

footer2.gif (14.4 KB)
footer1.jpeg (41.9 KB)

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

@smani,

Please try using the following code:

Document doc = new Document("D:\\AsposeIssue\\FooterIssue.docx");

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, ",");
}

doc.Save("D:\\AsposeIssue\\18.8.docx");

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

    if (footer == null)
    {
        Console.WriteLine("There is no footer of the specified type in the current section, create it.");
    }

    // Replace
    footer.Range.Replace(pattern, "", new FindReplaceOptions());
}

Hi,
we are facing issues in FindReplaceOptions section. I think we are missed the namespace or reference. Please provide the namespace or reference in FindReplaceOptions. Or please share the same project for this one.

footer.Range.Replace(pattern, "", new FindReplaceOptions());

Note: Am using the dll aspose-word version 14.12.0.0.

Thanks.

@smani,

The ‘FindReplaceOptions Class’ is part of ‘Aspose.Words.Replacing Namespace’.

Please upgrade to the latest version of Aspose.Words for .NET i.e. 18.8. Hope, this helps.

Our largest customer produces tens of thousands of documents each day using the current software version. We are on a path to upgrading but need a workaround in the interim. Any suggestions you can offer would be helpful.

@jcvanbesien,

Please try the following overload of Range.Replace method:

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

    if (footer == null)
    {
        Console.WriteLine("There is no footer of the specified type in the current section, create it.");
    }

    // Replace
    //footer.Range.Replace(pattern, "", new FindReplaceOptions());
    footer.Range.Replace(pattern, "", false, false);
}

I need to remove the “$” and “,” only OrderCost and TotalCost in footer section. No need to remove the averageCost. How can i achieve this?

we are using following code for footer changes.

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);
    }
}

Footer.jpeg (17.5 KB)

@smani,

Thanks for your inquiry. Please ZIP and upload your sample input document here for testing. We will investigate the issue on our end and provide you more information.

@ [awais.hafeez],

As per your request we attached the sample project for the above issue, Please review and give me the solution asap. we are waiting your response.

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

We are using following code for this requirements.

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

            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;
            
            //pdfDocument.Save(pdfOoutStream);
            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);
            }
        }

AsposeIssue.zip (634.7 KB)

@smani,

But, the document does not contain ‘averageCost’ text. Please ZIP and upload a new input Word document containing ‘averageCost’ text that you want to preserve here for testing.

@awais.hafeez

Again we attached the new ZIP file with document contains ‘averageCost’ text. Please review the newly attached Zip file and give me the solution asap. This is highly important and needed quickly, Once of our customer is facing this issue.

Thanks in advance.

AsposeIssue.zip (633.9 KB)

@smani,

As ‘averageCost’ is the last occurrence, you can use the following code to get the desired output:

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

    if (footer == null)
    {
        Console.WriteLine("There is no footer of the specified type in the current section");
    }
    else
    {
        // create a dummy footer to determine the total number of replacements
        HeaderFooter hf = (HeaderFooter)footer.Clone(true);
        int count = hf.Range.Replace(pattern, "", false, false);
        Console.WriteLine(count);

        // Do not Replace the last occurance
        Replacer handler = new Replacer(count);
        FindReplaceOptions options = new FindReplaceOptions();
        options.ReplacingCallback = handler;
        options.Direction = FindReplaceDirection.Backward;

        footer.Range.Replace(pattern, "", options);                
    }
}

private class Replacer : IReplacingCallback
{
    public int count = 0;
    public int i = 0;

    public Replacer(int totalCount)
    {
        count = totalCount;
        i = totalCount;
    }

    /// <summary>
    /// This method is called by the Aspose.Words find and replace engine for each match.
    /// This method highlights the match string, even if it spans multiple runs.
    /// </summary>
    ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
    {
        i--;
        if ((i + 1) == count)
        {
            // Signal to the replace engine to do nothing
            return ReplaceAction.Skip;
        }
        else
        {
            return ReplaceAction.Replace;
        }
    }
}
//////////////////////////////////////
Document doc = new Document("D:\\AsposeIssue\\FooterIssue.docx");
            
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, ",");
}

doc.Save("D:\\AsposeIssue\\18.8.docx");