page 213

A table in Microsoft Word can contain a variety of elements, including text, images, and many more. Sometimes, you may want to insert images into a table to present some information or extract images from a table for use in other documents. This article will teach you how to insert or extract images from tables in Word documents in C# and VB.NET 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

Insert Images into a Table in a Word Document in C# and VB.NET

Spire.Doc provides the TableCell.Paragraphs[int].AppendPicture() method which enables you to add an image to a specific table cell. The detailed steps are as follows:

  • Initialize an instance of the Document class.
  • Load a Word document using Document.LoadFromFile() method.
  • Get a specific section in the document by its index through Document.Sections[int] property.
  • Get a specific table in the section by its index through Section.Tables[int] property.
  • Access a specific cell to which you want to add an image through Table.Row[int].Cells[int] property.
  • Add an image to a specific paragraph of the cell using TableCell.Paragraphs[int].AppendPicture() method.
  • Set image width and height through DocPicture.Width and DocPicture.Height properties.
  • Save the result document using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Fields;
using System.Drawing;

namespace InsertImagesIntoTable
{
    class Program
    {
        static void Main(string[] args)
        {
            //Initialize an instance of the Document class
            Document doc = new Document();
            //Load a Word document
            doc.LoadFromFile("Table.docx");

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

            //Get the first table from the first section
            Table table = (Table)section.Tables[0];

            //Add an image to the 3rd cell of the second row in the table
            TableCell cell = table.Rows[1].Cells[2];
            DocPicture picture = cell.Paragraphs[0].AppendPicture(Image.FromFile("doc.png"));
            //Set image width and height
            picture.Width = 100;
            picture.Height = 100;
            //Add an image to the 3rd cell of the 3rd row in the table
            cell = table.Rows[2].Cells[2];
            picture = cell.Paragraphs[0].AppendPicture(Image.FromFile("xls.png"));
            //Set image width and height
            picture.Width = 100;
            picture.Height = 100;

            //Save the result document
            doc.SaveToFile("AddImageToTable.docx", FileFormat.Docx2013);
        }
    }
}

C#/VB.NET: Insert or Extract Images from Tables in Word

Extract Images from a Table in a Word Document in C# and VB.NET

To extract images from a table, you need to iterate through all rows in the tale, all cells in each row, all paragraphs in each cell and all child objects in each paragraph, then find the objects that are of DocPicture type and call DocPicture.Image.Save() method to save them to a specified file path. The detailed steps are as follows:

  • Initialize an instance of the Document class.
  • Load a Word document using Document.LoadFromFile() method.
  • Get a specific section in the document by its index through Document.Sections[int] property.
  • Get a specific table in the section by its index through Section.Tables[int] property.
  • Iterate through all rows in the table.
  • Iterate through all cells in each row.
  • Iterate through all paragraphs in each cell.
  • Iterate through all child objects in each paragraph.
  • Check if the current child object is of DocPicture type.
  • If the result is true, typecast the object as DocPicture and call DocPicture.Image.Save() method to save the image to a specified file path.
  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System;
using System.Drawing.Imaging;

namespace ExtractImagesFromTable
{
    class Program
    {
        static void Main(string[] args)
        {
            //Initialize an instance of the Document class
            Document doc = new Document();
            //Load a Word document
            doc.LoadFromFile("AddImageToTable.docx");

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

            //Get the first table from the first section
            Table table = (Table)section.Tables[0];

            int index = 0;
            string imageName = null;

            //Iterate through all rows in the table
            for (int i = 0; i < table.Rows.Count; i++)
            {
                //Iterate through all cells in each row
                for (int j = 0; j < table.Rows[i].Cells.Count; j++)
                {
                    //Iterate through all paragraphs in each cell
                    foreach (Paragraph para in table[i, j].Paragraphs)
                    {
                        //Iterate through all child objects in each paragraph
                        foreach (DocumentObject obj in para.ChildObjects)
                        {
                            //Check if the current child object is of DocPicture type
                            if (obj is DocPicture)
                            {
                                //Save the images to a specified file path
                                imageName = string.Format(@"images\TableImage-{0}.png", index);
                                (obj as DocPicture).Image.Save(imageName, ImageFormat.Png);
                                index++;
                            }
                        }
                    }
                }
            }
        }
    }
}

C#/VB.NET: Insert or Extract Images from Tables in Word

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 transitions give visual sparkle by creating a "PowerPoint-like" effect when switching between pages. Page transitions are useful when you create a slideshow in PDF format. This article will introduce how to add page transition effects to an existing PDF document using Spire.PDF.

Code Snippet:

Step 1: Initialize an object of PdfDocument class and load the sample PDF which you want to add transition effects to.

PdfDocument doc = new PdfDocument();
doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");

Step 2: Create a new PDF document, add sections to it based on the page count of source file and transfer every individual page to a section. Spire.PDF provides PageSettings property in PdfSection class for setting transition effects, that's reason why we should add pages to sections within a new PDF document.

PdfDocument newDoc = new PdfDocument();
for (int i = 0; i < doc.Pages.Count; i++)
{
    PdfSection secetion = newDoc.Sections.Add();
    PdfPageBase page = doc.Pages[i];
    PdfTemplate template = page.CreateTemplate();
    PdfNewPage newpage = secetion.Pages.Add();
    newpage.Canvas.DrawTemplate(template, new PointF(0 - doc.PageSettings.Margins.Left, 0- doc.PageSettings.Margins.Top));
}

Step 3: As demonstrated above, we know that each section in the new document contains a page. Then we could set transition effect for each page through PageSettings.Transition.

PdfSection secetion1 = newDoc.Sections[0];
secetion1.PageSettings.Transition.Style = PdfTransitionStyle.Cover;
secetion1.PageSettings.Transition.Duration = 3;
secetion1.PageSettings.Transition.PageDuration = 2;

