With auto-fit feature, you can automatically reduce the font size of text to fit a shape, or you can shrink or enlarge shape to fit the exact size of your text. This article will show you how to auto-fit text or shape using Spire.Presentation with C#, VB.NET.

Step 1: Create an instance of Presentation class.

Presentation presentation = new Presentation();

Step 2: Insert a shape, and set the AutofitType property to Normal, which means the text automatically shrinks to fit shape.

IAutoShape textShape1 = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(50, 50, 150, 80));
textShape1.TextFrame.Text = "Shrink text to fit shape. Shrink text to fit shape. Shrink text to fit shape. Shrink text to fit shape.";
textShape1.TextFrame.AutofitType = TextAutofitType.Normal;

Step 3: Insert a shape, and set the AutofitType property to Shape, which means the shape size automatically decreases or increases to fit text.

IAutoShape textShape2 = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(350, 50, 150, 80));
textShape2.TextFrame.Text = "Resize shape to fit text.";
textShape2.TextFrame.AutofitType = TextAutofitType.Shape;

Step 4: Save the file.

presentation.SaveToFile("output.pptx", FileFormat.Pptx2013);

Output:

Auto-Fit Text or Shape in PowerPoint in C#, VB.NET

Full Code:

[C#]
Presentation presentation = new Presentation();

IAutoShape textShape1 = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(50, 50, 150, 80));
textShape1.TextFrame.Text = "Shrink text to fit shape. Shrink text to fit shape. Shrink text to fit shape. Shrink text to fit shape.";
textShape1.TextFrame.AutofitType = TextAutofitType.Normal;

IAutoShape textShape2 = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(350, 50, 150, 80));
textShape2.TextFrame.Text = "Resize shape to fit text.";
textShape2.TextFrame.AutofitType = TextAutofitType.Shape;

presentation.SaveToFile("output.pptx", FileFormat.Pptx2013);
[VB.NET]
Dim presentation As Presentation =  New Presentation() 

Dim textShape1 As IAutoShape =  presentation.Slides(0).Shapes.AppendShape(ShapeType.Rectangle,New RectangleF(50,50,150,80)) 
textShape1.TextFrame.Text = "Shrink text to fit shape. Shrink text to fit shape. Shrink text to fit shape. Shrink text to fit shape."
textShape1.TextFrame.AutofitType = TextAutofitType.Normal
Dim textShape2 As IAutoShape =  presentation.Slides(0).Shapes.AppendShape(ShapeType.Rectangle,New RectangleF(350,50,150,80)) 
textShape2.TextFrame.Text = "Resize shape to fit text."
textShape2.TextFrame.AutofitType = TextAutofitType.Shape
presentation.SaveToFile("output.pptx", FileFormat.Pptx2013)
Tuesday, 06 February 2018 07:37

How to explode a doughnut chart in C#

In the previous article, we have demonstrated how to explode a pie chart via Spire.XLS. This article we will explain how to explode a doughnut chart in C#.

When we need to explode a doughnut chart, we can set the chart type as DoughnutExploded when we create a doughnut chart from scratch. For the existing doughnut chart, we can easily change the chart type to explode the doughnut chart.

Step 1: Create an instance of Excel workbook and load the document from file.

Workbook workbook = new Workbook();
workbook.LoadFromFile("DoughnutChart.xlsx",ExcelVersion.Version2010);

Step 2: Get the first worksheet from the sample workbook.

Worksheet sheet = workbook.Worksheets[0];

Step 3: Get the first chart from the first worksheet.

Chart chart = sheet.Charts[0];

Step 4: Set the chart type as DoughnutExploded to explode the doughnut chart.

chart.ChartType = ExcelChartType.DoughnutExploded;

Step 5: Save the document to file.

workbook.SaveToFile("ExplodedDoughnutChart.xlsx", ExcelVersion.Version2010);

Effective screenshot of the explode doughnut chart:

How to explode a doughnut chart in C#

Full codes of how to explode a doughnut chart:

Workbook workbook = new Workbook();
workbook.LoadFromFile("DoughnutChart.xlsx",ExcelVersion.Version2010);

