Spire.Doc supports to insert any type of file such as Excel, PDF, PowerPoint and etc, as OLE object into a Word document. In this article, you'll learn how to add a media file (audio or video) to a Word document using Spire.Doc in C#, VB.NET.

In the class of DocOleObject, a method named AppendOleObject(Stream oleStream, DocPicture olePicture, string fileExtension) is available for users to insert media file with the extension of mp3, mp4, avi or any other format into a Word document. The three parameters in this method represent:

  • oleStream: The OLE file stream.
  • olePicture: The image (icon) that is displayed in Word to show the OLE object.
  • fileExtension: The file extension.

Code Snippet:

Step 1: Initialize a new instance of Document class and add a new section.

Document doc = new Document();
Section section = doc.AddSection();

Step 2: Add a new paragraph, append some formatted text into the paragraph.

Paragraph para1 = section.AddParagraph();
para1.AppendText("Double click the PLAY button to view the video file");
ParagraphStyle style1 = new ParagraphStyle(doc);
style1.Name = "Style";
style1.CharacterFormat.FontName = "Calibri";
style1.CharacterFormat.FontSize = 15;
style1.CharacterFormat.Bold = true;
style1.CharacterFormat.TextColor = Color.Red;
doc.Styles.Add(style1);
para1.ApplyStyle(style1.Name);

Step 3: Add another paragraph, append a video file as OLE object into the paragraph.

Paragraph para2 = section.AddParagraph();
Stream s = File.OpenRead("media.mp4");
DocPicture pic = new DocPicture(doc);
pic.LoadImage(Image.FromFile("button.png"));
para2.AppendOleObject(s, pic, "mp4");

Step 4: Save the view the file.

doc.SaveToFile("Result.docx", FileFormat.Docx2010);
System.Diagnostics.Process.Start("Result.docx");

Output:

How to Embed Media File in Word in C#, VB.NET

Full Code:

