.NET (1322)
Children categories
When we work with the pie chart on the presentation slide, we may need to separate each part of pie chart to make them stand out. This article is going to introduce the method of how to set the pie explosion for the pie chart on the presentation slides in C# by using Spire.Presentation.
Spire.Presentation offers a property of chart.Series[].Distance to enable developers to pull the whole pie apart by exploding the pie chart.
On Microsoft PowerPoint, We can adjust the percentage of "Pie Explosion" on the Series Options at the "Format Data Series" area to control the distance between each section in the chart.

Step 1: Create a presentation document and load the file from disk.
Presentation ppt = new Presentation();
ppt.LoadFromFile("Sample.pptx");
Step 2: Get the chart that needs to set the point explosion.
IChart chart = ppt.Slides[0].Shapes[0] as IChart;
Step 3: Explode the pie chart.
chart.Series[0].Distance = 15;
Step 4: Save the document to file.
ppt.SaveToFile("Result.pptx", FileFormat.Pptx2010);
Effective screenshots after exploding the pie chart on presentation slide:

Full codes:
using Spire.Presentation;
using Spire.Presentation.Charts;
namespace ExplodePie
{
class Program
{
static void Main(string[] args)
{
Presentation ppt = new Presentation();
ppt.LoadFromFile("Sample.pptx");
IChart chart = ppt.Slides[0].Shapes[0] as IChart;
chart.Series[0].Distance = 15;
ppt.SaveToFile("Result.pptx", FileFormat.Pptx2010);
}
}
}
At some point, we may want to display a PDF file as it will appear when printed. This article demonstrates how to show print preview of a PDF file in Windows Forms application using Spire.PDF and c#.
Before using the following code, we need to create a windows forms application, add a PrintPreviewControl control to the form and reference Spire.Pdf.dll into the application.
using System;
using System.Windows.Forms;
using Spire.Pdf;
namespace PreviewPDF
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void printPreviewControl1_Click(object sender, EventArgs e)
{
//Load PDF file
PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("New Zealand.pdf");
//Set the PrintPreviewControl.Rows and PrintPreviewControl.Columns properties to show multiple pages
this.printPreviewControl1.Rows = 2;
this.printPreviewControl1.Columns = 2;
//Preview the pdf file
pdf.Preview(this.printPreviewControl1);
}
}
}
Screenshot:

A DataMatrix code is a two-dimensional barcode consisting of black and white "cells" or modules arranged in either a square or rectangular pattern. The information to be encoded can be text or numeric data.
Following code snippets show how to create a DataMatrix barcode image using Spire.Barcode.
Step 1: Create a BarcodeSettings instance.
BarcodeSettings settings = new BarcodeSettings();
Step 2: Set the width of barcode module
settings.X = 2;
Step 3: Set the barcode type as DataMatrix.
settings.Type = BarCodeType.DataMatrix;
Step 4: Set the data and display text
settings.Data = "ABC 123456789ABC 123456789ABC 123456789"; settings.Data2D = "ABC 123456789ABC 123456789ABC 123456789";
Step 5: Set the DataMatrix symbol shape to square.
settings.DataMatrixSymbolShape = DataMatrixSymbolShape.Square;
Step 6: Generate barcode image based on the settings and save it in .png format.
BarCodeGenerator generator = new BarCodeGenerator(settings);
Image image = generator.GenerateImage();
image.Save("DataMatrix.png", System.Drawing.Imaging.ImageFormat.Png);

To create a rectangular DataMatrix barcode, set the DataMatrixSymbolShape property to Rectangle.
settings.DataMatrixSymbolShape = DataMatrixSymbolShape.Rectangle;

Full Code:
using Spire.Barcode;
using System.Drawing;
namespace DataMatrix
{
class Program
{
static void Main(string[] args)
{
BarcodeSettings settings = new BarcodeSettings();
settings.Type = BarCodeType.DataMatrix;
settings.X = 1.5f;
settings.DataMatrixSymbolShape = DataMatrixSymbolShape.Square;
//rectangular DataMatrix barcode
//settings.DataMatrixSymbolShape = DataMatrixSymbolShape.Rectangle;
settings.Data = "ABC 123456789ABC 123456789ABC 123456789";
settings.Data2D = "ABC 123456789ABC 123456789ABC 123456789";
BarCodeGenerator generator = new BarCodeGenerator(settings);
Image image = generator.GenerateImage();
image.Save("DataMatrix.png", System.Drawing.Imaging.ImageFormat.Png);
}
}
}
We have already demonstrated how to add a brand new TOC when we create the word documents. This article will show you how to insert a TOC to the existing word documents with styles and remove the TOC from the word document.
Firstly, view the sample document with Title, Heading1 and Heading 2 styles:

