Knowledgebase (2311)
Children categories
This article is going to introduce how to create, write and save word document in WPF via Spire.Doc for WPF.
Spire.Doc for WPF enables users to do a large range of manipulations (such as create, write, open, edit, convert and save, etc.) on word with high performance and efficiency. In addition, as a powerful and independent library, it doesn’t require Microsoft Office or any other 3rd party tools to be installed on system.
Note: please download and install Spire.Doc correctly and add the dll file from the installation folder as reference.
First, let’s begin to create a word document in WPF.
Use namespace:
using System.Windows; using Spire.Doc; using Spire.Doc.Documents;
Step 1: Create a new word document instance, next add a section and a paragraph to it.
//Create New Word Document doc = new Document(); //Add Section Section section = doc.AddSection(); //Add Paragraph Paragraph Para = section.AddParagraph();
Second, we’re going to write something into the document.
Step 2: Append some text to it.
//Append Text
Para.AppendText("Hello! "
+ "I was created by Spire.Doc for WPF, it's a professional .NET Word component "
+ "which enables developers to perform a large range of tasks on Word document (such as create, open, write, edit, save and convert "
+ "Word document) without installing Microsoft Office and any other third-party tools on system.");
Third, save the generated document.
Step 3: Save and launch the document.
//Save and launch
doc.SaveToFile("MyWord.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("MyWord.docx");
Output:

Full codes:
using Spire.Doc;
using Spire.Doc.Documents;
using System.Windows;
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
//Create New Word
Document doc = new Document();
//Add Section
Section section = doc.AddSection();
//Add Paragraph
Paragraph Para = section.AddParagraph();
//Append Text
Para.AppendText("Hello! "
+ "I was created by Spire.Doc for WPF, it's a professional .NET Word component "
+ "which enables developers to perform a large range of tasks on Word document (such as create, open, write, edit, save and convert "
+ "Word document) without installing Microsoft Office and any other third-party tools on system.");
//Save and launch
doc.SaveToFile("MyWord.docx", Spire.Doc.FileFormat.Docx);
System.Diagnostics.Process.Start("MyWord.docx");
}
}
}
Embed uninstalled fonts by font document when convert word to PDF
2016-01-21 08:27:32 Written by KoohjiWe have already shown you how to use uninstalled font by font document when converting word to PDF. Now starts from Spire.Doc 5.6.3, Spire.Doc newly supports to set the font styles for the uninstalled fonts when convert word documents to PDF. Here comes to the code snippets of how to set the font styles for embed the uninstalled fonts by font documents:
Note: Before Start, please download the latest version of Spire.XLS and add Spire.xls.dll in the bin folder as the reference of Visual Studio.
Step 1: Create a new workbook and load from file.
Document document = new Document();
document.LoadFromFile("Testing.docx");
Step 2: Create an instance for class ToPdfParameterList named parms.
ToPdfParameterList parms = new ToPdfParameterList();
Step 3: Define the path of the uninstalled fonts.
{
new PrivateFontPath("Century Gothic",FontStyle.Regular,"fonts\\GOTHIC.TTF"),
new PrivateFontPath("Century Gothic",FontStyle.Bold,"fonts\\GOTHICB.TTF"),
new PrivateFontPath("Century Gothic",FontStyle.Italic,"fonts\\GOTHICI.TTF") ,
new PrivateFontPath("Century Gothic",FontStyle.Bold|FontStyle.Italic,"fonts\\GOTHICBI.TTF")
};
Step 4: Save the document to file and launch to preview it.
document.SaveToFile("Testing.pdf", parms);
System.Diagnostics.Process.Start("Testing.pdf");
Effective screenshot of the embedded uninstalled fonts by setting the font style after converts to PDF:

class Program
{
static void Main(string[] args)
{
string DEMOPATH = @"..\..\Documents\Myppt14.pptx";
// Retrieve the number of slides, excluding the hidden slides.
Console.WriteLine(RetrieveNumberOfSlides(DEMOPATH, false));
// Retrieve the number of slides, including the hidden slides.
Console.WriteLine(RetrieveNumberOfSlides(DEMOPATH));
Console.ReadKey();
}
public static int RetrieveNumberOfSlides(string fileName, bool includeHidden = true)
{
int slidesCount = 0;
using (PresentationDocument doc = PresentationDocument.Open(fileName, false))
{
// Get the presentation part of the document.
PresentationPart presentationPart = doc.PresentationPart;
if (presentationPart != null)
{
if (includeHidden)
{
slidesCount = presentationPart.SlideParts.Count();
}
else
{
// Each slide can include a Show property, which if hidden
// will contain the value "0". The Show property may not
// exist, and most likely will not, for non-hidden slides.
var slides = presentationPart.SlideParts.Where(
(s) => (s.Slide != null) &&
((s.Slide.Show == null) || (s.Slide.Show.HasValue &&
s.Slide.Show.Value)));
slidesCount = slides.Count();
}
}
}
return slidesCount;
}
}