[C#]
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;
using System.IO;
namespace EmbedMediaFile
{
    class Program
    {

        static void Main(string[] args)
        {
            //create a new Word document and insert section
            Document doc = new Document();
            Section section = doc.AddSection();
            //add a paragraph and append some text
            Paragraph para1 = section.AddParagraph();
            para1.AppendText("Double click the PLAY button to view the video file");
            ParagraphStyle style1 = new ParagraphStyle(doc);
            style1.Name = "Style";
            style1.CharacterFormat.FontName = "Calibri";
            style1.CharacterFormat.FontSize = 15;
            style1.CharacterFormat.Bold = true;
            style1.CharacterFormat.TextColor = Color.Red;
            doc.Styles.Add(style1);
            para1.ApplyStyle(style1.Name);
            //add another paragraph, append video file as OLE object in Word
            Paragraph para2 = section.AddParagraph();
            Stream s = File.OpenRead("media.mp4");
            DocPicture pic = new DocPicture(doc);
            pic.LoadImage(Image.FromFile("button.png"));
            para2.AppendOleObject(s, pic, "mp4");
            //save and view the file
            doc.SaveToFile("Result.docx", FileFormat.Docx2010);
            System.Diagnostics.Process.Start("Result.docx");

        }
    }
}
[VB.NET]
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Imports System.Drawing
Imports System.IO
Namespace EmbedMediaFile
	Class Program

		Private Shared Sub Main(args As String())
			'create a new Word document and insert section
			Dim doc As New Document()
			Dim section As Section = doc.AddSection()
			'add a paragraph and append some text
			Dim para1 As Paragraph = section.AddParagraph()
			para1.AppendText("Double click the PLAY button to view the video file")
			Dim style1 As New ParagraphStyle(doc)
			style1.Name = "Style"
			style1.CharacterFormat.FontName = "Calibri"
			style1.CharacterFormat.FontSize = 15
			style1.CharacterFormat.Bold = True
			style1.CharacterFormat.TextColor = Color.Red
			doc.Styles.Add(style1)
			para1.ApplyStyle(style1.Name)
			'add another paragraph, append video file as OLE object in Word
			Dim para2 As Paragraph = section.AddParagraph()
			Dim s As Stream = File.OpenRead("media.mp4")
			Dim pic As New DocPicture(doc)
			pic.LoadImage(Image.FromFile("button.png"))
			para2.AppendOleObject(s, pic, "mp4")
			'save and view the file
			doc.SaveToFile("Result.docx", FileFormat.Docx2010)
			System.Diagnostics.Process.Start("Result.docx")

		End Sub
	End Class
End Namespace

The Mark as Final command makes a presentation read-only and prevents changes to the document. When you share a presentation that is marked as final, you're telling viewers the presentation is final and no changes on it are wanted. In this article, I'll make a brief introduction about how to mark a presentation as final using Spire.Presentation in C#, VB.NET.

In Spire.Presentation, the presentation class contains a property of DocumentProperty which enables developers to set document properties easily. Here in this case, you are able to mark a presentation as final by setting the Boolean value of MarkAsFinal as true.

Code Snippet:

Step 1: Initialize a new instance of presentation class and load the sample file.

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

Step 2: Set the value of MarkAsFinal property as true.

ppt.DocumentProperty["_MarkAsFinal"] =true;

Step 3: Save and launch the file.

ppt.SaveToFile("result.pptx",FileFormat.Pptx2010);
System.Diagnostics.Process.Start("result.pptx");

Effect:

How to Mark a Presentation as Final using Spire.Presentation in C#, VB.NET

Full Code:

[C#]
using Spire.Presentation;
namespace MarkPPT
{
    class Program
    {
        static void Main(string[] args)
        {
            Presentation ppt = new Presentation();
            ppt.LoadFromFile("sample.pptx", FileFormat.Pptx2010);

            ppt.DocumentProperty["_MarkAsFinal"] = true;

            ppt.SaveToFile("result.pptx", FileFormat.Pptx2010);
            System.Diagnostics.Process.Start("result.pptx");
            System.Diagnostics.Process.Start("result.pptx");

            }
        }
    }
[VB.NET]
Imports Spire.Presentation
Namespace MarkPPT
	Class Program
		Private Shared Sub Main(args As String())
			Dim ppt As New Presentation()
			ppt.LoadFromFile("sample.pptx", FileFormat.Pptx2010)

			ppt.DocumentProperty("_MarkAsFinal") = True

			ppt.SaveToFile("result.pptx", FileFormat.Pptx2010)
			System.Diagnostics.Process.Start("result.pptx")
			System.Diagnostics.Process.Start("result.pptx")

		End Sub
	End Class
End Namespace

C#: Insert Paragraphs in Word

2024-11-14 08:01:00 Written by Koohji

Inserting paragraphs in Word is a fundamental skill for creating well-structured and organized documents. Paragraphs can break down large blocks of text, making it easier for readers to follow the flow of ideas and find specific information. In Word, you can add new paragraphs to represent new ideas or add additional information. This article will demonstrate how to insert a new paragraph in Word in C# using Spire.Doc for .NET.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Add a Paragraph at the End of a Word Document in C#

To add a new paragraph at the end, you need to get the last section of the Word document through the Document.LastSection property, and then add a paragraph at the end of the section through the Section.AddParagraph() method. The following are the detailed steps:

  • Create a Document instance.
  • Load a Word document using Document.LoadFromFile() method.
  • Get the last section of the document using Document.LastSection property.
  • Add a paragraph at the end of the section using Section.AddParagraph() method, and then add text to it using Paragraph.AppendText() method.
  • Create a ParagraphStyle object and set the font name, size, style of the paragraph text.
  • Apply the paragraph style using Paragraph.ApplyStyle() method
  • Save the result document using Document.SaveToFile() method.
  • C#
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;

namespace AddParagraph
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document object
            Document doc = new Document();

            //Load a Word document
            doc.LoadFromFile("Test.docx");

            //Get the last section
            Section section = doc.LastSection;

            //Add a paragraph at the end and set its text content
            Paragraph para = section.AddParagraph();
            para.AppendText("Add a paragraph to the end of the document.");

            //Set the paragraph style
            ParagraphStyle style = new ParagraphStyle(doc);
            style.Name = "Style1";
            style.CharacterFormat.FontName = "Times New Roman";
            style.CharacterFormat.FontSize = 12;
            style.CharacterFormat.TextColor = Color.Blue;
            style.CharacterFormat.Bold = true;
            doc.Styles.Add(style);
            para.ApplyStyle("Style1");
            para.Format.BeforeSpacing = 10;

            //Save the result file
            doc.SaveToFile("AddParagraph.docx", FileFormat.Docx2016);
        }
    }
}

An output file that adds a new paragraph with text in Word using C#

Insert a Paragraph at a Specified Location in Word in C#

You can also add a paragraph and then insert it to a specified position through the Section.Paragraphs.Insert(int index, IParagraph paragraph) method. The following are the detailed steps:

  • Create a Document instance.
  • Load a Word document using Document.LoadFromFile() method.
  • Get a specified section using Document.Sections[] property.
  • Add a paragraph using Section.AddParagraph() method, and then add text to it using Paragraph.AppendText() method.
  • Set the font name, size, style of the paragraph text.
  • Insert the newly added paragraph at a specified index using Section.Paragraphs.Insert(int index, IParagraph paragraph) method.
  • Save the result document using Document.SaveToFile() method.
  • C#
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;

namespace InsertParagraph
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document object
            Document doc = new Document();

            //Load a Word document
            doc.LoadFromFile("Test.docx");
        

            //Get the first section
            Section section = doc.Sections[0];

            //Add a paragraph and set its text content
            Paragraph para = section.AddParagraph();
            TextRange textRange = para.AppendText("Insert a paragraph at a specified location in the Word document.");

            //Set the font name, size, color and style
            textRange.CharacterFormat.TextColor = Color.Blue;
            textRange.CharacterFormat.FontName = "Times New Roman";
            textRange.CharacterFormat.FontSize = 14;
            textRange.CharacterFormat.UnderlineStyle = UnderlineStyle.Single;

            //Insert the paragraph as the third paragraph
            section.Paragraphs.Insert(2, para);

            //Set spacing after the paragraph
            para.Format.AfterSpacing = 10;

            //Save the result file
            doc.SaveToFile("InsertParagraph.docx", FileFormat.Docx2016);
        }
    }
}

An output file that inserts a new paragraph as the third paragraph in Word with C#

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

page 238