Spire.Doc
Friday, 31 October 2014 06:13

Convert Word to Image in WPF

Written by support iceblue

In some circumstances, we may need to convert or save Word documents as pictures. For one reason, a picture is difficult to edit; for another, compared with Word, pictures are much easier to be published for browsing. This article is aimed to explore how we can convert .doc/.docx to popular image formats such as Jpg, Png, Gif and Bmp in WPF using Spire.Doc.

Spire.Doc for WPF, as a professional Word component, provides a plenty of useful methods to manipulate Word documents in your WPF applications. Using Spire.Doc, developers are able to export Word documents as images with high quality. Here comes the method:

Step 1: Create a new project by choosing WPF Application in Visual Studio, add a button in MainWindow, double click the button to write code.

public partial class MainWindow : Window
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            
        }

Step 2: Initialize a new instance of Spire.Doc.Document class and load the sample Word file.

       Document doc = new Document("sample.docx", FileFormat.Docx2010);

Step 3: To convert Word to image in WPF, we need firstly save Word as BitmapSource by calling the method Document.SaveAsImage(ImageType type), then convert BitmapSource to Bitmap, then save the Bitmap as image with a specified format using Image.Save(). Here, I saved Bitmap as .Png.

       BitmapSource[] bss = doc.SaveToImages(ImageType.Bitmap);
            for (int i = 0; i < bss.Length; i++)
            {
                SourceToBitmap(bss[i]).Save(string.Format("img-{0}.png", i));
            }
        }

        private Bitmap SourceToBitmap(BitmapSource source)
        {        

            Bitmap bmp;
            using (MemoryStream ms = new MemoryStream())
            {
                PngBitmapEncoder encoder = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(source));
                encoder.Save(ms);
                bmp = new Bitmap(ms);
            }
            return bmp;
        }

Output of the first page:

Convert Word to Image in WPF

Entire Code:

using Spire.Doc;
using Spire.Doc.Documents;

namespace Word2Image
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Document doc = new Document("sample.docx", FileFormat.Docx2010);
            BitmapSource[] bss = doc.SaveToImages(ImageType.Bitmap);
            for (int i = 0; i < bss.Length; i++)
            {
                SourceToBitmap(bss[i]).Save(string.Format("img-{0}.png", i));
            }
        }

        private Bitmap SourceToBitmap(BitmapSource source)
        {        

            Bitmap bmp;
            using (MemoryStream ms = new MemoryStream())
            {
                PngBitmapEncoder encoder = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(source));
                encoder.Save(ms);
                bmp = new Bitmap(ms);
            }
            return bmp;
        }
    }
}

Every time we create a plain table in a Word document, we may want to change the style of the table so as to make it more vivid and attractive. In our previous article, we have demonstrated how to set Word table formatting with custom style. However, if we do not have much time to do so, we can apply built-in table styles to own a better appearance within a few minutes. This article focuses on how to apply built-in table styles to existing Word tables using Spire.Doc with C#, VB.NET.

In the class of Spire.Doc.Table, it provides Table.ApplyStyle() method which enable us to easily change the layout of tables with default styles. As is shown below, here is a Word document that contains two plain tables in the first page. Let us see how we can format the table style with several lines of code.

Test File:

Apply Built-In Table Styles to Existing Word Tables in C#, VB.NET

Code Snippet:

Step 1: Create a new Word document and load the test file.

Document doc = new Document("table.docx", FileFormat.Docx2010);

Step 2: Get the two tables from document.

Section section = doc.Sections[0];
Table table1 = section.Tables[0] as Table;
Table table2 = section.Tables[1] as Table;

Step 3: Apply tables with built-in table styles separately.

table1.ApplyStyle(DefaultTableStyle.MediumShading1Accent2);
table2.ApplyStyle(DefaultTableStyle.MediumShading2Accent1);

Step 4: Save the file.

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

Output:

Apply Built-In Table Styles to Existing Word Tables in C#, VB.NET

Entire Code:

[C#]
Document doc = new Document("table.docx", FileFormat.Docx2010);
Section section = doc.Sections[0];
Table table1 = section.Tables[0] as Table;
Table table2 = section.Tables[1] as Table;
           
table1.ApplyStyle(DefaultTableStyle.MediumShading1Accent2);
table2.ApplyStyle(DefaultTableStyle.MediumShading2Accent1);
doc.SaveToFile("result.docx", FileFormat.Docx);
[VB.NET]
Dim doc As New Document("table.docx", FileFormat.Docx2010)
Dim section As Section = doc.Sections(0)
Dim table1 As Table = TryCast(section.Tables(0), Table)
Dim table2 As Table = TryCast(section.Tables(1), Table)

table1.ApplyStyle(DefaultTableStyle.MediumShading1Accent2)
table2.ApplyStyle(DefaultTableStyle.MediumShading2Accent1)
doc.SaveToFile("result.docx", FileFormat.Docx)

Spire.Doc for .NET enables developers to add, modify and remove text and Image header for word documents easily. We have already shown you how to insert header for all the pages and only add header for the first page in word document in C#. This article will focus on demonstrate how to remove the header for all the pages in word document and only remove the header for the first page in C#.

In the example, we will load a word document with headers. And then we will show you how to remove the header only from the first page and all the pages independently.

Check the word document with headers:

How to remove header from the word document in C#

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

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

Step 2: Get the first section of document.

Section section = doc.Sections[0];

Step 3: Remove the header only from the first page of word document.

//This is necessary
section.PageSetup.DifferentFirstPageHeaderFooter = true;
section.HeadersFooters.FirstPageHeader.ChildObjects.Clear();

Step 4: Remove the header for all the pages.

section.HeadersFooters.Header.ChildObjects.Clear();

Step 5: Save the document to file.

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

Effective screenshot of the removal of the header:

How to remove header from the word document in C#

Full codes:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Doc;
using System.Drawing;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace RemoveHeader
{
    class Program
    {
        static void Main(string[] args)
        {
            Document doc = new Document();
            doc.LoadFromFile("Blues.docx");
            Section section = doc.Sections[0];
            //This is necessary
            section.PageSetup.DifferentFirstPageHeaderFooter = true;
            section.HeadersFooters.FirstPageHeader.ChildObjects.Clear();
            //section.HeadersFooters.Header.ChildObjects.Clear();
            doc.SaveToFile("output.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("output.docx");
        }
    }
}
Page 1 of 26