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