Add SmartArt to Slide in C#

PowerPoint SmartArt is a special type of graphic object that gives you the ability to construct fancy graphical lists and diagrams in your slide quickly and easily. With just one click, you’re able to create a flow chart, an organization chart or a timeline in slide. This article gives you an example of how to add an organization chart to slide using Spire.Presentation with C#.

//create an instance of Presentation
Presentation ppt = new Presentation();

//set slide size
ppt.SlideSize.Type = SlideSizeType.Screen16x9;

//append an organization chart and specify position and size
ISmartArt smartArt = ppt.Slides[0].Shapes.AppendSmartArt(50, 50, 450, 250, SmartArtLayoutType.OrganizationChart);

//set the style and color of smartart
smartArt.Style = SmartArtStyleType.IntenceEffect;
smartArt.ColorStyle = SmartArtColorType.ColorfulAccentColors3to4;

//remove default notes (represent shape in smartart)
foreach (ISmartArtNode node in smartArt.Nodes)

//add nested shapes to smartart
ISmartArtNode node1 = smartArt.Nodes.AddNode();
ISmartArtNode node1_1 = node1.ChildNodes.AddNode();
ISmartArtNode node1_1_1 = node1_1.ChildNodes.AddNode();
ISmartArtNode node1_1_2 = node1_1.ChildNodes.AddNode();
ISmartArtNode node1_1_3 = node1_1.ChildNodes.AddNode();
ISmartArtNode node1_1_4 = node1_1.ChildNodes.AddNode();
ISmartArtNode node1_1_5 = node1_1.ChildNodes.AddNode();
ISmartArtNode node1_1_6 = node1_1.ChildNodes.AddNode();
ISmartArtNode node1_1_1_1 = node1_1_1.ChildNodes.AddNode();
ISmartArtNode node1_1_1_2 = node1_1_1.ChildNodes.AddNode();
ISmartArtNode node1_1_1_3 = node1_1_1.ChildNodes.AddNode();
ISmartArtNode node1_1_3_1 = node1_1_3.ChildNodes.AddNode();
ISmartArtNode node1_1_3_2 = node1_1_3.ChildNodes.AddNode();
ISmartArtNode node1_1_6_1 = node1_1_6.ChildNodes.AddNode();
ISmartArtNode node1_1_6_2 = node1_1_6.ChildNodes.AddNode();
ISmartArtNode node1_1_6_3 = node1_1_6.ChildNodes.AddNode();

//add text to each shape
node1.TextFrame.Text = "Board of Directors";
node1_1.TextFrame.Text = "General Manager";
node1_1_1.TextFrame.Text = "Supply Department";
node1_1_2.TextFrame.Text = "Sales Department";
node1_1_3.TextFrame.Text = "Productive Department";
node1_1_4.TextFrame.Text = "Finance Department";
node1_1_5.TextFrame.Text = "HR Department";
node1_1_6.TextFrame.Text = "Quality Center";
node1_1_1_1.TextFrame.Text = "Purchase Department";
node1_1_1_2.TextFrame.Text = "Warehouse Manager";
node1_1_1_3.TextFrame.Text = "Logistics Department";
node1_1_3_1.TextFrame.Text = "Production Department";
node1_1_3_2.TextFrame.Text = "Maintenance Department";
node1_1_6_1.TextFrame.Text = "Production Quality Management";
node1_1_6_2.TextFrame.Text = "Production Safety Management";
node1_1_6_3.TextFrame.Text = "Environmental Management";

//save to file
ppt.SaveToFile("output.pptx", FileFormat.Pptx2013);




Insert Image and Formatted Text in Excel Header or Footer in C#

Excel header or footer, which presents information like page numbers, the creation date, the logo of your company, and even customized text, can be of use in printed worksheets. In this article, you’ll learn how to insert images and formatted text in Excel header or footer in C#.

This solution relies on Spire.XLS, an Excel processing library, which provides a PageSetup class to deal with all page setup settings. Specifically, it contains LeftHeader, CenterHeader, LeftHeaderImage, OddHeaderStringand similar properties that represent header/footer text, image, odd header/footer and even header/footer in Excel. Besides, this component provides some special script commands, which are used to get the dynamic value like page number or format the header or footer text.

Script D
&P The current page number
&N The total number of pages
&D The current data
&T The current time
&G A picture
&A The worksheet name
&F The file name
&B Make text bold
&I Italicize text
&U Underline text
&”font name” Represents a font name, for example, &”Aril”
&font size Represents font size, for example, &12
&K Represents font color, for example, &KFF0000

