Following [C#: Copy Content from a Word Document to Another | Clone Word DOCX] we want to copy contents from text file and append it to a final word document. The data is copied but the font is not using it’s default (Times New Roman) and is just keeping the source (text files) one, even though we added the param: ImportFormatMode.UseDestinationStyles. Code:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Writeln("This is a test paragraph before");
Font font = builder.Font;
font.Name = "Arial";
font.Bold = true;
builder.Writeln("This is a test paragraph after");
Document docFirstPage = new Document(Path.Combine(pathToTemp, "Townlist.txt"));
//docFirstPage.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;
//doc.AppendDocument(docFirstPage, ImportFormatMode.UseDestinationStyles);
builder.InsertDocument(docFirstPage, ImportFormatMode.UseDestinationStyles);
doc.Save(pathToTemp + "TestFont.docx");
Tried to at least change the font (as per lines 5-8) and is not working as well. Tried adding a custom style (instead of font instance) and still didn’t changed.
Just need advise on how to keep the default Aspose.Words font (Times New Roman) and not to change to the source ones.
@Remus87 In case of TXT document you do not need to load it into a Document
object before insertion. You can simply use DocumentBulder.Write
method. For example see the following code:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Writeln("This is a test paragraph before");
builder.Font.Name = "Arial";
builder.Font.Bold = true;
builder.Writeln("This is a test paragraph after");
builder.Write(File.ReadAllText(@"C:\Temp\in.txt"));
doc.Save(@"C:\Temp\out.docx");
Hi , this works ok, but makes it harder for me to get the paragraph that i need so i can further style it.
As a use case i have many text files that want to append as below: (feedfiles is a string array that holds the name of the files)
for (int i = 0; i < feedFiles.Length; i++)
{
// Adding town details
string townFileFormatted = FormatTownFiles(feedtown);
builder.Write(townFileFormatted);
builder.MoveToDocumentEnd();
builder.InsertBreak(BreakType.PageBreak);
}
A text file sample will be:
Town1
LIST OF SCHEDULES
1.001 No Waiting At Any Time
3.001 No Loading At Any Time
4.066 Limited Waiting 9am-6pm
Now i want to fetch the first 2 rows (Town1 and LIST OF SCHEDULES) to make them bold and add more space between paragraphs on the rest of the records (last 3 ones)
With the previous method tried (appending as separate documents), i know that i could get first paragraph (Town1) by assigning to a Paragraph instance -> doc.FirstSection.Body.FirstParagraph;
Don’t know how to get them now and apply the style
@Remus87 You can use DocumentBuilder.CurrentParagraph
as the first paragraph. For example see the following code:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
for (int i = 0; i < 10; i++)
{
// Adding town details
string townFileFormatted = File.ReadAllText(@"C:\Temp\in.txt");
Paragraph first = builder.CurrentParagraph;
builder.Writeln(townFileFormatted);
Paragraph second = (Paragraph)first.NextSibling;
// Format paragraphs.
first.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;
second.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading3;
builder.MoveToDocumentEnd();
builder.InsertBreak(BreakType.PageBreak);
}
doc.Save(@"C:\Temp\out.docx");
in.txt
file contains:
Town1
LIST OF SCHEDULES
1.001 No Waiting At Any Time
3.001 No Loading At Any Time
4.066 Limited Waiting 9am-6pm
Here is the produced output: out.docx (7.3 KB)
Thanks for the prompt reply Alexey!
Will update shortly after I’ll try.
1 Like
It works fine. Thanks for the solution.
1 Like