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

Free Support Forum - aspose.com

ControlChar.LineBreak in worddoc

Hi ,

Firstly,We use Aspose.Word 14.5.0 and
Aspose.PDF 9.3.0.0 in our project.

Our original.doc have <REFERANS_DOKUMANLAR> tag. While we replacing this tag with more than one ‘special name’ , we use aspose ‘ControlChar.LineBreak’ code for seperating.

But we use long ‘special name’ , our doc mixing. I attached original doc, has small special name doc and has long special name doc- mixing doc.

Thanks,

Hi Ahmet,

Thanks for your inquiry. I have tried to understand your query and as per my understanding you are replacing <REFERANS_DOKUMANLAR> tag with some text which contain ControlChar.LineBreak.

I can see the ControlChar.LineBreak in small.doc and long-mixing.doc. Could you please share which issue you are facing while using Aspose.Words? We will then provide you more information about your query.

Hi Tahir,

I attach two new doc and explain what is my problem.

Thanks.

Hi Ahmet,

Thanks for sharing the detail. I have tested the scenario using latest version of Aspose.Words for .NET 14.5.0 and have not found the shared issue. I suggest you please use the latest version of Aspose.Words for .NET 14.5.0.

I have used the following code example to test this issue. Please find the output document in attachment.

private class ReplaceEvaluatorTest01 : IReplacingCallback
{
    private string repalcetext;
    public ReplaceEvaluatorTest01(String str)
    {
        repalcetext = str;
    }

    /// 
    /// This method is called by the Aspose.Words find and replace engine for each match.
    /// 
    ReplaceAction IReplacingCallback.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);
        // 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);
        }
        // Create Document Buidler aond insert MergeField
        DocumentBuilder builder = new DocumentBuilder(e.MatchNode.Document as Document);
        builder.MoveTo((Run)runs[runs.Count - 1]);
        builder.Writeln(repalcetext);
        // Now remove 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;
    }

    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;
    }
}
Document doc = new Document(MyDir + "Original.doc");
//doc.Range.Replace(new Regex(@"\"), new ReplaceEvaluatorTest01(@"SOPMON046-SERİ KAYITLARININ DEĞERLENDİRİLMESİ VE SERBEST BIRAKMA PROSEDÜRÜ"), false);
doc.Range.Replace(new Regex(@"\"), new ReplaceEvaluatorTest01(@"00H-008_-GENEL MÜDÜRLÜK BİNASI SEÇ PROSEDÜRÜ2 00H-029_2-00H-029_Deneme3"), false);
doc.Save(MyDir + "Out.docx");

Hi Tahir,

Our code is:

strig deger = "";
string ayrac = ControlChar.LineBreak;
foreach (string item in refDocs)
{
    deger += item + ayrac;
}
dcKeyword.ColumnName = "REFERANS_DOKUMANLAR";
ReplaceWithEvaluator(wordDoc, dcKeyword.ColumnName, deger, withLang);
public static void ReplaceWithEvaluator(Aspose.Words.Document wordDoc, string OldValue, string NewValue, bool withLanguage)
{
    ReplaceEval.ReplaceText = NewValue.Replace("\n", "");
    ReplaceEval.WithLang = withLanguage;
    ReplaceEval.OldValue = OldValue;
    wordDoc.Range.Replace(new System.Text.RegularExpressions.Regex(@"<" + OldValue + @">"), new ReplaceEval(), false);
}

And refDocs :

SOPMON046-SERİ
KAYITLARININ DEĞERLENDİRİLMESİ VE SERBEST BIRAKMA PROSEDÜRÜ

SOPMON046-PRF7-Serbest
Bırakma formu

Hi Ahmet,

Thanks for sharing the detail. Unfortunately, I have not understood your query related to ControlChar.LineBreak. In your code, you are replacing <REFERANS_DOKUMANLAR> tag with deger (which contains the line break). Could you please explain what exact issue you are facing?

It would be great if you please share following detail for investigation purposes.

  • Please attach your input Word document.
  • Please

create a standalone/runnable simple application (for example a Console
Application Project
) that demonstrates the code (Aspose.Words code) you used to generate
your output document

  • Please attach the output Word file that shows the undesired behavior.
  • Please
    attach your target Word document showing the desired behavior. You can
    use Microsoft Word to create your target Word document. I will
    investigate as to how you are expecting your final document be generated
    like.

As soon as you get these pieces of information to
us we’ll start our investigation into your issue.

Hi Tahir,

I attached our test project and test document. While you are testing , you must test like this.

Replacing Tag:REFERANS_DOKUMANLAR

Replacing Value:SOPMON046-SERİ KAYITLARININ DEĞERLENDİRİLMESİ VE SERBEST BIRAKMA PROSEDÜRÜ$SOPMON046-PRF7-Serbest Bırakma formu

Thanks,

Hi Ahmet,

Thanks for your inquiry.

I have tested the scenario and have not found any issue with output document. Aspose.Words generates the expected output.

Aspose.Words mimics the same behavior as MS Word does. Please insert the ‘Replacing Value’ at the place of ‘Replacing Tag’ using MS Word, you will get the same output.

I have attached the output document with this post for your kind reference. See the attached image for detail. Please let us know if you have any more queries.

Thanks for your attention.

Our problem is that after replacement some part of text stands above table and some parts of text goes under the table. Is that behaviour usual for Aspose?

Hi Ahmet,

Thanks for your inquiry. The table after <REFERANS_DOKUMANLAR> tag have text wrapping as around. See the attached image for detail. Please change the table’s text wrapping to 'None ’ by using Table.TextWrapping property.

Please check the following code example for your kind reference.

Document doc = new Document(MyDir + "Original.doc");
foreach (Table table in doc.GetChildNodes(NodeType.Table, true))
{
    table.TextWrapping = TextWrapping.None;
}
ReplaceWithEvaluator(doc, "REFERANS_DOKUMANLAR", @"SOPMON046-SERİ KAYITLARININ DEĞERLENDİRİLMESİ VE SERBEST BIRAKMA PROSEDÜRÜ$SOPMON046-PRF7-Serbest Bırakma formu", true);
doc.Save(MyDir + "Out.docx");