Insert Image to Header

  //create a word document
Workbook wb = new Workbook();

//get the first worksheet
Worksheet sheet = wb.Worksheets[0];

//load a pic to Image object
Image image = Image.FromFile("company-logo.jpg");

//scale the image
Bitmap bitmap = new Bitmap(image, new Size(image.Width / 4, image.Height / 4));

//insert image to left header
sheet.PageSetup.LeftHeaderImage = bitmap;
sheet.PageSetup.LeftHeader = "&G";

//save the file
wb.SaveToFile("Insert_Text_Footer.xlsx", ExcelVersion.Version2013);


Insert Formatted Text to Footer

 //create an object of Workbook class   
Workbook wb = new Workbook();

//get the first sheet  
Worksheet sheet = wb.Worksheets[0];

//format a string with script commands and assign it to CenterFooter   
sheet.PageSetup.CenterFooter = "&\"Arial\"&B&12&KFF0000Copyright © 2017 JMD. All Rights Reserved.";

//save the file
wb.SaveToFile("Insert_Text_Footer.xlsx", ExcelVersion.Version2013);


Convert PPT/PPTX to PNG in C#

It is not uncommon that we may need to convert a PowerPoint file to images or a PDF file in our work. In my previous post, I have introduced how to export PPT/PPTX files as PDFs. This article will introduce how we can convert a PPT/PPTX file into PNGs by using Spire.Presentation in C#.

Here come the code snippets:

using Spire.Presentation;
using System.Drawing;

namespace Conversion
    class Program
        static void Main(string[] args)
            //Initialize an instance of Presentation class
            Presentation presentation = new Presentation();

            //Load a PowerPoint document

            //Traverse each slide in PowerPoint and save as png image
            for (int i = 0; i < presentation.Slides.Count; i++)
                Image image = presentation.Slides[i].SaveAsImage();
                String fileName = String.Format("Result-{0}.png", i);



Compress PDF Files in C#

Knowing how to reduce PDF file size can save you from a number of different frustrating hold-ups—like trying to send a large file via email, only to get the annoying popup telling us the file is too large to send. This post provides two solutions in C# to make PDFs smaller.

Before start, you should download .NET PDF component and add the dll file as reference to your Visual Studio.

Reducing PDF file size by compressing content

Content compression involves removing all extra space characters, inserting a single repeat character to indicate a string of repeated characters and substituting smaller bit strings for frequently occurring characters.

//Creates a new PDF document
PdfDocument doc = new PdfDocument();
//Load file
//Disables the incremental update
doc.FileInfo.IncrementalUpdate = false;
//Sets the compression level to best
doc.CompressionLevel = PdfCompressionLevel.Best;
//Save file

Reducing PDF file size by shrinking all images

For PDF files that contain many images, compressing/changing the quality of them all can make a noticeable difference in making your PDF smaller.

//Creates a new PDF document
PdfDocument pdf = new PdfDocument();
//Load file
pdf.FileInfo.IncrementalUpdate = false;

foreach (PdfPageBase page in pdf.Pages)
    Image[] images = page.ExtractImages();
    if (images != null && images.Length > 0)
        for (int j = 0; j < images.Length; j++)
            Image image = images[j];
            PdfBitmap bp = new PdfBitmap(image);
            bp.Quality = 20; //Reduces the quality of the image
            page.ReplaceImage(j, bp);
//Save file

Convert HTML String to PDF using Free API in C#

Converting from HTML to PDF is not an easy task. There are some tools that can do this, but most of them costs thousands of dollars. This article provides a free solution with only few lines of C# code, using free Spire.PDF.

Free Spire.PDF is a community edition of the fully featured Spire.PDF for .NET. It allows you to create PDF documents directly from HTML pages in your ASP.NET and MVC Websites, Desktop Applications and Services.

You can use the free library in .NET 4.0 and later applications, in C# and VB.NET languages, on 32-bit and 64-bit Windows machines.


Using the code

PdfDocument pdf = new PdfDocument();
PdfPageSettings setting = new PdfPageSettings();
setting.Size = PdfPageSize.A4;

PdfHtmlLayoutFormat htmlLayoutFormat = new PdfHtmlLayoutFormat();
htmlLayoutFormat.IsWaiting = false;                    
string htmlCode = File.ReadAllText("HTML example.html");
Thread thread = new Thread(() =>
{ pdf.LoadFromHTML(htmlCode, false, setting, htmlLayoutFormat); });