Knowledgebase (2311)
Children categories
In some case, we need make some modifications in an existing table but don't want destroy the original data, so we would like to copy the existing table then make some changes in the new table. How could we get the copied table? The easiest method is clone. There would introduce a solution to copy table and modify some data then insert the new table after original table via Spire.Doc.
Spire.Doc for .NET, a stand-alone .NET Word component, provides a method, Table.clone() to allow users to copy an existing table.
The main steps of the solution:
Firstly: load the word document with a table.
Document doc = new Document(); doc.LoadFromFile(@"CopyTable.doc");
The original document effect screenshot:

Secondly: extract the existing table and call the table.clone () method to copy it.
Section se = doc.Sections[0]; Table original_Table =(Table) se.Tables[0]; Table copied_Table = original_Table.Clone();
Thirdly: extract the last row then traversal its cells to modify data.
string[] st = new string[] { "Guyana", "Georgetown", "South America", "214969", "800000" };
//get the last row of copied table
TableRow lastRow = copied_Table.Rows[copied_Table.Rows.Count - 1];
//change lastRow data.
lastRow.Cells[i].CellFormat.Shading.BackgroundPatternColor = Color.Gray;
for (int i = 0; i < lastRow.Cells.Count; i++)
{
lastRow.Cells[i].Paragraphs[0].Text = st[i];
}
Finally: call Section. tables.add() method to add the copied table in section and save this document.
se.Tables.Add(copied_Table);
doc.SaveToFile("result.doc", FileFormat.Doc);
The result document effect screenshot:

Full code:
using Spire.Doc;
using System.Drawing;
namespace InsertingaAnExistingTable
{
class Program
{
static void Main(string[] args)
{
//load a word document
Document doc = new Document();
doc.LoadFromFile(@"CopyTable.doc");
// extract the existing table
Section se = doc.Sections[0];
Table original_Table =(Table) se.Tables[0];
// copy the existing table to copied_Table via Table.clone()
Table copied_Table = original_Table.Clone();
string[] st = new string[] { "Guyana", "Georgetown", "South America", "214969", "800000" };
//get the last row of table
TableRow lastRow = copied_Table.Rows[copied_Table.Rows.Count - 1];
//change last row data.
lastRow.Cells[i].CellFormat.Shading.BackgroundPatternColor = Color.Gray;
for (int i = 0; i < lastRow.Cells.Count; i++)
{
lastRow.Cells[i].Paragraphs[0].Text = st[i];
}
// add copied_Table in section
se.Tables.Add(copied_Table);
doc.SaveToFile("result.doc", FileFormat.Doc);
}
}
}
In some circumstances, we may need to convert or save Word documents as pictures. For one reason, a picture is difficult to edit; for another, compared with Word, pictures are much easier to be published for browsing. This article is aimed to explore how we can convert .doc/.docx to popular image formats such as Jpg, Png, Gif and Bmp in WPF using Spire.Doc.
Spire.Doc for WPF, as a professional Word component, provides a plenty of useful methods to manipulate Word documents in your WPF applications. Using Spire.Doc, developers are able to export Word documents as images with high quality. Here comes the method:
Step 1: Create a new project by choosing WPF Application in Visual Studio, add a button in MainWindow, double click the button to write code.
public partial class MainWindow : Window
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
}
Step 2: Initialize a new instance of Spire.Doc.Document class and load the sample Word file.
Document doc = new Document("sample.docx", FileFormat.Docx2010);
Step 3: To convert Word to image in WPF, we need firstly save Word as BitmapSource by calling the method Document.SaveAsImage(ImageType type), then convert BitmapSource to Bitmap, then save the Bitmap as image with a specified format using Image.Save(). Here, I saved Bitmap as .Png.
BitmapSource[] bss = doc.SaveToImages(ImageType.Bitmap);
for (int i = 0; i < bss.Length; i++)
{
SourceToBitmap(bss[i]).Save(string.Format("img-{0}.png", i));
}
}
private Bitmap SourceToBitmap(BitmapSource source)
{
Bitmap bmp;
using (MemoryStream ms = new MemoryStream())
{
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(source));
encoder.Save(ms);
bmp = new Bitmap(ms);
}
return bmp;
}
Output of the first page:

Entire Code:
using Spire.Doc;
using Spire.Doc.Documents;
namespace Word2Image
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
Document doc = new Document("sample.docx", FileFormat.Docx2010);
BitmapSource[] bss = doc.SaveToImages(ImageType.Bitmap);
for (int i = 0; i < bss.Length; i++)
{
SourceToBitmap(bss[i]).Save(string.Format("img-{0}.png", i));
}
}
private Bitmap SourceToBitmap(BitmapSource source)
{
Bitmap bmp;
using (MemoryStream ms = new MemoryStream())
{
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(source));
encoder.Save(ms);
bmp = new Bitmap(ms);
}
return bmp;
}
}
}
How to change the font and size for Excel header and footer in C#
2014-10-29 07:18:58 Written by KoohjiExcel header and footer give additional information of an Excel sheet, such as the page number, author name, topic name and date etc. With the help of Spire.XLS, developers can easily add text header and footer into an excel spreadsheet. Sometimes, the font and size of the header and footer may be different with the font in the excel documents, and it makes the document in disorder. This article will demonstrate how to change the font and size for the text on excel header and footer.
Firstly, check the original font and size on Excel header and footer:

By using Spire.XLS, we can easily set the new size and font for the header and footer text. Here comes to the steps of how to change the font and size for excel header and footer:
Step 1: Create an excel document and load the Excel with header and footer from file:
Workbook workbook = new Workbook();
workbook.LoadFromFile("Sample.xlsx");
Step 2: Gets the first worksheet in the Excel file
Worksheet sheet = workbook.Worksheets[0];
Step 3: Set the new font and size for the header and footer
string text = sheet.PageSetup.LeftHeader; //"Arial Unicode MS" is font name, "18" is font size text = "&\"Arial Unicode MS\"&18 Header Footer Sample by Spire.XLS "; sheet.PageSetup.LeftHeader = text; sheet.PageSetup.RightFooter = text;
Step 4: Save the document to file and preview it
workbook.SaveToFile(@"..\Result.xlsx",ExcelVersion.Version2010); System.Diagnostics.Process.Start(@"..\Result.xlsx");
Effective screenshot after changing the font and size for the header:

Full codes:
using Spire.Xls;
namespace Changefontforheaderfooter
{
class Program
{
static void Main(string[] args)
{
Workbook workbook = new Workbook();
workbook.LoadFromFile("Sample.xlsx");
Worksheet sheet = workbook.Worksheets[0];
string text = sheet.PageSetup.LeftHeader;
//"Arial Unicode MS" is font name, "18" is font size
text = "&\"Arial Unicode MS\"&18 Header Footer Sample by Spire.XLS ";
sheet.PageSetup.LeftHeader = text;
sheet.PageSetup.RightFooter = text;
//Save workbook and preview it
workbook.SaveToFile(@"..\Result.xlsx",ExcelVersion.Version2010);
System.Diagnostics.Process.Start(@"..\Result.xlsx");
}
}
}