Worksheet sheet = workbook.Worksheets[0];
Chart chart = sheet.Charts[0];
chart.ChartType = ExcelChartType.DoughnutExploded;

workbook.SaveToFile("ExplodedDoughnutChart.xlsx", ExcelVersion.Version2010);
Tuesday, 06 February 2018 07:32

Resize Images in a Word document in C#

We often need to resize images to a desired size after we insert them into a Word document. This article demonstrates how to programmatically resize images in a Word document using Spire.Doc and C#.

Below is the screenshot of the example document we used for demonstration:

Resize Images in a Word document in C#

Detail steps:

Step 1: Load the Word document.

Document document = new Document("Input.docx");

Step 2: Get the first section and the first paragraph in the section.

Section section = document.Sections[0];
Paragraph paragraph = section.Paragraphs[0];

Step 3: Resize images in the paragraph.

foreach (DocumentObject docObj in paragraph.ChildObjects)
{
    if (docObj is DocPicture)
    {
        DocPicture picture = docObj as DocPicture;
        picture.Width = 50f;
        picture.Height = 50f;
    }
} 

Step 4: Save the document.

document.SaveToFile("ResizeImages.docx");

Output:

Resize Images in a Word document in C#

Full code:

Document document = new Document("Input.docx");
Section section = document.Sections[0];
Paragraph paragraph = section.Paragraphs[0];
 
foreach (DocumentObject docObj in paragraph.ChildObjects)
{
    if (docObj is DocPicture)
    {
        DocPicture picture = docObj as DocPicture;
        picture.Width = 50f;
        picture.Height = 50f;
    }
} 
document.SaveToFile("ResizeImages.docx");

Error bars in charts can help us to see margins of error and standard deviations at a glance. We can use error bars in area, bar, column, line, and XY charts. In XY charts i.e. scatter and bubble charts, we can show X (Horizontal) and Y (Vertical) error bars. However if the chart type is not XY, the error bars for X values are disabled.

This article demonstrates how to add and format error bars for non-XY and XY charts in a PowerPoint document using Spire.Presentation and C#.

Detail steps:

Step 1: Initialize an object of Presentation class and load the PowerPoint document.

Presentation ppt = new Presentation();
ppt.LoadFromFile("Input.pptx");

Step 2: Get the column chart on the first slide and set chart title.

IChart columnChart = ppt.Slides[0].Shapes[0] as IChart;
columnChart.ChartTitle.TextProperties.Text = "Vertical Error Bars";

Step 3: Add Y (Vertical) error bars to the first chart series and format the error bars.

//Get Y error bars of the first chart series
IErrorBarsFormat errorBarsYFormat1 = columnChart.Series[0].ErrorBarsYFormat;
//Set end cap
errorBarsYFormat1.ErrorBarNoEndCap = false;
//Specify direction
errorBarsYFormat1.ErrorBarSimType = ErrorBarSimpleType.Plus;
//Specify error amount type
errorBarsYFormat1.ErrorBarvType = ErrorValueType.StandardError;
//Set value
errorBarsYFormat1.ErrorBarVal = 0.3f;            
//Set line format          
errorBarsYFormat1.Line.FillType = FillFormatType.Solid;
errorBarsYFormat1.Line.SolidFillColor.Color = Color.MediumVioletRed;
errorBarsYFormat1.Line.Width = 1;

Step 4: Get the bubble chart on the second slide and set chart title.

IChart chart2 = ppt.Slides[1].Shapes[0] as IChart;
chart2.ChartTitle.TextProperties.Text = "Vertical and Horizontal Error Bars";

Step 5: Add x (Horizontal) and Y (Vertical) error bars to the first chart series and format the error bars.

//Get X error bars of the first chart series
IErrorBarsFormat errorBarsXFormat = bubbleChart.Series[0].ErrorBarsXFormat;
//Set end cap
errorBarsXFormat.ErrorBarNoEndCap = false;
//Specify direction
errorBarsXFormat.ErrorBarSimType = ErrorBarSimpleType.Both;

//Specify error amount type
errorBarsXFormat.ErrorBarvType = ErrorValueType.StandardError;
//Set value
errorBarsXFormat.ErrorBarVal = 0.3f;
            
//Get Y error bars of the first chart series
IErrorBarsFormat errorBarsYFormat2 = bubbleChart.Series[0].ErrorBarsYFormat;
//Set end cap
errorBarsYFormat2.ErrorBarNoEndCap = false;
//Specify direction
errorBarsYFormat2.ErrorBarSimType = ErrorBarSimpleType.Both;
//Specify error amount type
errorBarsYFormat2.ErrorBarvType = ErrorValueType.StandardError;
//Set value
errorBarsYFormat2.ErrorBarVal = 0.3f;

Step 6: Save the document.

ppt.SaveToFile("ErrorBars.pptx", FileFormat.Pptx2013);

Screenshot:

Add and Format Error Bars in PowerPoint Charts in C#

Add and Format Error Bars in PowerPoint Charts in C#

Full code:

using System.Drawing;
using Spire.Presentation;
using Spire.Presentation.Charts;
using Spire.Presentation.Drawing;

namespace Add_error_bars_to_chart_in_PPT
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load the PowerPoint document
            Presentation ppt = new Presentation();
            ppt.LoadFromFile("Input.pptx");

            //Get the column chart on the first slide and set chart title
            IChart columnChart = ppt.Slides[0].Shapes[0] as IChart;
            columnChart.ChartTitle.TextProperties.Text = "Vertical Error Bars";

            //Add Y (Vertical) Error Bars

            //Get Y error bars of the first chart series
            IErrorBarsFormat errorBarsYFormat1 = columnChart.Series[0].ErrorBarsYFormat;
            //Set end cap
            errorBarsYFormat1.ErrorBarNoEndCap = false;
            //Specify direction
            errorBarsYFormat1.ErrorBarSimType = ErrorBarSimpleType.Plus;
            //Specify error amount type
            errorBarsYFormat1.ErrorBarvType = ErrorValueType.StandardError;
            //Set value
            errorBarsYFormat1.ErrorBarVal = 0.3f;            
            //Set line format          
            errorBarsYFormat1.Line.FillType = FillFormatType.Solid;
            errorBarsYFormat1.Line.SolidFillColor.Color = Color.MediumVioletRed;
            errorBarsYFormat1.Line.Width = 1;

            //Get the bubble chart on the second slide and set chart title
            IChart bubbleChart = ppt.Slides[1].Shapes[0] as IChart;
            bubbleChart.ChartTitle.TextProperties.Text = "Vertical and Horizontal Error Bars";

            //Add X (Horizontal) and Y (Vertical) Error Bars

            //Get X error bars of the first chart series
            IErrorBarsFormat errorBarsXFormat = bubbleChart.Series[0].ErrorBarsXFormat;
            //Set end cap
            errorBarsXFormat.ErrorBarNoEndCap = false;
            //Specify direction
            errorBarsXFormat.ErrorBarSimType = ErrorBarSimpleType.Both;
            //Specify error amount type
            errorBarsXFormat.ErrorBarvType = ErrorValueType.StandardError;
            //Set value
            errorBarsXFormat.ErrorBarVal = 0.3f;
            
            //Get Y error bars of the first chart series
            IErrorBarsFormat errorBarsYFormat2 = bubbleChart.Series[0].ErrorBarsYFormat;
            //Set end cap
            errorBarsYFormat2.ErrorBarNoEndCap = false;
            //Specify direction
            errorBarsYFormat2.ErrorBarSimType = ErrorBarSimpleType.Both;
            //Specify error amount type
            errorBarsYFormat2.ErrorBarvType = ErrorValueType.StandardError;
            //Set value
            errorBarsYFormat2.ErrorBarVal = 0.3f;
                        
            //Save the document
            ppt.SaveToFile("ErrorBars.pptx", FileFormat.Pptx2013);
        }
    }
}

Spire.Presentation supports to operate the paragraph styles from code. There are two kinds of special indentation styles for the paragraph, first line and hanging. This article will demonstrate how to set the indent style for the paragraph on presentation slide in C#.

Step 1: Create an instance of presentation and load the document from file.

Presentation presentation = new Presentation();
presentation.LoadFromFile("Sample.pptx");

Step 2: Get the paragraphs from the first slide.

IAutoShape shape = (IAutoShape)presentation.Slides[0].Shapes[0];
ParagraphCollection paras = shape.TextFrame.Paragraphs;

Step 3: Set the indentation as first line for the first paragraph.

paras[0].Indent = 20;
paras[0].SpaceAfter = 10;

Step 4: Set the indentation as Hanging for the third paragraph.

paras[2].Indent = -100;
paras[2].LeftMargin = 30;

Step 5: Save the document to file.

presentation.SaveToFile("Result.pptx", FileFormat.Pptx2010);

Effective screenshot of the presentation with indentation style:

How to set the indent style for the paragraph on presentation slide in C#

Full codes:

Presentation presentation = new Presentation();
presentation.LoadFromFile("Sample.pptx");

IAutoShape shape = (IAutoShape)presentation.Slides[0].Shapes[0];
ParagraphCollection paras = shape.TextFrame.Paragraphs;

paras[0].Indent = 20;
paras[0].SpaceAfter = 10;

paras[2].Indent = -100;
paras[2].LeftMargin = 30;

presentation.SaveToFile("Result.pptx", FileFormat.Pptx2010);

A file with the .ODT file extension is an OpenDocument Text document file. These files are most often created by the free OpenOffice Writer word processor program. ODT files are similar to the popular DOCX file format used with Microsoft Word. Both of the two file types can hold things like text, images, objects, and styles.

However, when you open an ODT document in Microsoft Word, the formatting of the ODT document may differ as a result of the two programs not sharing the same features. When converting ODT to DOCX or vice versa, the data and content will be converted successfully, but may not including the original formatting.

Following code snippets introduce how to convert ODT to DOC or DOCX and vice versa using Spire.Doc.

ODT to DOCX

To convert ODT to DOC, change the file extension and file format to .Doc in SaveToFile method.

[C#]
Document doc = new Document();
doc.LoadFromFile("SampleODTFile.odt");
doc.SaveToFile("output.docx", FileFormat.Docx);
[VB.NET]
Document doc = New Document()
doc.LoadFromFile("SampleODTFile.odt")
doc.SaveToFile("output.docx", FileFormat.Docx)

DOCX to ODT

To convert Doc to ODT, load a .Doc file format document when loading the source file.

[C#]
Document doc = new Document();
doc.LoadFromFile("SampleWordFile.docx");
doc.SaveToFile("output.odt", FileFormat.Odt);
[VB.NET]
Document doc = New Document()
doc.LoadFromFile("SampleWordFile.docx")
doc.SaveToFile("output.odt", FileFormat.Odt)
Thursday, 25 January 2018 06:25

Create Scatter Chart in PowerPoint in C#

A Scatter (X Y) Chart has two value axes – X value axis and Y value axis. It combines X and Y values into single data points and shows them in irregular intervals, or clusters. This article will show you how to create scatter chart in PowerPoint using Spire.Presentation with C#.

Step 1: Create a Presentation object.

Presentation presentation = new Presentation();

Step 2: Add a "Scatter with Smooth Lines and Markers" chart to the first slide, and set the chart title.

IChart chart = presentation.Slides[0].Shapes.AppendChart(ChartType.ScatterSmoothLinesAndMarkers, new RectangleF(40, 80, 550, 320), false);
chart.ChartTitle.TextProperties.Text = "Scatter Chart";
chart.ChartTitle.TextProperties.IsCentered = true;
chart.ChartTitle.Height = 20;
chart.HasTitle = true;

Step 3: Write data to the chart data.

Double[] xdata = new Double[] { 1.0, 2.4, 5.0, 8.9 };
Double[] ydata = new Double[] { 5.3, 15.2, 6.7, 8 };
chart.ChartData[0, 0].Text = "X-Values";
chart.ChartData[0, 1].Text = "Y-Values";
for (Int32 i = 0; i < xdata.Length; ++i)
{
    chart.ChartData[i + 1, 0].Value = xdata[i];
    chart.ChartData[i + 1, 1].Value = ydata[i];
}

Step 4: Set up the data source of the X values, the Y values, and the series label.

chart.Series.SeriesLabel = chart.ChartData["B1", "B1"];
chart.Series[0].XValues= chart.ChartData["A2", "A5"];
chart.Series[0].YValues = chart.ChartData["B2", "B5"];

Step 5: Add and display the data labels in the chart.

for (int i = 0; i < 4; i++)
{
    ChartDataLabel dataLabel = chart.Series[0].DataLabels.Add();
    dataLabel.LabelValueVisible = true;
}

Step 6: Set the axis titles.

chart.PrimaryValueAxis.HasTitle = true;
chart.PrimaryValueAxis.Title.TextProperties.Text = "X-Axis Title";
chart.SecondaryValueAxis.HasTitle = true;
chart.SecondaryValueAxis.Title.TextProperties.Text = "Y-Axis Title";

Step 7: Format the gridlines.

chart.SecondaryValueAxis.MajorGridTextLines.FillType = FillFormatType.Solid;
chart.SecondaryValueAxis.MajorGridTextLines.Style = TextLineStyle.ThinThin;
chart.SecondaryValueAxis.MajorGridTextLines.SolidFillColor.Color = Color.Gray;
chart.PrimaryValueAxis.MajorGridTextLines.FillType = FillFormatType.None;

Step 8: Format the outline.

chart.Series[0].Line.FillType = FillFormatType.Solid;
chart.Series[0].Line.Width = 0.1f;
chart.Series[0].Line.SolidFillColor.Color = Color.RoyalBlue;

Step 9: Save the file.

presentation.SaveToFile("ScatterChart.pptx", FileFormat.Pptx2010);

Output:

Create Scatter Chart in PowerPoint in C#

Full Code:

Presentation presentation = new Presentation();
IChart chart = presentation.Slides[0].Shapes.AppendChart(ChartType.ScatterSmoothLinesAndMarkers, new RectangleF(40, 80, 550, 320), false);
chart.ChartTitle.TextProperties.Text = "Scatter Chart";
chart.ChartTitle.TextProperties.IsCentered = true;
chart.ChartTitle.Height = 20;
chart.HasTitle = true;

Double[] xdata = new Double[] { 1.0, 2.4, 5.0, 8.9 };
Double[] ydata = new Double[] { 5.3, 15.2, 6.7, 8 };
chart.ChartData[0, 0].Text = "X-Values";
chart.ChartData[0, 1].Text = "Y-Values";
for (Int32 i = 0; i < xdata.Length; ++i)
{
    chart.ChartData[i + 1, 0].Value = xdata[i];
    chart.ChartData[i + 1, 1].Value = ydata[i];
}

chart.Series.SeriesLabel = chart.ChartData["B1", "B1"];
chart.Series[0].XValues= chart.ChartData["A2", "A5"];
chart.Series[0].YValues = chart.ChartData["B2", "B5"];

for (int i = 0; i < 4; i++)
{
    ChartDataLabel dataLabel = chart.Series[0].DataLabels.Add();
    dataLabel.LabelValueVisible = true;
}

chart.PrimaryValueAxis.HasTitle = true;
chart.PrimaryValueAxis.Title.TextProperties.Text = "X-Axis Title";
chart.SecondaryValueAxis.HasTitle = true;
chart.SecondaryValueAxis.Title.TextProperties.Text = "Y-Axis Title";

chart.SecondaryValueAxis.MajorGridTextLines.FillType = FillFormatType.Solid;
chart.SecondaryValueAxis.MajorGridTextLines.Style = TextLineStyle.ThinThin;
chart.SecondaryValueAxis.MajorGridTextLines.SolidFillColor.Color = Color.Gray;
chart.PrimaryValueAxis.MajorGridTextLines.FillType = FillFormatType.None;

chart.Series[0].Line.FillType = FillFormatType.Solid;
chart.Series[0].Line.Width = 0.1f;
chart.Series[0].Line.SolidFillColor.Color = Color.RoyalBlue;

presentation.SaveToFile("ScatterChart.pptx", FileFormat.Pptx2010);

We have already demonstrated how to set the animations on shapes in PowerPoint. Now Spire.Presentation starts to support set the Animation Effect for the type and time of the animate text. This article will show you how to use IterateType property and IterateTimeValue property for animation Effect to set the type and time of animate text in C#.

Firstly, view the original sample document with animate text effect with "All at once".

C# Set animation effect for the animate text on the presentation slides

Step 1: Create a presentation instance and load the sample document from file.

Presentation ppt = new Presentation();
ppt.LoadFromFile("Sample.pptx",FileFormat.Pptx2010);

Step 2: Set the AnimateType as Letter.

ppt.Slides[0].Timeline.MainSequence[0].IterateType = Spire.Presentation.Drawing.TimeLine.AnimateType.Letter;

Step 3: Set the IterateTimeValue for the animate text.

ppt.Slides[0].Timeline.MainSequence[0].IterateTimeValue = 10;

Step 4: Save the document to file.

ppt.SaveToFile("Result.pptx", FileFormat.Pptx2010);

Effective screenshot of the animation effect for the animate text effect with "by Letter".

C# Set animation effect for the animate text on the presentation slides

C# Set animation effect for the animate text on the presentation slides

This article demonstrates how to print different pages of a PDF document to different printer trays using Spire.PDF and c#.

Code snippets:

Step 1: Initialize an object of PdfDocument class and Load the PDF document.

PdfDocument doc = new PdfDocument();
doc.LoadFromFile(@"F:\sample.pdf");

Step 2: Set different printer trays for different pages of the document.

doc.PrintSettings.PaperSettings += delegate(object sender, PdfPaperSettingsEventArgs e)
{
    //Set the paper source of page 1-50 as tray 1
    if (1 <= e.CurrentPaper && e.CurrentPaper <= 50)
    {        
        e.CurrentPaperSource = e.PaperSources[0];
    }
    //Set the paper source of the rest of pages as tray 2
    else
    {
        e.CurrentPaperSource = e.PaperSources[1];
    }
};

Step 3: Print the document.

doc.Print();

Full code:

using Spire.Pdf;
using Spire.Pdf.Print;

namespace Print_pages_to_different_printer_trays
{
    class Program
    {
        static void Main(string[] args)
        {
            //Initialize an object of PdfDocument class
            PdfDocument doc = new PdfDocument();
            //Load the PDF document
            doc.LoadFromFile(@"F:\sample.pdf");

            //Set Paper source
            doc.PrintSettings.PaperSettings += delegate(object sender, PdfPaperSettingsEventArgs e)
            {
                //Set the paper source of page 1-50 as tray 1
                if (1 <= e.CurrentPaper && e.CurrentPaper <= 50)
                {
                    e.CurrentPaperSource = e.PaperSources[0];
                }
                //Set the paper source of the rest of pages as tray 2
                else
                {
                    e.CurrentPaperSource = e.PaperSources[1];
                }
            };
            //Print the document
            doc.Print();
        }
    }
}

A theme is a set of colors, fonts, and effects that determines the overall look of your Word document. Suppose you have a document which is neat and stylish, you’d like to copy contents of a section to another document without losing the theme and style. You can clone the theme to destination file using CloneThemeTo method.

Step 1: Create a Document object and load a sample Word file.

Document doc = new Document();
doc.LoadFromFile("theme.docx");

Step 2: Create a new Word document.

Document newWord = new Document();

Step 3: Clone default style, theme, compatibility from the source file to destination document.

doc.CloneDefaultStyleTo(newWord);
doc.CloneThemesTo(newWord);
doc.CloneCompatibilityTo(newWord);

Step 4: Add the cloned section to destination document.

newWord.Sections.Add(doc.Sections[0].Clone());

Step 5: Save the file.

newWord.SaveToFile("result.docx", FileFormat.Docx);

Output:

Preserve Theme When Copying Sections from One Word Document to Another in C#

Full Code:

Document doc = new Document();
doc.LoadFromFile("theme.docx");

Document newWord = new Document();     
doc.CloneDefaultStyleTo(newWord);
doc.CloneThemesTo(newWord);
doc.CloneCompatibilityTo(newWord);
newWord.Sections.Add(doc.Sections[0].Clone());

newWord.SaveToFile("result.docx", FileFormat.Docx);
Page 1 of 90