Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Tue Sep 19, 2023 2:03 pm

I use this code to replace text with HTML text.

// Create Temp. Document
Spire.Doc.Document docHtml = new Spire.Doc.Document();
// Copy Styles and Themes from main document to temp document
doc.CloneDefaultStyleTo(docHtml);
doc.CloneThemesTo(docHtml);
doc.CloneCompatibilityTo(docHtml);

// Get the first section of the main document
Section docSection = (Section)doc.Sections[0];
// Add section to temp document
docHtml.AddSection();

var count = docSection.Paragraphs.Count;
// Get the first paragraph of the main document
Spire.Doc.Documents.Paragraph docParagraph= docSection.Paragraphs[docSection.Paragraphs.Count - 1];
// Make a clone of the main document's paragraph so the styling is copied too
Spire.Doc.Documents.Paragraph newParagraph = (Spire.Doc.Documents.Paragraph)docParagraph.Clone();

// Add the copied paragraph to the section of the temp. document
docHtml.Sections[0].Paragraphs.Add(newParagraph);
// Add the HTML string of the description tot the paragraph
docHtml.Sections[0].Paragraphs[0].AppendHTML(quoteDescription);

// Loop through the appended paragraphs to remove the liune spacing.
foreach (Section htmlSection in docHtml.Sections)
{
for (int i = 0; i < htmlSection.Body.ChildObjects.Count; i++)
{
if (htmlSection.Body.ChildObjects[i].DocumentObjectType == DocumentObjectType.Paragraph)
{
// Disabled this for demonstration
//(htmlSection.Body.ChildObjects[i] as Spire.Doc.Documents.Paragraph).Format.LineSpacing = 8;

}

}
}
// Make the document an Interface Document so we can insert it in the main document
IDocument replaceDoc = docHtml as IDocument;
// Replace the placeholder with the Interface Document
doc.Replace("{{Description}}", replaceDoc, false, true);


There are a few problems:
1. I need to override the line spacing for each paragraph, because otherwise the spacing is way too large. Even though it's correctly set in the Word document. See attachment 1. I don't want to set a static number as line spacing, since the line spacing may vary in the used Word document.
2. I have the replacement text after a label Omschrijving:. When the text is replaced with HTML, there a spacing on the left of the text? See attachment 1
3. The font of the numbered list is not the same as the font in the document. Even though the text after the number is correct.

Attachment 2 is how the replaced text is supposed to look like.

wesley_0172
 
Posts: 1
Joined: Tue Sep 19, 2023 1:49 pm

Wed Sep 20, 2023 2:27 am

Hi,

Thanks for your feedback.
Could you please share us with your document, html and a detailed description of your requirements? So that we can investigate further and work out a solution for you. You can send them to us via email ([email protected]) or attach them here. Thanks for your assistance.

Best regards,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Return to Spire.Doc