We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Header - Title Not Populated when Page Landscape

I have an interesting issue regarding Headers. I have a string that I embed in my headers to populate information about the page ( i.e. Secition 1 etc. )

I have about 15 sections to a document that I process through, everything works fine with this field except for one section. That section’s body is landscaped. So what happens with that string is it is not overwritten by the code on runtime and every page in that section document prints out or retains the string after the landscape page is processed.

I have a function called replaceString(Run run, string strCompare, DocumentBuilder db, string strName, string strReplace)
{
strReplace = run.ParentParagraph.Range.Text.Replace(strName, strCompare);
db.MoveTo(run.ParentParagraph);
run.ParentParagraph.Runs.Clear();
db.Write(strReplace);
}


The string I am passing in looks like this:

"mystring\r"

I thought perhaps the \r was causing a problem, but I’m perplexed the String being replaced in the document is retained. I was thinking that if this was an issue where the string replaced that string but the \r pushed the new replacement string past a margin, I would not see anything or a portion of the string being passed in.

Due to the nature of my work, I cannot post files for my clients.

Hi

Thanks for your request. ‘\r’ is a special character and Run’s text usually does not contain such characters. I suppose you should simply remove ‘\r’ from the captured string.

Also, I would suggest you to use built-in Replace method:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/find-and-replace-overview.html

Hope this helps.

Best regards,

Here is an update and what I can tell. I have a document with about 10 pages. In that document is a header…ok. That header has what we call a ‘tag’ which is just a string we’re looking for to replace the value.

Here is a fragment of code:

NodeCollection nc = db.Document.GetChildNodes(NodeType.Run, true, false)
Run run = null;
There is a loop that goes through the Child node count.
for(int i=0; i< nc.Count; i++) <-- this count is 1711 nodes in this document.
{
run = (Run)nc[i];
if (run != null)
{
if(run.Text == myString)
{
replaceRun(run, cmpStr, db, myString, pText);
}
}

What I have found out is after n number of loops, nothing is found out after the 5 loop. The coincides with the 6th page have non-header content set in landscape mode.

private static void replaceRun(Run run, string thecmpStr, DocumentBuilder db, string theMyString, string thepText)
{

// set the paragraph = paragraph text with the parameter replaced.
//replacementText = run.ParentParagraph.Range.Text.Replace(BookMarkName,Compare);
//// move to the paragraph
//db.MoveTo(run.ParentParagraph);
//// clear existing content
//run.ParentParagraph.Runs.Clear();
//// replace existing content with the modified text.
//db.Write(replacementText);

Range rg = run.ParentParagraph.Range;
rg.Replace(BookMarkName, Compare, false, true);

}


Hi

Thank you for additional information. I think the problem is sitting in the highlighted line of code:

if (run != null)

{

if (run.Text == myString)

{

replaceRun(run, cmpStr, db, myString, pText);

}

}

The reason I think so is because text can consist of several runs, i.e. one run contain the first letter of the word and the other run contains other letters. That is why this condition will not pass when you loop through all runs.

As a workaround, you can call JuinRunsWithSameFormatign before performing replace:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/aspose.words.document.joinrunswithsameformatting.html

This might help, but there is no 100% guaranty.

I would suggest you simply use Range.Replace method without looping through runs.

Best regards,

See attached. Debugged through the code this specific code. The replaceRun module is handed a specific Run which is not a composite node, a simple Run in which the content is only the text I’m looking for and looking to replace.

I think if you look at the attached .jpg file of my debugging session in VS2008 shows this information succinctly.

I also removed the information on that page in the document I was processing in the event non-header item’s margins/borders were overshooting the header area. You will also see commented out is the earlier suggestion you provided with the Range code. When I replaced the code in the replaceRun. You’ll note I’ve changed the parameters to match the parameter call from the calling program.

I don’t know if this helps or not, but I’m perplexed because the module outline in the attached picture is working inside this document until a certain page is process in the NodeCollection run in which the text is not replaced. And it looks like the correct run is being handled out of the node collection.


Hi Eric,

Thank you for additional information. But it would be great if you attach a sample document or create a simple application that will allow us to reproduce the problem. We will check the issue and provide you more information.

Best regards,

Alexey, I can’t provide you the code. I feel I have given you information to work with as well. I’m working on sensitive documents that I would have to remove important information that is sensitive in nature.

Also, the ‘\r’ is being added by your code or the Visual Studio 2008 debugger. Specifically when I debug through your ASPOSE code.

if I do this:
string myNewStr =
run.ParentParagraph.Range.Text.Replace(strToRemove,ReplacingString)

in this code, the string in the document ( strToRemove ) will be replaced by
ReplacingStr using the following strings.

So if my strToRemove string is “||SECTION_TITLE||” and my ReplacingString is "Appendix C:"

myNewStr looks like this in debugger: "Appendix C:\r"

I don’t understand why there is a carriage return unless there is some intrinsic underlying process in the run.ParentParagraph.Range.Text.Replace method is picking up something from the document.

Hi

Thank you for additional information. But I cannot replicate the issue on my side. I do not really need your actual document, I need a simple example that will allow me to reproduce this strange behavior. You can simply remove all sensitive information from your document or replace it with dummy data. This will really help me to understand where the problem is sitting.

Best regards,