The below code snippet shows how to insert a Table of contents (TOC) into a document.
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Text.RegularExpressions;
namespace InsertTOC
{
class Program
{
static void Main(string[] args)
{
//Create a new instance of Document and load the document from file.
Document doc = new Document();
doc.LoadFromFile("Sample.docx", FileFormat.Docx2010);
//Add the TOC to the document
TableOfContent toc = new TableOfContent(doc, "{\\o \"1-3\" \\h \\z \\u}");
Paragraph p = doc.LastSection.Paragraphs[0];
p.Items.Add(toc);
p.AppendFieldMark(FieldMarkType.FieldSeparator);
p.AppendText("TOC");
p.AppendFieldMark(FieldMarkType.FieldEnd);
doc.TOC = toc;
//Update the table of contents
doc.UpdateTableOfContents();
//Save the document to file
doc.SaveToFile("Result.docx", FileFormat.Docx);
}
}
}

Removing a Table of Contents from the Document
using Spire.Doc;
using System.Text.RegularExpressions;
namespace RemoveTOC
{
class Program
{
static void Main(string[] args)
{
//load the document from file with TOC
Document doc = new Document();
doc.LoadFromFile("Result.docx", FileFormat.Docx2010);
//get the first body from the first section
Body body = doc.Sections[0].Body;
//remove TOC from first body
Regex regex = new Regex("TOC\\w+");
for (int i = 0; i < body.Paragraphs.Count; i++)
{
if (regex.IsMatch(body.Paragraphs[i].StyleName))
{
body.Paragraphs.RemoveAt(i);
i--;
}
}
//save the document to file
doc.SaveToFile("RemoveTOC.docx", FileFormat.Docx2010);
}
}
}

We have already demonstrated how to view the PDF file on the web with the help of Spire.PDFViewer for ASP.NET. This article we will demonstrate how to use three PDFViewer Control to view multiple PDF files on One Web Form in C#.
Before started, we need to create a new ASP.NET Empty Web Application in Visual Studio and add the Spire.PDFViewer.Asp dll file as the references. Then add the PDFViewer control and the PDFDocumentViewer control into toolbox.
Usually, we can Right-click Default.aspx, select view designer, and then drag the PDFViewer control from toolbox into Deafault.aspx to view one PDF file from web. This article we will use the code to add the PDFViewer control to one Web Form.
Step 1: Right-click Default.aspx, click the “Source” and use the following code to add three PDFViewer control from toolbox into Deafault.aspx. We can set the size for each PDFViewer control.
<div>
<cc1:PdfViewer ID="PdfViewer1" runat="server" Height="378px"
style="margin-top: 33px" Width="961px">
</cc1:PdfViewer>
</div>
<cc1:PdfViewer ID="PdfViewer2" runat="server"
Height="200px" style="margin-top: 20px" Width="961px">
</cc1:PdfViewer>
<cc1:PdfViewer ID="PdfViewer3" runat="server"
Height="200px" style="margin-top: 20px" Width="961px">
</cc1:PdfViewer>

Step 2: Add a new folder under the projects and add the sample PDF files need to view on the web.

Step 3: Double-click Default.aspx.cs, add the code below to load a PDF file.
this.PdfViewer1.LoadFromFile("Document/Test.pdf");
this.PdfViewer2.LoadFromFile("Document/Sample.pdf");
this.PdfViewer3.LoadFromFile("Document/Test2.pdf");
After clicking the debug button, we will view the three PDF files on web.

A PDF Portfolio can combine a wide range of file types such as Word, Excel, PDF and Image files, compared with merging files into a single PDF file, PDF Portfolio remains the individual identities of the files, and you can easily open, read, edit, and format each of them independently of the other files in the PDF Portfolio.
Spire.PDF allows developers to detect if a PDF file is a Portfolio programmatically using c# and vb.net. The following example uses a PDF Portfolio consists of an image, a PDF document and a Word file:

