Knowledgebase (2311)
Children categories
Split a Word document into multiple documents by section break in C#
2016-07-25 08:45:33 Written by KoohjiIn Word, we can split a word document in an easiest way - open a copy of the original document, delete the sections that we don’t want and then save the remains to local drive. But doing this section by section is rather cumbersome and boring. This article will explain how we can use Spire.Doc for .NET to programmatically split a Word document into multiple documents by section break instead of copying and deleting manually.
Detail steps and code snippets:
Step 1: Initialize a new word document object and load the original word document which has two sections.
Document document = new Document();
document.LoadFromFile("Test.docx");
Step 2: Define another new word document object.
Document newWord;
Step 3: Traverse through all sections of the original word document, clone each section and add it to a new word document as new section, then save the document to specific path.
for (int i = 0; i < document.Sections.Count; i++)
{
newWord = new Document();
newWord.Sections.Add(document.Sections[i].Clone());
newWord.SaveToFile(String.Format(@"test\out_{0}.docx", i));
}
Run the project and we'll get the following output:

Full codes:
using System;
using Spire.Doc;
namespace Split_Word_Document
{
class Program
{
static void Main(string[] args)
{
Document document = new Document();
document.LoadFromFile("Test.doc");
Document newWord;
for (int i = 0; i < document.Sections.Count; i++)
{
newWord = new Document();
newWord.Sections.Add(document.Sections[i].Clone());
newWord.SaveToFile(String.Format(@"test\out_{0}.docx", i));
}
}
}
}
Add simplified and traditional Chinese characters to PDF in C#/VB.NET
2016-07-21 08:02:26 Written by KoohjiSpire.PDF for .NET provides several font classes such as PdfFont, PdfTrueTypeFont and PdfCjkStandardFont which enable developers to add various fonts to PDF files. In this article we will learn how to generate fonts that support Chinese characters and use the fonts to add simplified and traditional Chinese characters to a PDF file in C# and VB.NET.
Before start, please ensure you have installed the corresponding font on system. If not, you can download it from the following link:
http://www.adobe.com/support/downloads/thankyou.jsp?ftpID=5508&fileID=5521
Detail steps and code snippets:
Step 1: Create a new PDF document and add a new page to it.
PdfDocument pdf = new PdfDocument(); PdfPageBase page = pdf.Pages.Add();
Step 2: Use PdfTrueTypeFont class and PdfCjkStandardFont class to generate fonts that support simplified and traditional Chinese characters.
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 11f), true);
PdfCjkStandardFont font1 = new PdfCjkStandardFont(PdfCjkFontFamily.MonotypeSungLight, 11f);
Step 3: Use PdfCanvas.DrawString(string s, PdfFontBase font, PdfBrush brush, float x, float y) method and the generated fonts to draw Chinese characters to specified location of the PDF file.
page.Canvas.DrawString("中国", font, PdfBrushes.Red, 50, 50);
page.Canvas.DrawString("中國", font1, PdfBrushes.Red, 50, 70);
Step 4: Save the PDF file to disk.
pdf.SaveToFile("result.pdf");
Run the project and we'll get the following result PDF file:

Full codes:
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;
namespace Add_Chinese_Characters_to_PDF
{
class Program
{
static void Main(string[] args)
{
PdfDocument pdf = new PdfDocument();
PdfPageBase page = pdf.Pages.Add();
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 11f), true);
PdfCjkStandardFont font1 = new PdfCjkStandardFont(PdfCjkFontFamily.MonotypeSungLight, 11f);
page.Canvas.DrawString("中国", font, PdfBrushes.Red, 50, 50);
page.Canvas.DrawString("中國", font1, PdfBrushes.Red, 50, 70);
pdf.SaveToFile("result.pdf");
System.Diagnostics.Process.Start("result.pdf");
}
}
}
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports System.Drawing
Namespace Add_Chinese_Characters_to_PDF
Class Program
Private Shared Sub Main(args As String())
Dim pdf As New PdfDocument()
Dim page As PdfPageBase = pdf.Pages.Add()
Dim font As New PdfTrueTypeFont(New Font("Arial Unicode MS", 11F), True)
Dim font1 As New PdfCjkStandardFont(PdfCjkFontFamily.MonotypeSungLight, 11F)
page.Canvas.DrawString("中国", font, PdfBrushes.Red, 50, 50)
page.Canvas.DrawString("中國", font1, PdfBrushes.Red, 50, 70)
pdf.SaveToFile("result.pdf")
System.Diagnostics.Process.Start("result.pdf")
End Sub
End Class
End Namespace
Generally, when we open a PDF document from a PDF viewer, it displays the first page instead of others. For some reasons, we may want to skip the first few pages and start on another page. This article will introduce how to specify a particular page when viewing a PDF document in C# and VB.NET.
Code Snippet:
Step 1: Initialize an instance of PdfDocument class and a load a sample PDF file.
PdfDocument doc = new PdfDocument(); doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");
Step 2: Create a PdfGoToAction which will redirect to a destination (page 2 in this case) in the current document.
PdfDestination destination = new PdfDestination(doc.Pages[1]); PdfGoToAction action = new PdfGoToAction(destination);
Step 3: Sets the action to execute after the document is opened.
doc.AfterOpenAction = action;
Step 4: Save the file.
doc.SaveToFile("GoTo2Page.pdf",FileFormat.PDF);
Output:
The document opens at the second page.

Full Code:
using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.General;
namespace OpenPDF
{
class Program
{
static void Main(string[] args)
{
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");
PdfDestination destination = new PdfDestination(doc.Pages[1]);
PdfGoToAction action = new PdfGoToAction(destination);
action.Destination.Zoom = 0.8F;
doc.AfterOpenAction = action;
doc.SaveToFile("GoTo2Page.pdf", FileFormat.PDF);
}
}
}
Imports Spire.Pdf
Imports Spire.Pdf.Actions
Imports Spire.Pdf.General
Namespace OpenPDF
Class Program
Private Shared Sub Main(args As String())
Dim doc As New PdfDocument()
doc.LoadFromFile("C:\Users\Administrator\Desktop\sample.pdf")
Dim destination As New PdfDestination(doc.Pages(1))
Dim action As New PdfGoToAction(destination)
action.Destination.Zoom = 0.8F
doc.AfterOpenAction = action
doc.SaveToFile("GoTo2Page.pdf", FileFormat.PDF)
End Sub
End Class
End Namespace