Replace Line Feeds with Carriage Returns

Hi

How can we replace all Line Feeds with Carriage Returns within a Word Document

Thanks

@david.hancock.imagef,

Thanks for your inquiry. Please use the Range.Replace method as shown below to get the desired output. Hope this helps you.

Document doc = new Document(MyDir + "input.docx");
doc.Range.Replace(ControlChar.LineBreak, ControlChar.ParagraphBreak, new FindReplaceOptions());

Hi

Is there any other way to do this?

As I have discovered a bug cause this when using
document.Range.Replace(ControlChar.LineBreak, ControlChar.ParagraphBreak, new FindReplaceOptions());

The bug is as follows:

In our code, we used the document builder object

  1. Open the document
  2. Iterate though all sections in the document
  3. When in a section we iterate though all paragraphs

A paragraph will contain any lines which where replace from LB to PB

Thanks
Dave

@david.hancock.imagef,

Thanks for your inquiry. To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document.
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.

As soon as you get these pieces of information ready, we’ll start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

Hi Tahir,

I’m less concern with the bug.

Would to be possible to use document builder to insert a new paragraph, where we find a line break?

@david.hancock.imagef,

Thanks for your inquiry. Yes, you can insert new paragraph at the place of line break. You need to iterate through Run nodes of document, check if Run node contains the line break, move the cursor to the Run node and insert the paragraph break. Please check the following code snippet.

if (run.GetText().Equals(ControlChar.LineBreak))
{
    builder.MoveTo(run);
    run.Text = "";
    builder.InsertParagraph();
}

thanks

How do i iterate thought the Run nodes of a document

@david.hancock.imagef,

Thanks for your inquiry. Please use CompositeNode.GetChildNodes method to get a live collection of child nodes that match the specified type. Please check the following code example. Hope this helps you.

Document doc = new Document(MyDir + "in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
foreach (Run run in doc.GetChildNodes(NodeType.Run, true))
{
    if (run.GetText().Equals(ControlChar.LineBreak))
    {
        builder.MoveTo(run);
        run.Text = "";
        builder.InsertParagraph();
    }
}
                
doc.Save(MyDir + "18.4.docx");

Hi

I’m afraid that this code does not work

How else can I replace Line Breaks with Carriage Returns

Thanks

Additionally, I think its not working when then Line Break as it on the end of a Paragraph

@david.hancock.imagef,

Thanks for your inquiry. Please ZIP and attach your input and expected output documents here for our reference. We will then provide you more information about your query along with code.

Hi Tahir,

Please see the attached zip

Documents.zip (30.7 KB)

The zip contains 3 files

01-Before Running Code.docx - the word document prior to running the provided sample code posted in the thread earlier
02-After Running Code.docx - the word document after the code has been run, you will see no difference and LineBreaks will still be present
03-Desired Result.docx - is what we would like to achieve

Thanks
Dave

Can I have an update please

@david.hancock.imagef,

Thanks for sharing the detail. Please use latest version of Aspose.Words for .NET 18.7 and one of the following code example to replace line break with paragraph break. We have attached the output document with this post for your kind reference. 18.7.zip (9.9 KB)

Document doc = new Document(MyDir + "01-Before Running Code.docx");
doc.Range.Replace(ControlChar.LineBreak, ControlChar.ParagraphBreak, new FindReplaceOptions());
doc.Save(MyDir + "18.7.docx");

Document doc = new Document(MyDir + "01-Before Running Code.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
foreach (Run run in doc.GetChildNodes(NodeType.Run, true))
{
    if (run.GetText().Equals(ControlChar.LineBreak))
    {
        builder.MoveTo(run);
        run.Text = run.Text.Replace(ControlChar.LineBreak, ControlChar.ParagraphBreak);
    }
}

doc.Save(MyDir + "18.7.docx");

Thats worked thank you

@david.hancock.imagef,

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.