1
Here you'll find answers to common questions about E-iceblue's Spire product series, covering both purchase and technical topics, so you can get solutions more efficiently.
- For purchase inquiries, please contact sales@e-iceblue.com.
- For technical support, please contact support@e-iceblue.com.
Q1: How to get text from word document?
A : You can call the method method document.GetText() to do so. Full code:
Document document = new Document();
document.LoadFromFile(@"..\..\test.docx");
using (StreamWriter sw = File.CreateText("output.txt"))
{
sw.Write(document.GetText());
}
Q2: How to insert an image with specified height and width?
A : You can set the properties height and width of DocPicture to resize the image. Full code:
Document document = new Document();
document.LoadFromFile("sample.docx", FileFormat.Docx);
Image image = Image.FromFile("image.jpg");
//specify the paragraph
Paragraph paragraph = document.Sections[0].Paragraphs[2];
DocPicture picture = paragraph.AppendPicture(image);
//resize the image
picture.Height = picture.Height * 0.8f;
picture.Width = picture.Width * 0.8f;
document.SaveToFile("result.docx", FileFormat.Docx);
Q3: How to align text in word document?
A : Please set the property HorizontalAlignment of the paragraph to align text. Full code:
Document document = new Document();
document.LoadFromFile("sample.docx");
//set paragraph1 to align left
Paragraph paragraph1 = document.Sections[0].Paragraphs[0];
paragraph1.Format.HorizontalAlignment = HorizontalAlignment.Left;
//set paragraph2 to align center
Paragraph paragraph2 = document.Sections[0].Paragraphs[1];
paragraph2.Format.HorizontalAlignment = HorizontalAlignment.Center;
//set paragraph3 to align right
Paragraph paragraph3 = document.Sections[0].Paragraphs[2];
paragraph3.Format.HorizontalAlignment = HorizontalAlignment.Right;
document.SaveToFile("result.docx");
Q4: How to change text on existing bookmarks?
A : You can use BookmarksNavigator to locate the specified bookmark. Then please call the method ReplaceBookmarkContent to replace text on bookmarks. Full code:
Document document = new Document();
document.LoadFromFile("sample.doc");
BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);
bookmarkNavigator.MoveToBookmark("mybookmark");
//replace text on bookmarks
bookmarkNavigator.ReplaceBookmarkContent("new context", false);
document.SaveToFile("result.doc", FileFormat.Doc);
Q5: How to convert word to html?
A : You can just call the method SaveToFile with specified file format HTML to convert word document to html. Full code:
Document document = new Document();
document.LoadFromFile("sample.doc");
//save word document as html file
document.SaveToFile("result.html", FileFormat.Html);
document.Close();
Q6: How to convert html to word document?
A : Please call the method LoadFromFile to load html file. Then call the method SaveToFile to convert html to word document. Full code:
Document document = new Document();
document.LoadFromFile("sample.html", FileFormat.Html, XHTMLValidationType.None);
//save html as word document
document.SaveToFile("result.doc");
document.Close();
Q7: How to convert word2007 to word2003?
A : Just call the method SaveToFile with specified file format doc to convert word2007 to word2003. Full code:
Document document = new Document("word2007.docx");
//convert word2007 to word2003
document.SaveToFile("word2003.doc", FileFormat.Doc);
document.Close();
Q8: How to replace and delete header or footer in word document?
A : Please use Section to get header or footer. And you can call the method Replace to replace header and call the method Clear to remove the headers or footers of the word document.
Document document = new Document();
Section section = document.AddSection();
//add a header
HeaderFooter header = section.HeadersFooters.Header;
Paragraph headerParagraph = header.AddParagraph();
TextRange text = headerParagraph.AppendText("Demo of Spire.Doc");
text.CharacterFormat.TextColor = Color.Blue;
document.SaveToFile("DocWithHeader.doc");
//replace the header
headerParagraph.Replace("Demo", "replaceText", true, true);
document.SaveToFile("DocHeaderReplace.doc");
document.LoadFromFile("DocWithHeader.doc");
//delete the heater
document.Sections[0].HeadersFooters.Header.Paragraphs.Clear();
document.SaveToFile("DocHeaderDelete.doc");
Q9: How to merge word documents?
A : Please call the method Clone to copy a section. Then call the method Add to add the copy of the section to specified document. Full code:
Document document1 = new Document();
document1.LoadFromFile("merge1.docx");
Document document2 = new Document();
document2.LoadFromFile("merge2.docx");
//add sections from document1 to document2
foreach (Section sec in document2.Sections)
{
document1.Sections.Add(sec.Clone());
}
document1.SaveToFile("result.docx");
Q10: How to traverse the cells of a table in a word document?
A : Rows is the collection of rows in a table and Cells is the collection of cells in a row. So you can traverse cells of a table using two loops. Full code:
Document document = new Document();
document.LoadFromFile("sample.docx");
Spire.Doc.Interface.ITable table = document.Sections[0].Tables[0];
int i=0;
//traverse the cells
foreach (TableRow row in table.Rows)
{
foreach (TableCell cell in row.Cells)
{
i++;
}
}
Q11: How to set text with shadow?
A : You just need to set the property IsShadow of TextRange. Full code:
Document document = new Document();
Section section = document.AddSection();
Paragraph paragraph = section.AddParagraph();
TextRange HText = paragraph.AppendText("this is a test!");
//set the property IsShadow
HText.CharacterFormat.IsShadow = true;
HText.CharacterFormat.FontSize = 80;
document.SaveToFile("result.doc");
Q12: How to insert line numbers in the Word?
A : You need to set the properties LineNumberingRestartMode, LineNumberingStep, LineNumberingStartValue of the section to insert line numbers in word document. Full code:
Document document = new Document();
Section section = document.AddSection();
//insert line numbers
section.PageSetup.LineNumberingRestartMode = LineNumberingRestartMode.RestartPage;
section.PageSetup.LineNumberingStep = 1;
section.PageSetup.LineNumberingStartValue = 1;
Paragraph paragraph = section.AddParagraph();
paragraph.AppendText("As an independent Word .NET component, Spire.Doc for .NET doesn't need Microsoft Word to be installed on the machine. However, it can incorporate Microsoft Word document creation capabilities into any developers .NET applications.");
document.SaveToFile("result.doc");
Q13: How to Make Text around image?
A : You need to set the properties TextWrappingStyle and ShapeHorizontalAlignment of the picture. Full code:
Document document = new Document();
Section section = document.AddSection();
Paragraph paragraph = section.AddParagraph();
string str = "As an independent Word .NET component, Spire.Doc for .NET doesn't need Microsoft Word to be installed on the machine. However, it can incorporate Microsoft Word document creation capabilities into any developers.NET applications.As an independent Word .NET component, Spire.Doc for .NET doesn't need Microsoft Word to be installed on the machine. However, it can incorporate Microsoft Word document creation capabilities into any developers’.NET applications.";
paragraph.AppendText(str);
DocPicture picture = paragraph.AppendPicture(Image.FromFile("logo.png"));
picture.TextWrappingStyle = TextWrappingStyle.Tight;
picture.HorizontalAlignment = ShapeHorizontalAlignment.Center;
document.SaveToFile("result.doc");
Q14: How to edit existing table in word document?
A : Use Section to get the table and you can edit the text in a cell and you can insert new row into the table. Full code:
Document doc = new Document("sample.docx");
Section section = doc.Sections[0];
ITable table = section.Tables[0];
//edit text in a cell
TableCell cell1 = table.Rows[1].Cells[1];
Paragraph p1 = cell1.Paragraphs[0];
p1.Text = "abc";
TableCell cell2 = table.Rows[1].Cells[2];
Paragraph p2 = cell2.Paragraphs[0];
p2.Items.Clear();
p2.AppendText("def");
TableCell cell3 = table.Rows[1].Cells[3];
Paragraph p3 = cell3.Paragraphs[0];
(p3.Items[0] as TextRange).Text = "hij";
//insert new row
TableRow newRow = table.AddRow(true, true);
foreach (TableCell cell in newRow.Cells)
{
cell.AddParagraph().AppendText("new row");
}
doc.SaveToFile("result.doc");
Q15: How to set the format of hyperlink with no underline?
A : Please set the textRange node of Hyperlink field to format hyperlink. Full code:
Document document = new Document();
Section section = document.AddSection();
Paragraph paragraph = section.AddParagraph();
Field hyperlink = paragraph.AppendHyperlink("www.e-iceblue.com", "www.e-iceblue.com", HyperlinkType.WebLink);
TextRange text = hyperlink.NextSibling.NextSibling as TextRange;
text.CharacterFormat.Bold = true;
text.CharacterFormat.UnderlineStyle = UnderlineStyle.None;
document.SaveToFile("result.doc");
Q16: How to set word document read-only?
A : Please call the method Protect to set the ProtectionType. Full code:
Document document = new Document();
document.LoadFromFile("sample.docx");
document.Protect(ProtectionType.AllowOnlyReading);
document.SaveToFile("result.doc");