Knowledgebase (2417)
Children categories
We have discussed before about converting workbook to PDF. However, in this section, we will show you a neat solution to convert the specific worksheet to PDF with C# and VB.NET in workbook. Apply Spire.Xls for .NET in your application and you can turn worksheet into PDF easily without changing the layout of worksheet.
In the following sections, we will demonstrate how to convert worksheet to PDF.
Step 1: Initialize a new instance of Workbook class and load the sample Excel file.
Workbook workbook = new Workbook();
workbook.LoadFromFile("Sample.xlsx");
Step 2: Get its first worksheet.
Worksheet sheet = workbook.Worksheets[0];
Step 3: Convert the selected worksheet to PDF and save to file.
sheet.SaveToPdf("toPDF.pdf");
Step 4: Launch the file.
System.Diagnostics.Process.Start("toPDF.pdf");
Effective screenshot:

Full codes:
using Spire.Xls;
namespace Excel_Worksheet_to_PDF
{
class Program
{
static void Main(string[] args)
{
Workbook workbook = new Workbook();
workbook.LoadFromFile("Sample.xlsx");
Worksheet sheet = workbook.Worksheets[0];
sheet.SaveToPdf("toPDF.pdf");
System.Diagnostics.Process.Start("toPDF.pdf");
}
}
}
Imports Spire.Xls
Namespace Excel_Worksheet_to_PDF
Class Program
Private Shared Sub Main(args As String())
Dim workbook As New Workbook()
workbook.LoadFromFile("Sample.xlsx")
Dim sheet As Worksheet = workbook.Worksheets(0)
sheet.SaveToPdf("toPDF.pdf")
System.Diagnostics.Process.Start("toPDF.pdf")
End Sub
End Class
End Namespace
In Word, bookmark is used for positioning, it enables readers to return to the read or modified location quickly. Thus, it would be necessary for us to insert a bookmark when we are editing or reading a long word document, so that next time we can get back to the specific location in a short period.
This article will demonstrate how to insert word bookmark in WPF by using Spire.Doc for WPF.
At first, please download Spire.Doc and install it correctly, then add Spire.Doc.Wpf.dll and Spire.License.dll from the installation folder as reference.
This is the effective screenshot after inserting bookmark:

Now, follow the Detail steps below:
Use namespace:
using System.Windows; using Spire.Doc;
Step 1: Initialize a new instance of the Document class and load the word document from file.
Document document = new Document();
document.LoadFromFile("Stories.docx");
Step 2: Get its first section and insert bookmark starts from the end of the second paragraph and ends to the third paragraph.
Section section = document.Sections[0];
section.Paragraphs[1].AppendBookmarkStart("bookmark");
section.Paragraphs[2].AppendBookmarkEnd("bookmark");
Step 3: Save and launch the file.
document.SaveToFile("Bookmark.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("Bookmark.docx");
Full codes:
using Spire.Doc;
using System.Windows;
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
Document document = new Document();
document.LoadFromFile("Stories.docx");
Section section = document.Sections[0];
section.Paragraphs[1].AppendBookmarkStart("bookmark");
section.Paragraphs[2].AppendBookmarkEnd("bookmark");
document.SaveToFile("Bookmark.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("Bookmark.docx");
}
}
}
A table is a great way to display data into group, frequently used in our financial reports, academic reports and any other documents that contain a group of similar data. In this article, I am fusing on how to insert a table in Word, fill the table with data and how to format the cells using Spire.Doc for WPF with C#.
Code Snippets
Step 1: Initialize a new instance of Document class, add a section to it.
Document doc = new Document(); Spire.Doc.Section s = doc.AddSection();
Step 2: Define the data that will be filled with table.
String[] Header ={"Vendor No","Vendor Name", "Address","City","State","Zip"};
String[][] data = {
new String[]{"3501","Cacor Corporation","161 Southfield Rd","Southfield","OH","60093"},
new String[]{"3502","Underwater","50 N3rd Street","Indianapolis","IN","46208"},
new String[]{"3503","J.W. Luscher Mfg.","65 Addams Street","Berkely","MA","02779"},
new String[]{"3504","Scuba Professionals","3105 East Brace","Rancho Dominguez","CA","90221"},
new String[]{"3505","Divers' Supply Shop", "5208 University Dr","Macon","GA","20865"},
new String[]{"3506","Techniques","52 Dolphin Drive","Redwood City","CA","94065-1086"},
};
Step 3: Add a table with border to section, set the rows and columns number based on the data length.
Spire.Doc.Table table = s.AddTable(true); table.ResetCells(data.Length + 1, Header.Length);
Step 4: Fill the table with the predefined data and format the cells.
Spire.Doc.TableRow FRow= table.Rows[0];
FRow.IsHeader = true;
FRow.Height = 23;
FRow.RowFormat.BackColor = System.Drawing.Color.DarkBlue;
for (int i = 0; i < Header.Length; i++)
{
//Cell Alignment
Paragraph p = FRow.Cells[i].AddParagraph();
FRow.Cells[i].CellFormat.VerticalAlignment = Spire.Doc.Documents.VerticalAlignment.Middle;
p.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
//Fill Header data and Format the Cells
TextRange TR = p.AppendText(Header[i]);
TR.CharacterFormat.FontName = "Calibri";
TR.CharacterFormat.FontSize = 12;
TR.CharacterFormat.TextColor = System.Drawing.Color.White;
TR.CharacterFormat.Bold = true;
}
for (int r = 0; r < data.Length; r++)
{
TableRow DataRow = table.Rows[r + 1];
//Row Height
DataRow.Height = 15;
//C Represents Column.
for (int c = 0; c < data[r].Length; c++)
{
//Cell Alignment
DataRow.Cells[c].CellFormat.VerticalAlignment = Spire.Doc.Documents.VerticalAlignment.Middle;
//Fill Data in Rows
Paragraph p2 =DataRow.Cells[c].AddParagraph();
TextRange TR2=p2.AppendText(data[r][c]);
//Format Cells
p2.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
TR2.CharacterFormat.FontName = "Calibri";
TR2.CharacterFormat.FontSize = 10;
TR2.CharacterFormat.TextColor = System.Drawing.Color.Black;
}
}
Step 5: Call AutoFitBebavior() method to make the table AutoFit to Window.
table.AutoFitBehavior(AutoFitBehaviorType.wdAutoFitWindow);
Step 6: Save and launch the file.
doc.SaveToFile("WordTable.docx");
System.Diagnostics.Process.Start("WordTable.docx");
Output:

