Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files from any .NET platform (C#, VB.NET, ASP.NET, .NET Core) and Java applications (J2SE and J2EE) with fast and high quality performance.

Fri Sep 09, 2011 5:32 pm

I have used code from one of your examples on regex returning an array of textselections as follows...
System.Text.RegularExpressions.Regex regx = new System.Text.RegularExpressions.Regex(@"\{\{[^{}]+\}\}");
Spire.Doc.Documents.TextSelection[] textSelections = document.FindAllPattern(regx);

The problem I am having is that during the processing of each one of these, a few of these (half way through the collection)
are giving me errors "Index and length must refer to a location within the string"

here is the looping code
for (int x = (textSelections.Length - 1); x >= 0; x--)
{
Spire.Doc.Documents.TextSelection selection = textSelections[x];
try
{
string val = selection.GetAsOneRange().Text; <-- error thrown by this line...
val = "<SPAN class=ip5 >" + val + "</SPAN>";
selection.GetAsOneRange().Text = val;
}
catch (Exception ex)
{ return ex.Message }
}

When I try to figure out why it is doing this, I get nowhere as I have no way of accessing the data inside....could I get a hand please?

hesaigo999ca
 
Posts: 18
Joined: Thu Aug 18, 2011 6:07 pm

Mon Sep 12, 2011 1:17 pm

Also, I have used all the available functions to try and debug this, but seems inherent to the GetRanges() call and GetAsOneRange() call.
here is the full error

{System.ArgumentOutOfRangeException: Index and length must refer to a location within the string.
Parameter name: length
at System.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length, Boolean fAlwaysCopy)
at System.String.Substring(Int32 startIndex, Int32 length)
at Spire.Doc.Documents.TextSelection.f()
at Spire.Doc.Documents.TextSelection.GetAsOneRange()
at Intellera.WordMerge.API.ConvertWordToHtml(String pathOfDoc, String pathFileNameToBeSavedSave, String UserControlId)}

I am seeing that the field does have a value which is {{tagname}} which is funny, because all previous tags that had values such as this from within my loop
were ok, and on this particular TextSelection object, I can see the SelectedText value as being {{tagname}}, I get an error when I try to assign to SelectedText = "newtext" , and I also get an error when I try to use any of those 2 functions.....as well as the count is 1 for this object (same as all the others)...

awaiting your a response
ps- I also can provide you with my template if it will help solve this issue.

hesaigo999ca
 
Posts: 18
Joined: Thu Aug 18, 2011 6:07 pm

Tue Sep 13, 2011 10:08 am

Hi,
Sorry for any inconvenience caused by us.
Please try:
Code: Select all
Regex regex = new Regex(@"\{\{[^\{\}]+\}\}");
TextSelection[] selections = doc.FindAllPattern(regex);
IList<TextRange> ranges = new List<TextRange>();
foreach (TextSelection selection in selections)
{
    TextRange range = selection.GetAsOneRange();
    ranges.Add(range);
}

foreach (TextRange range in ranges)
{
    String text = range.Text;
    range.Text = String.Format("<SPAN class=ip5 >{0}</SPAN>", text);
}

It works fine in my computer.
Because TextSelection just handles the start and end index of the matched string, in your code when you change the former TextRange, the struct of the paragraph will be changed, and the index in the latter TextSelection will be wrong.
Harry
Technical Support / Developer,
e-iceblue Support Team
User avatar

harry.support
 
Posts: 180
Joined: Mon Nov 08, 2010 3:11 pm

Tue Sep 13, 2011 1:25 pm

Sorry Harry, this does not work ....the line here is the culprit as I posted above...

---> TextRange range = selection.GetAsOneRange();
as soon as I get upon 1 selection (i guess that is corrupted???) it throws an exception.

As my post above you will see the exception thrown, and this happens on the 17th selection object in my array of about 24
I am sure it is an internal thing, I could send you the word document that I am parsing,
but I was rather wanting to know if there was any other way of accessing the OM of your selection object.....
as I am new to spire objects, I went looking to the documentation, but it is missing info about this...

here is what I have on selection objects from the documentation you offer with your product.
>Namespace: Spire.Doc.Documents
>Assembly: Spire.Doc (in Spire.Doc.dll) Version: 4.0.5.2
>Syntax
>public TextRange[] GetRanges()

...there is no real description about the methods used getasonerange or getrange
nor is there any help about the actual class, all Isee is that it is implementing an ienumerable interface....aside this, that's it.

I do not want to complain, but I want to disturb you guys even less if I can help it, and without proper documentation,
there is not much I can do to debug what is going on.

Please let me know if you need me to send you the word template for your to run
through your code with breakpoints and see where the error is being thrown.

thanks in advance...

hesaigo999ca
 
Posts: 18
Joined: Thu Aug 18, 2011 6:07 pm

Wed Sep 14, 2011 2:17 am

Hi,

Sorry for any inconvenience caused by us.
I ran my code with Spire.Doc 4.0.10.2, please download it from http://www.e-iceblue.com/Download/downl ... t-now.html.
If it does not work with your doc file yet, please send your doc file to support@e-iceblue.com to help us reproduce your problem.

Thanks for your suggestion. You are right, api document is very important, we have started to complete them.