PdfSection secetion2 = newDoc.Sections[1];
secetion2.PageSettings.Transition.Style = PdfTransitionStyle.Glitter;
secetion2.PageSettings.Transition.Duration = 3;
secetion2.PageSettings.Transition.PageDuration = 2;

PdfSection secetion3 = newDoc.Sections[2];
secetion3.PageSettings.Transition.Style = PdfTransitionStyle.Fade;
secetion3.PageSettings.Transition.Duration = 3;
secetion3.PageSettings.Transition.PageDuration = 2;

Step 4: Save and launch the file.

newDoc.SaveToFile("Transition.pdf", FileFormat.PDF);
System.Diagnostics.Process.Start("Transition.pdf");

Output:

How to set page transitions for existing PDF document in C#, VB.NET

[C#]
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;


namespace SetPageTransitions
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");

            PdfDocument newDoc = new PdfDocument();
            for (int i = 0; i < doc.Pages.Count; i++)
            {
                PdfSection secetion = newDoc.Sections.Add();
                PdfPageBase page = doc.Pages[i];
                PdfTemplate template = page.CreateTemplate();
                PdfNewPage newpage = secetion.Pages.Add();
                newpage.Canvas.DrawTemplate(template, new PointF(0 - doc.PageSettings.Margins.Left, 0 - doc.PageSettings.Margins.Top));
            }

            PdfSection secetion1 = newDoc.Sections[0];
            secetion1.PageSettings.Transition.Style = PdfTransitionStyle.Cover;
            secetion1.PageSettings.Transition.Duration = 3;
            secetion1.PageSettings.Transition.PageDuration = 2;

            PdfSection secetion2 = newDoc.Sections[1];
            secetion2.PageSettings.Transition.Style = PdfTransitionStyle.Glitter;
            secetion2.PageSettings.Transition.Duration = 3;
            secetion2.PageSettings.Transition.PageDuration = 2;

            PdfSection secetion3 = newDoc.Sections[2];
            secetion3.PageSettings.Transition.Style = PdfTransitionStyle.Fade;
            secetion3.PageSettings.Transition.Duration = 3;
            secetion3.PageSettings.Transition.PageDuration = 2;

            newDoc.SaveToFile("Transition.pdf", FileFormat.PDF);
            System.Diagnostics.Process.Start("Transition.pdf");
        }
    }
}
[VB.NET]
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports System.Drawing


Namespace SetPageTransitions
	Class Program
		Private Shared Sub Main(args As String())
			Dim doc As New PdfDocument()
			doc.LoadFromFile("C:\Users\Administrator\Desktop\sample.pdf")

			Dim newDoc As New PdfDocument()
			For i As Integer = 0 To doc.Pages.Count - 1
				Dim secetion As PdfSection = newDoc.Sections.Add()
				Dim page As PdfPageBase = doc.Pages(i)
				Dim template As PdfTemplate = page.CreateTemplate()
				Dim newpage As PdfNewPage = secetion.Pages.Add()
				newpage.Canvas.DrawTemplate(template, New PointF(0 - doc.PageSettings.Margins.Left, 0 - doc.PageSettings.Margins.Top))
			Next

			Dim secetion1 As PdfSection = newDoc.Sections(0)
			secetion1.PageSettings.Transition.Style = PdfTransitionStyle.Cover
			secetion1.PageSettings.Transition.Duration = 3
			secetion1.PageSettings.Transition.PageDuration = 2

			Dim secetion2 As PdfSection = newDoc.Sections(1)
			secetion2.PageSettings.Transition.Style = PdfTransitionStyle.Glitter
			secetion2.PageSettings.Transition.Duration = 3
			secetion2.PageSettings.Transition.PageDuration = 2

			Dim secetion3 As PdfSection = newDoc.Sections(2)
			secetion3.PageSettings.Transition.Style = PdfTransitionStyle.Fade
			secetion3.PageSettings.Transition.Duration = 3
			secetion3.PageSettings.Transition.PageDuration = 2

			newDoc.SaveToFile("Transition.pdf", FileFormat.PDF)
			System.Diagnostics.Process.Start("Transition.pdf")
		End Sub
	End Class
End Namespace

How to hide word paragraph in C#

2016-06-08 05:46:24 Written by Koohji

When we operate the word document, we can hide some texts or the whole paragraph on the Microsoft Word after click the font box to hide the selected texts. Please check the screenshot of how Microsoft hide the text as below:

How to hide word paragraph in C#

Spire.Doc offers a CharacterFormat.Hidden property to enable developers to hide the specified text or the whole paragraph. This article will demonstrate how to hide the whole paragraph on the word document in C#.

Here comes to the code snippets:

Step 1: Create a new word document and load from file.

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

Step 2: Get the first section and the first paragraph from the word document.

Section sec = doc.Sections[0];
Paragraph para = sec.Paragraphs[0];

Step 3: Get the first TextRange and set CharacterFormat.Hidden property as true to hide the texts.

(para.ChildObjects[0] as TextRange).CharacterFormat.Hidden = true;

Step 4: Save the document to file.

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

Effective screenshot after the paragraph is hidden:

How to hide word paragraph in C#

After we set the options to display the hidden text, the hidden text will show like this:

How to hide word paragraph in C#

Full codes:

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace HideParagh
{
 class Program
    {
     
      static void Main(string[] args)
        {
            Document doc = new Document();
            doc.LoadFromFile("sample.docx");
            Section sec = doc.Sections[0];
            Paragraph para = sec.Paragraphs[0];

            (para.ChildObjects[0] as TextRange).CharacterFormat.Hidden = true;

            doc.SaveToFile("result.docx", FileFormat.Docx);
        }
    }
}
page 213