I intent to determine indent size of each line and replace it with white-spaces (for example 5 white-spaces for each 5 px indent).
Thank you
Hi Amir,
Thanks for your inquiry.
Sure, this is possible, please see the code below.
string spaces = new String(' ', (int)para.ParagraphFormat.LeftIndent);
string rangeText = para.Range.Text;
para.Range.Replace(rangeText, spaces + rangeText, false, false);
para.ParagraphFormat.LeftIndent = 0;
However note, that the total size of the spaces will depend upon the font, the font size and the character spacing used so the space generated may not be that accurate.
Thanks,
Unfortunately it doesn’t work. it can’t determine amount of indents accurately in many of lines.
If you can please put a more complete source here
Thank you
------------
edited:
I’ve found that this code effect only on first paragraph line. how can i solve this problem
Hi Amir,
Thanks for this additional information.
You can apply this code to every pargraph in your document by using the code below.
foreach (Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true))
{
string spaces = new String(' ', (int)para.ParagraphFormat.LeftIndent);
string rangeText = para.Range.Text;
para.Range.Replace(rangeText, spaces + rangeText, false, false);
para.ParagraphFormat.LeftIndent = 0;
}
I’m afraid as described in my previous post, it will be very difficult to try calculate the exact spacing due to the factors such as font size, character spacing etc. Instead could you please describe what you are trying to achieve? Perhaps there is an easier way to achieve it.
Thanks,
Really my main problem has been solved
I have checked para.Range.Text string and found that it separate each line with “/v” and each paragraph with “/r”
I used Regx and splite string then use DocumentBuilder.Writeln and the result was the exactly that i want.
But i have another problem when i use Range.Replace and string has some character such as “/v” or “/r” a below exception been throw:
“The match includes one or more special or break characters and cannot be replaced.”
I’ve read some topic related to this problem and have used solutions but my problem still remains
Please direct me
Thank you
Hi
Thanks for your inquiry. It is perfect, that you already found the way to resolve the problem.
In additional, you can’t replace special character using Aspose.Words. This is by design. An exception is thrown if captured or replacement strings contain one or more special characters: paragraph break, cell break, section break, field start, field separator, field end, inline picture, drawing object, and footnote.
Best regards,
Hi,
Thank you for your reply
So it seems that i have to use DocumentBuilder and rewrite all document to a new file to replace some texts that have special characters. But my documents have complicate structures (such as tables,lists,header,footer,images and etc).
Is it possible to create a new document with a straightforward way without using all DocumentBuilder methods to build nodes such as instead of create a table just copy a table with its format from source document to destination
Thank you
Hi Amir,
Thanks for your inquiry.
Sure, you can import a table from another document by using the code below. Note that in the code snippet dstDoc in your destination document, srcDoc is the source document where the table comes from and the srcTable is the table to copy.
Table srcTable = (Table)srcDoc.GetChild(NodeType.Table, 0, true); // The table to import from your source document
Table importedTable = (Table)dstDoc.ImportNode(srcTable, true); // The table imported into the destination node
dstDoc.FirstSection.Body.AppendChild(importedTable); // Add the imported table into the document.
If you have any further queries, please feel free to ask.
Thanks,
Thank you for your reply
For last question i want to know how can i found the exact position of a paragraph and move to that position to write new text with DocumentBuilder insted of that paragraph
Thank you
Hi
Thanks for your request. I think in your case you can try using IReplacingCallback. Please see the following link for more information:
https://reference.aspose.com/words/net/aspose.words.replacing/ireplacingcallback/
Also please see the following simple code:
// Open document.
Document doc = new Document(@"C:\\Temp\in.doc");
Regex regex = new Regex("MyParagraph", RegexOptions.IgnoreCase);
// Find and replace paragrap
doc.Range.Replace(regex, new ReplaceHandler(), false);
// Save output document
doc.Save("C:\\Temp\\out.doc");
====================================================================
private class ReplaceHandler : IReplacingCallback
{
public ReplaceAction Replacing(ReplacingArgs e)
{
// This is a Run node that contains either the beginning or the complete match.
Node currentNode = e.MatchNode;
// The first (and may be the only) run can contain text before the match,
// in this case it is necessary to split the run.
if (e.MatchOffset > 0)
currentNode = SplitRun((Run)currentNode, e.MatchOffset);
// Create DocumentBuilder object, which will help us to insert filds.
DocumentBuilder builder = new DocumentBuilder((Document)e.MatchNode.Document);
// Move builder cursor to the current node.
builder.MoveTo(currentNode);
// Insert new text.
builder.Write("New Text");
// This array is used to store all nodes of the match for further removing.
ArrayList runs = new ArrayList();
// Find all runs that contain parts of the match string.
int remainingLength = e.Match.Value.Length;
while (
(remainingLength > 0) &&
(currentNode != null) &&
(currentNode.GetText().Length <= remainingLength))
{
runs.Add(currentNode);
remainingLength = remainingLength - currentNode.GetText().Length;
// Select the next Run node.
// Have to loop because there could be other nodes such as BookmarkStart etc.
do
{
currentNode = currentNode.NextSibling;
}
while ((currentNode != null) && (currentNode.NodeType != NodeType.Run));
}
// Split the last run that contains the match if there is any text left.
if ((currentNode != null) && (remainingLength > 0))
{
SplitRun((Run)currentNode, remainingLength);
runs.Add(currentNode);
}
// Now highlight all runs in the sequence.
foreach (Run run in runs)
run.Remove();
// Signal to the replace engine to do nothing because we have already done all what we wanted.
return ReplaceAction.Skip;
}
///
/// Splits text of the specified run into two runs.
/// Inserts the new run just after the specified run.
///
private static Run SplitRun(Run run, int position)
{
Run afterRun = (Run)run.Clone(true);
afterRun.Text = run.Text.Substring(position);
run.Text = run.Text.Substring(0, position);
run.ParentNode.InsertAfter(afterRun, run);
return afterRun;
}
}
Hope this helps.
Best regards,