Thanks and Regards.
Harry
Technical Support / Developer,
e-iceblue Support Team
User avatar

harry.support
 
Posts: 180
Joined: Mon Nov 08, 2010 3:11 pm

Wed Sep 14, 2011 1:27 pm

Thanks Harry for the quick reply and help, I was uncertain where to copy the dlls so I copied them into the folder for spire products inside the program files.
I removed the old references from my project and added the new ones to the new dlls.
Now I loop through, and still get the same error....I have sent you (support) my copy of the template for
you to review and test on your own as the latest build does nothing for my problem.

thanks!

hesaigo999ca
 
Posts: 18
Joined: Thu Aug 18, 2011 6:07 pm

Thu Sep 15, 2011 8:44 am

Hi,

I have run the code with your file, it works file. I send you my project via email, please check.
To use the new dll, you can:
1. copy them into the installation folder as what you did.
2. remove the old references and bin folder of your project.
3. add new references to new dll files.
4. rebuild and run.
Harry
Technical Support / Developer,
e-iceblue Support Team
User avatar

harry.support
 
Posts: 180
Joined: Mon Nov 08, 2010 3:11 pm

Thu Sep 15, 2011 1:38 pm

Hi Harry...the new code you sent me does work fine, although it is very different then the first code I was sent to use...

For anyone interested in knowing what I had to do, here is the code that does work from the example sent to me by email...
I guess adding the TextRange object class to hold the info to then modify it is better then trying to do a modify directly on the
.Text property of the GetAsOneRange() function/class returned..... makes sense, although I was
unaware of the object class hierarchy as it is not really in the documentation....but all in all this works great now....thanks Harry for all your help....
Here is the code that works

Spire.Doc.Document doc = new Spire.Doc.Document(pathOfDoc);
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"\{\{[^\{\}]+\}\}");
Spire.Doc.Documents.TextSelection[] selections = doc.FindAllPattern(regex);
IList<Spire.Doc.Fields.TextRange> ranges = new List<Spire.Doc.Fields.TextRange>();
foreach (Spire.Doc.Documents.TextSelection selection in selections)
{
Spire.Doc.Fields.TextRange range = selection.GetAsOneRange();
ranges.Add(range);
}
foreach (Spire.Doc.Fields.TextRange range in ranges)
{
String text = range.Text;
range.Text = String.Format("<SPAN class=ip5 >{0}</SPAN>", text);
}
doc.SaveToFile("Test.doc");

hesaigo999ca
 
Posts: 18
Joined: Thu Aug 18, 2011 6:07 pm

Thu Sep 15, 2011 2:05 pm

Question for you Harry, I am seeing that the code that is generated, when I convert the word document now into html, all the html encapsulated tags, do not get parsed as html, instead it almost looks like they are inside a <pre> tag or something, is there a way for the conversion process that spire uses allows any html code inside the word document to be left as real html, so that the html document generated has the new html text parsed properly....maybe a property during the conversion process needs to be set, or something similar....?

I have a previous example you sent me to AppendHtml(texthere) so that it will be read as word formatted text....
however my need for this new functionality is actually the exact opposite....I need to make sure I can encapsulate
my text with html that will be considered real html in the end.....

Thanks in advance for all your help guys.

hesaigo999ca
 
Posts: 18
Joined: Thu Aug 18, 2011 6:07 pm

Fri Sep 16, 2011 9:50 am

Hi,

AppendHtml does support convert HTML to word document elements, but it does not support the style class.
We suggest you to directly apply the style list of class ip5 to TextRange object.
For example:
Code: Select all
Regex regex = new Regex(@"\{\{[^\{\}]+\}\}");
TextSelection[] selections = doc.FindAllPattern(regex);
IList<TextRange> ranges = new List<TextRange>();
foreach (TextSelection selection in selections)
{
    TextRange range = selection.GetAsOneRange();

    //apply style list
    range.CharacterFormat.FontSize = 30;
    range.CharacterFormat.TextColor = Color.Red;
}
Harry
Technical Support / Developer,
e-iceblue Support Team
User avatar

harry.support
 
Posts: 180
Joined: Mon Nov 08, 2010 3:11 pm

Sat Sep 17, 2011 5:16 pm

Instead I have doen what I need to do, and replaced the text within the actual output html file using a filestreamobject to readeachline...and on eachline replace the spire generated html (&lt;span class=ip5 &gt;) with the actual code (<span class=ip5 >)...then I close the file....so now I actually have a properly formatted file.

Thanks Harry for all your help, just too bad I need to do all these steps to get my tags working correctly....
maybe the next version of spire.doc could have a new property (say .EnableHtmlTextExpressions) that will treat any html text within a range.text field to be
seen as real html...and not just text to be inserted as text.

Thanks Spire!

hesaigo999ca
 
Posts: 18
Joined: Thu Aug 18, 2011 6:07 pm

Mon Sep 19, 2011 8:01 am

Hi,

Thanks for your suggestion. I have forward it to my colleagues. We will seriously consider and try to implement it.
Harry
Technical Support / Developer,
e-iceblue Support Team
User avatar

harry.support
 
Posts: 180
Joined: Mon Nov 08, 2010 3:11 pm

Return to Spire.Doc