Detail steps:
Step 1: Instantiate a PdfDocument object and load the PDF file.
PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("Portfolio.pdf");
Step 2: Detect if the PDF file is a Portfolio.
bool isPortfolio = pdf.IsPortfolio;
if (isPortfolio)
{
Console.WriteLine("It's a Portfolio!");
}
Screenshot:

Full code:
using System;
using Spire.Pdf;
namespace Detect_if_a_PDF_is_a_Portfolio
{
class Program
{
static void Main(string[] args)
{
PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("Portfolio.pdf");
bool isPortfolio = pdf.IsPortfolio;
if (isPortfolio)
{
Console.WriteLine("It's a Portfolio!");
}
Console.ReadKey();
}
}
}
Imports Spire.Pdf
Namespace Detect_if_a_PDF_is_a_Portfolio
Class Program
Private Shared Sub Main(args As String())
Dim pdf As New PdfDocument()
pdf.LoadFromFile("Portfolio.pdf")
Dim isPortfolio As Boolean = pdf.IsPortfolio
If isPortfolio Then
Console.WriteLine("It's a Portfolio!")
End If
Console.ReadKey()
End Sub
End Class
End Namespace
With Spire.Doc, we can set the formats for paragraph in C#. This article will focus on demonstrate how to set the spacing before and after the paragraph in C#.
Set the spacing before and after the paragraph for a newly added paragraph added by the method of paragraph.AppendHTML() to a new blank word document.
//create a new word document and add a section and paragraph to it.
Document doc = new Document();
Section sec = doc.AddSection();
Paragraph para = sec.AddParagraph();
//Add the text strings to the paragraph and set the style
para.AppendHTML("Add a new paragraph to the word and set the spacing
");
para.ApplyStyle(BuiltinStyle.Heading1);
//set the spacing before and after
para.Format.BeforeAutoSpacing = false;
para.Format.BeforeSpacing = 20;
para.Format.AfterAutoSpacing = false;
para.Format.AfterSpacing = 20;
//save the document to file
doc.SaveToFile("Result1.docx");

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace SetSpacing
{
class Program
{
static void Main(string[] args)
{
//create a new word document and load the sample from file
Document document = new Document();
document.LoadFromFile("sample.docx", FileFormat.Docx);
//Add the text strings to the paragraph and set the style
Paragraph para = new Paragraph(document);
TextRange textRange1 = para.AppendText("This is a inserted paragraph.");
textRange1.CharacterFormat.TextColor = Color.Blue;
textRange1.CharacterFormat.FontSize = 15;
//set the spacing before and after
para.Format.BeforeAutoSpacing = false;
para.Format.BeforeSpacing = 10;
para.Format.AfterAutoSpacing = false;
para.Format.AfterSpacing = 10;
//insert the added paragraph to the word document
document.Sections[0].Paragraphs.Insert(1, para);
//save the document to file
document.SaveToFile("Result2.docx", FileFormat.Docx2010);
}
}
}

When creating a PDF layer, Spire.PDF allows developers to set an initial visibility state for the layer. While it also supports to change the visibility of existing layers in a PDF document. This article explains how to show or hide the existing layers using Spire.PDF.
PdfLayer.Visibility property is used to change the visibility of a PDF layer. To show a hidden layer, set the PdfLayer.Visibility property to PdfVisibility.On. To hide an existing layer, set the PdfLayer.Visibility to PdfVisibility.Off.
The following example shows how to hide a specific PDF layers:
using Spire.Pdf;
using Spire.Pdf.Graphics.Layer;
namespace HideLayer
{
class Program
{
static void Main(string[] args)
{
using (PdfDocument doc = new PdfDocument("AddLayers.pdf"))
{
//Hide the layer by index
doc.Layers[1].Visibility = PdfVisibility.Off;
//Hide the layer by Name
//doc.Layers["BlueLine"].Visibility = PdfVisibility.Off;
//Save the file
doc.SaveToFile("HideLayer.pdf");
}
}
}
}
To show or hide all of the layers:
using Spire.Pdf;
using Spire.Pdf.Graphics.Layer;
namespace ShowLayer
{
class Program
{
static void Main(string[] args)
{
using (PdfDocument doc = new PdfDocument("AddLayers.pdf"))
{
for (int i = 0; i < doc.Layers.Count; i++)
{
//Show all of the layers
//doc.Layers[i].Visibility = PdfVisibility.On;
//Hide all of the layers
doc.Layers[i].Visibility = PdfVisibility.Off;
}
//Save the file
doc.SaveToFile("HideAllLayers.pdf");
}
}
}
}
Screeshot of the sample PDF document:

Screenshot after hiding all of the layers:

Spire.Doc supports to add new shape and shape group to the word document, it also supports to remove the shapes from the word. This article will show you how to reset the size of shape on an existing word document via Spire.Doc.
Firstly, view the original shape on the word document:

Step 1: Create a new instance of word document and load the document from file.
Document doc = new Document();
doc.LoadFromFile("Sample.docx",FileFormat.Docx2010);
Step 2: Get the first section and the third paragraph that contains the shape.
Section section = doc.Sections[0]; Paragraph para = section.Paragraphs[2];
Step 3: Get the shape and reset the width and height for the shape.
ShapeObject shape = para.ChildObjects[0] as ShapeObject; shape.Width = 400; shape.Height = 300;
Step 4: Save the document to file.
doc.SaveToFile("result.docx",FileFormat.Docx2010);
Effective screenshot of the shape after reset the size.

Full codes:
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace Reset
{
class Program
{
static void Main(string[] args)
{
Document doc = new Document();
doc.LoadFromFile("Sample.docx", FileFormat.Docx2010);
Section section = doc.Sections[0];
Paragraph para = section.Paragraphs[2];
ShapeObject shape = para.ChildObjects[0] as ShapeObject;
shape.Width = 400;
shape.Height = 300;
doc.SaveToFile("result.docx", FileFormat.Docx2010);
}
}
}
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Namespace Reset
Class Program
Private Shared Sub Main(args As String())
Dim doc As New Document()
doc.LoadFromFile("Sample.docx", FileFormat.Docx2010)
Dim section As Section = doc.Sections(0)
Dim para As Paragraph = section.Paragraphs(2)
Dim shape As ShapeObject = TryCast(para.ChildObjects(0), ShapeObject)
shape.Width = 400
shape.Height = 300
doc.SaveToFile("result.docx", FileFormat.Docx2010)
End Sub
End Class
End Namespace
Print Multiple PDF Pages per Sheet and Print Single PDF Page to Multiple Sheets in C#
2017-10-24 08:04:36 Written by KoohjiWhen it comes to printing, Spire.PDF offers users a wide range of options to fulfil their printing needs. Two of these options are “print multiple PDF pages per sheet” and “print single PDF page to multiple sheets”. This article elaborates how to achieve these two options using Spire.PDF and C#.
Print Multiple PDF Pages per Sheet
Uses can invoke the SelectMultiPageLayout(int rows, int columns) method of the PdfPrintSettings class to print multiple PDF pages per sheet of paper.
using Spire.Pdf;
namespace PrintPDF
{
class Program
{
static void Main(string[] args)
{
//Initialize a PdfDocument object
PdfDocument pdf = new PdfDocument();
//Load the pdf file
pdf.LoadFromFile("Input.pdf");
//Print two PDF pages per sheet
pdf.PrintSettings.SelectMultiPageLayout(1, 2);
pdf.Print();
}
}
}
Below screenshot shows the sample pdf document which contains two pages:

Screenshot after printing to XPS:

Print Single PDF Page to Multiple Sheets
The SelectSplitPageLayout() method of the PdfPrintSettings class can be used when printing a large PDF page to multiple sheets. This method splits the PDF page to multiple pages according to the standard A4 paper size: 595pt*842pt.
using Spire.Pdf;
namespace PrintPDF2
{
class Program
{
static void Main(string[] args)
{
//Initialize a PdfDocument object
PdfDocument pdf = new PdfDocument();
//Load the pdf file
pdf.LoadFromFile("Input1.pdf");
//Print the PDF page to multiple sheets
pdf.PrintSettings.SelectSplitPageLayout();
pdf.Print();
}
}
}
Below screenshot shows the sample pdf document which contains a single page with a size of 2100pt*750pt:

Screenshot after printing to XPS:
