page 260

Spire.Presentation is a powerful and easy-to-use .NET component, especially designed for developers. Using Spire.Presentation you can generate, modify, convert, render, and print documents without installing Microsoft PowerPoint on your machine. In this document, I will introduce you how to create PowerPoint file and save it to stream. I also introduce you how to load PowerPoint file from stream. Check it below.

Create PowerPoint file and save it to Stream

Step 1: Create Presentation instance.

Presentation presentation = new Presentation();

Step 2: Set background image.

string ImageFile = "bg.png";
RectangleF rect = new RectangleF(0, 0, presentation.SlideSize.Size.Width, presentation.SlideSize.Size.Height);
presentation.Slides[0].Shapes.AppendEmbedImage(ShapeType.Rectangle, ImageFile, rect);
presentation.Slides[0].Shapes[0].Line.FillFormat.SolidFillColor.Color = Color.FloralWhite;

Step 3: Add text to PowerPoint document.

IAutoShape shape = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(50, 70, 450, 150));
shape.Fill.FillType = FillFormatType.None;
shape.ShapeStyle.LineColor.Color = Color.White;

//add text to shape
shape.TextFrame.Text = "This demo shows how to Create PowerPoint file and save it to Stream.";
shape.TextFrame.Paragraphs[0].TextRanges[0].Fill.FillType = FillFormatType.Solid;
shape.TextFrame.Paragraphs[0].TextRanges[0].Fill.SolidColor.Color = Color.Black;

Step 4: Save document to stream.

FileStream to_stream = new FileStream("To_stream.pptx", FileMode.Create);
presentation.SaveToFile(to_stream, FileFormat.Pptx2010);
to_stream.Close();

Preview the generated PowerPoint file:

Save and Load PowerPoint file to/from Stream

Load PowerPoint file from stream.

Step 1: Create Presentation instance.

Presentation presentationLoad = new Presentation();

Step 2: Load PowerPoint file from stream.

FileStream from_stream = File.OpenRead("sample.pptx");
presentationLoad.LoadFromStream(from_stream, FileFormat.Pptx2010);

Step 3: Save the document.

presentationLoad.SaveToFile("From_stream.pptx",FileFormat.Pptx2010);
from_stream.Dispose();

Preview the PowerPoint file:

Save and Load PowerPoint file to/from Stream

Full code:

[C#]
using Spire.Presentation;
using Spire.Presentation.Drawing;
using System.Drawing;
using System.IO;
namespace SavePPTtoStream
{
    class Program
    {
        static void Main(string[] args)
        {

            //A: create PowerPoint file and save it to stream
            Presentation presentation = new Presentation();

            //set background Image
            string ImageFile = "bg.png";
            RectangleF rect = new RectangleF(0, 0, presentation.SlideSize.Size.Width, presentation.SlideSize.Size.Height);
            presentation.Slides[0].Shapes.AppendEmbedImage(ShapeType.Rectangle, ImageFile, rect);
            presentation.Slides[0].Shapes[0].Line.FillFormat.SolidFillColor.Color = Color.FloralWhite;

            //append new shape
            IAutoShape shape = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(50, 70, 450, 150));
            shape.Fill.FillType = FillFormatType.None;
            shape.ShapeStyle.LineColor.Color = Color.White;

            //add text to shape
            shape.TextFrame.Text = "This demo shows how to Create PowerPoint file and save it to Stream.";
            shape.TextFrame.Paragraphs[0].TextRanges[0].Fill.FillType = FillFormatType.Solid;
            shape.TextFrame.Paragraphs[0].TextRanges[0].Fill.SolidColor.Color = Color.Black;

            //save to Stream
            FileStream to_stream = new FileStream("To_stream.pptx", FileMode.Create);
            presentation.SaveToFile(to_stream, FileFormat.Pptx2010);
            to_stream.Close();

            System.Diagnostics.Process.Start("To_stream.pptx");


            //B: Load PowerPoint file from Stream
            Presentation presentationLoad = new Presentation();

            //load PowerPoint file from stream
            FileStream from_stream = File.OpenRead("sample.pptx");
            presentationLoad.LoadFromStream(from_stream, FileFormat.Pptx2010);

            //save the document
            presentationLoad.SaveToFile("From_stream.pptx", FileFormat.Pptx2010);
            from_stream.Dispose();

            System.Diagnostics.Process.Start("From_stream.pptx");


        }
    }
}

If you couldn't successfully use Spire.Presentation, please refer Spire.Presentation Quick Start which can guide you quickly use Spire.Presentation.

How to Remove Page breaks in C#

2014-06-11 08:24:54 Written by Koohji

In Word document, users can add new page break or remove existing page breaks. This sample shows how to remove page breaks from the word document by using Spire.Doc. Spire.Doc supports to remove the page breaks from the word document from the format of .docx, .doc, and RTF etc.

Firstly make sure Spire.Doc for .NET has been installed correctly and then add Spire.Doc.dll as reference in the downloaded Bin folder though the below path: ".. \Spire.Doc\Bin\NET4.0\ Spire.Doc.dll". Here comes to the details of how to remove page breaks in C#.

//Create a new word document and load from the file.
Document document = new Document();
document.LoadFromFile("sample.docx");

