Handle Spaces Options

In this tutorial, we are going to explore the C# source code provided for the functionality of “Managing Spaces with TXT Loading Options” with Aspose.Words for .NET. This feature allows you to specify whitespace handling behavior when loading a TXT document.

Step 1: Setting up the environment

Before you begin, make sure you’ve set up your development environment with Aspose.Words for .NET. Make sure you’ve added the necessary references and imported the appropriate namespaces.

Step 2: Creating the text document

// Path to your documents directory
string dataDir = "YOUR DOCUMENTS DIRECTORY";

const string textDoc = "Line 1\n" +
                        "Line 2\n" +
                        "Line 3";

In this step, we create a text string that simulates a text document containing lines with leading and trailing spaces.

Step 3: Configuring upload options

TxtLoadOptions loadOptions = new TxtLoadOptions
{
     LeadingSpacesOptions = TxtLeadingSpacesOptions.Trim,
     TrailingSpacesOptions = TxtTrailingSpacesOptions.Trim
};

In this step, we configure the options for loading the TXT document. We create a new TxtLoadOptions object and set the LeadingSpacesOptions and TrailingSpacesOptions properties to TxtLeadingSpacesOptions.Trim and TxtTrailingSpacesOptions.Trim respectively. This tells Aspose.Words to remove leading and trailing spaces from lines when loading the document.

Step 4: Loading the document

Document doc = new Document(new MemoryStream(Encoding.UTF8.GetBytes(textDoc)), loadOptions);

In this step, we load the document using the Document method and passing the memory stream containing the specified text string and load options.

Step 5: Save the document

doc.Save(dataDir + "WorkingWithTxtLoadOptions.HandleSpacesOptions.docx");

In this last step, we save the resulting document in .docx format using the Save method and passing the path to the output file.

Now you can run the source code to load the text document by specifying whitespace handling options. The resulting document will be saved in the specified directory with the name “WorkingWithTxtLoadOptions.HandleSpacesOptions.docx”.

Sample Source Code for Space Management Feature with TXT Loading Options with Aspose.Words for .NET*


            
// Path to your document directory
string dataDir = "YOUR DOCUMENT DIRECTORY";

const string textDoc = "      Line 1 \n" +
					   "    Line 2   \n" +
					   " Line 3       ";

TxtLoadOptions loadOptions = new TxtLoadOptions
{
	LeadingSpacesOptions = TxtLeadingSpacesOptions.Trim,
	TrailingSpacesOptions = TxtTrailingSpacesOptions.Trim
};

Document doc = new Document(new MemoryStream(Encoding.UTF8.GetBytes(textDoc)), loadOptions);

doc.Save(dataDir + "WorkingWithTxtLoadOptions.HandleSpacesOptions.docx")
            
        

Conclusion

In this tutorial, we explored the functionality of managing spaces with TXT loading options in Aspose.Words for .NET. We learned how to specify whitespace handling behavior when loading a TXT document.

This feature is very useful for dealing with unnecessary spaces to the left and right of lines in a document. By configuring the appropriate loading options, you can easily remove these unwanted spaces, which helps to make the document content cleaner and more readable.

Aspose.Words for .NET offers many advanced features for document manipulation and generation. Managing spaces when loading a TXT document is one of the many powerful tools it puts at your disposal.

It’s important to choose the space management options that best suit your specific scenario. In this example, we used the Trim options to remove unnecessary spaces from the beginning and end of the line. However, Aspose.Words also has other options to keep spaces, remove them completely, or keep them as they are.

Don’t forget to adapt these options according to your specific needs and the structure of your TXT documents.

With Aspose.Words for .NET, you can easily manipulate whitespace in your documents, improving layout quality and content readability.

So, don’t hesitate to integrate whitespace management with TXT loading options in your Aspose.Words for .NET projects and take advantage of its advantages to create well-formatted and easy-to-read documents.