Full Code:
private void button1_Click(object sender, RoutedEventArgs e)
{
Document doc = new Document();
Spire.Doc.Section s = doc.AddSection();
String[] Header ={"Vendor No","Vendor Name", "Address","City","State","Zip"};
String[][] data = {
new String[]{"3501","Cacor Corporation","161 Southfield Rd","Southfield","OH","60093"},
new String[]{"3502","Underwater","50 N3rd Street","Indianapolis","IN","46208"},
new String[]{"3503","J.W. Luscher Mfg.","65 Addams Street","Berkely","MA","02779"},
new String[]{"3504","Scuba Professionals","3105 East Brace","Rancho Dominguez","CA","90221"},
new String[]{"3505","Divers' Supply Shop", "5208 University Dr","Macon","GA","20865"},
new String[]{"3506","Techniques","52 Dolphin Drive","Redwood City","CA","94065-1086"},
};
Spire.Doc.Table table = s.AddTable(true);
table.ResetCells(data.Length + 1, Header.Length);
Spire.Doc.TableRow FRow= table.Rows[0];
FRow.IsHeader = true;
FRow.Height = 23;
FRow.RowFormat.BackColor = System.Drawing.Color.DarkBlue;
for (int i = 0; i < Header.Length; i++)
{
Paragraph p = FRow.Cells[i].AddParagraph();
FRow.Cells[i].CellFormat.VerticalAlignment = Spire.Doc.Documents.VerticalAlignment.Middle;
p.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
TextRange TR = p.AppendText(Header[i]);
TR.CharacterFormat.FontName = "Calibri";
TR.CharacterFormat.FontSize = 12;
TR.CharacterFormat.TextColor = System.Drawing.Color.White;
TR.CharacterFormat.Bold = true;
}
for (int r = 0; r < data.Length; r++)
{
TableRow DataRow = table.Rows[r + 1];
DataRow.Height = 15;
for (int c = 0; c < data[r].Length; c++)
{
DataRow.Cells[c].CellFormat.VerticalAlignment = Spire.Doc.Documents.VerticalAlignment.Middle;
Paragraph p2 =DataRow.Cells[c].AddParagraph();
TextRange TR2=p2.AppendText(data[r][c]);
p2.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
TR2.CharacterFormat.FontName = "Calibri";
TR2.CharacterFormat.FontSize = 10;
TR2.CharacterFormat.TextColor = System.Drawing.Color.Black;
}
}
table.AutoFitBehavior(AutoFitBehaviorType.wdAutoFitWindow);
doc.SaveToFile("WordTable.docx");
System.Diagnostics.Process.Start("WordTable.docx");
}