// Traverse every paragraph of the first section of the document
for (int j = 0; j < document.Sections[0].Paragraphs.Count; j++)
  {
   Paragraph p = document.Sections[0].Paragraphs[j];

// Traverse every child object of a paragraph
for (int i = 0; i < p.ChildObjects.Count; i++)
    {
     DocumentObject obj = p.ChildObjects[i];

//Find the page break object
 if (obj.DocumentObjectType == DocumentObjectType.Break)
    {
     Break b = obj as Break;

// Remove the page break object from paragraph
p.ChildObjects.Remove(b);

//save the document to file.
document.SaveToFile("result.docx");

Please check the effective screenshot:

How to Remove Page breaks in C#

Full codes:

using Spire.Doc;
using Spire.Doc.Documents;
namespace RemovePageBreak
{
    class Program
    {
        static void Main(string[] args)
        {
            Document document = new Document();
            document.LoadFromFile("sample.docx", FileFormat.Docx);
            for (int j = 0; j < document.Sections[0].Paragraphs.Count; j++)
            {
                Paragraph p = document.Sections[0].Paragraphs[j];
                for (int i = 0; i < p.ChildObjects.Count; i++)
                {
                    DocumentObject obj = p.ChildObjects[i];
                    if (obj.DocumentObjectType == DocumentObjectType.Break)
                    {
                        Break b = obj as Break;
                        p.ChildObjects.Remove(b);
                    }
                }
            }
            document.SaveToFile("result.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("result.docx");
        }
    }
}

Edit Table in PowerPoint document

2014-06-10 03:57:22 Written by Koohji

Spire.Presentation is a powerful and easy-to-use .NET component, especially designed for developers. Using Spire.Presentation you can generate, modify, convert, render, and print documents without installing Microsoft PowerPoint on your machine. There is a document in our website introducing you how to insert table. And in this document, you will be introduced how to edit a table within a PPT document.

Step 1: Create a Presentation instance and load the file.

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

Step 2: Store the data used in replacement in string [].

string[] str = new string[] { "Germany", "Berlin", "Europe", "0152458", "20860000" };

Step 3: Get the table within the PPT document.

ITable table = null;
foreach (IShape shape in presentation.Slides[0].Shapes)
{
    if (shape is ITable)
    {
        table = (ITable) shape;
    }
}

Step 4: Fill in the third row with new data and set the HighlightColor.

for (int i = 0; i < table.ColumnsList.Count;i++ )
{
    //replace the data in cell
    table[i, 2].TextFrame.Text = str[i];

    //set the highlightcolor
    table[i, 2].TextFrame.TextRange.HighlightColor.Color = Color.BlueViolet;
}

Step 5: Set the style of the table.

table.StylePreset = TableStylePreset.LightStyle1Accent2;

Step 6: Save the document.

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

Download and install Spire.Presentation for .NET and refer to below code to edit table within PPT document.

Screenshots and full code:

Before:

Edit Table in PowerPoint document

After:

Edit Table in PowerPoint document

[C#]
using Spire.Presentation;
using System.Drawing;

namespace EditTable
{

    class Program
    {

        static void Main(string[] args)
        {
            //create a PPT document
            Presentation presentation = new Presentation();

            presentation.LoadFromFile("table.pptx");

            //the data used in replacement
            string[] str = new string[] { "Germany", "Berlin", "Europe", "0152458", "20860000" };

            ITable table = null;

            //get the table in PPT document
            foreach (IShape shape in presentation.Slides[0].Shapes)
            {
                if (shape is ITable)
                {
                    table = (ITable)shape;

                    //change the style of table
                    table.StylePreset = TableStylePreset.LightStyle1Accent2;

                    for (int i = 0; i < table.ColumnsList.Count; i++)
                    {
                        //replace the data in cell
                        table[i, 2].TextFrame.Text = str[i];

                        //set the highlightcolor
                        table[i, 2].TextFrame.TextRange.HighlightColor.Color = Color.BlueViolet;
                    }
                }
            }

            //save the document
            presentation.SaveToFile("result.pptx", FileFormat.Pptx2010);
            System.Diagnostics.Process.Start("result.pptx");

        }

    }
}
[VB.NET]
Imports Spire.Presentation
Imports System.Drawing

Namespace EditTbale

	Class Program

		Private Shared Sub Main(args As String())
			'create a PPT document
			Dim presentation As New Presentation()

			presentation.LoadFromFile("table.pptx")

			'the data used in replacement
			Dim str As String() = New String() {"Germany", "Berlin", "Europe", "0152458", "20860000"}

			Dim table As ITable = Nothing

			'get the table in PPT document
			For Each shape As IShape In presentation.Slides(0).Shapes
				If TypeOf shape Is ITable Then
					table = DirectCast(shape, ITable)

					'change the style of table
					table.StylePreset = TableStylePreset.LightStyle1Accent2

					For i As Integer = 0 To table.ColumnsList.Count - 1
						'replace the data in cell
						table(i, 2).TextFrame.Text = str(i)

						'set the highlightcolor
						table(i, 2).TextFrame.TextRange.HighlightColor.Color = Color.BlueViolet
					Next
				End If
			Next

			'save the document
			presentation.SaveToFile("result.pptx", FileFormat.Pptx2010)
			System.Diagnostics.Process.Start("result.pptx")

		End Sub

	End Class
End Namespace

If you couldn't successfully use Spire.Presentation, please refer Spire.Presentation Quick Start which can guide you quickly use Spire.Presentation.

page 260