.NET Word Header-Insert Word Header with Image in C# and Visual Basic

Word header, put on the top of one page, is often used to present additional information of one document. The header contents can be document title, a brief summary, company name, author name, page numbers etc. At the beginning, the header is just presented as text. Gradually, the contents are more and more colorful so that header can be used to decorate a document, not only show information, for example special shapes, images etc. In this post, I will introduce a solution to insert Word image header in C# and Visual Basic.

Note: In this example, I use a .NET Word library, Spire.Doc for .NET so that I have added its dll as reference in my project.

Firstly, get default Header property of HeaderFooter class and assign it as value for the new declared HeaderFooter instance. Invoke header.AddParagraph method to add paragraph in header and then set Format property (HorizontalAlignment) for this paragraph.

Secondly, invoke Paragraph.AppendPicture method to add an image in this header paragraph and assign the result as value for DocPicture instance. Then, set Height, Width and TextWrappingStyle properties of this instance.

Thirdly, it is a little dull to have only one image in header. So add some text for header paragraph by invoke Paragrap.AppendText method. And assign the method result as value for a TextRange instance. Set CharacterFormat properties for this instance, including FontName, FontSize and TextColor.

Follow the code blow to realize the function to insert Word image header.

C#

using System.Drawing;

using Spire.Doc;

using Spire.Doc.Documents;

using Spire.Doc.Fields;

 

namespace ImageHeader

{

    class Program

    {

        static void Main(string[] args)

        {

            //Load Document

            Document doc = new Document();

            doc.LoadFromFile(@”E:\Work\Documents\WordDocuments\.NET Framework.docx”);

 

            //Header Paragraph

            HeaderFooter header = doc.Sections[0].HeadersFooters.Header;

            Paragraph para = header.AddParagraph();

            para.Format.HorizontalAlignment = HorizontalAlignment.Right;

 

            //Header Image

            DocPicture pic = para.AppendPicture(Image.FromFile(@”E:\Work\Documents\SampleImage\Microsoft.jpg”));

            pic.Height = 22;

            pic.Width = 30;

            pic.TextWrappingStyle = TextWrappingStyle.Inline;

 

            //Header Text

            TextRange tr=para.AppendText(“Microsoft Technology”);

            tr.CharacterFormat.FontName = “Impact”;

            tr.CharacterFormat.FontSize = 12;

            tr.CharacterFormat.TextColor = Color.DarkBlue;

 

            //Save and Launch

            doc.SaveToFile(“ImageHeader.docx”, FileFormat.Docx);

            System.Diagnostics.Process.Start(“ImageHeader.docx”);

        }

    }

}

 

Visual Basic

Imports System.Drawing

Imports Spire.Doc

Imports Spire.Doc.Documents

Imports Spire.Doc.Fields

 

Namespace ImageHeader

    Friend Class Program

        Shared Sub Main(ByVal args() As String)

            ‘Load Document

            Dim doc As New Document()

            doc.LoadFromFile(“E:\Work\Documents\WordDocuments\.NET Framework.docx”)

 

            ‘Header Paragraph

            Dim header As HeaderFooter = doc.Sections(0).HeadersFooters.Header

            Dim para As Paragraph = header.AddParagraph()

            para.Format.HorizontalAlignment = HorizontalAlignment.Right

 

            ‘Header Image

            Dim pic As DocPicture = para.AppendPicture(Image.FromFile(“E:\Work\Documents\SampleImage\Microsoft.jpg”))

            pic.Height = 22

            pic.Width = 30

            pic.TextWrappingStyle = TextWrappingStyle.Inline

 

            ‘Header Text

            Dim tr As TextRange = para.AppendText(“Microsoft Technology”)

            tr.CharacterFormat.FontName = “Impact”

            tr.CharacterFormat.FontSize = 12

            tr.CharacterFormat.TextColor = Color.DarkBlue

 

            ‘Save and Launch

            doc.SaveToFile(“ImageHeader.docx”, FileFormat.Docx)

            System.Diagnostics.Process.Start(“ImageHeader.docx”)

        End Sub

    End Class

End Namespace

Debug this program and then I get the following result:

Download Spire.Doc for .NET Here

The .NET Office component, Spire.Office for .NET can be used to realize this function as well.

Related Post: Insert Header in Word Document – C#/VB.NET

Convenient Solution to Convert RTF to Image (.jpg) in C# and Visual Basic

Document conversion is always the hot topic among developers and there are lots of powerful and easy-to-use converters released. Why is document conversion so popular? It is very helpful for users to read one document in other applications if there is no viewer of original document format installed on users’ system. In this post, I will introduce a solution about document conversion: RTF to Image in C# and Visual Basic.

RTF (Rich Text Format) describe contents with plain text and can save variety of information with different formats. It can be created by many applications, such as MS Word, Wordpad etc. The feature of RTF is compatibility so that it can be a wonderful document converter. Image, great tool to decorate contents, has many formats, including .jpg, .png, .gif, .bmp etc. In this example, the RTF will be converted to .jpg file.

This solution is based on a .NET Word component, Spire.Doc for .NET. So I have installed it on my system and added its dll as reference in my project.

Firstly, load the RTF document through invoking Document.LoadFromFile method. The overloads passed are fileName string and FileFormat.

Secondly, convert RTF to image (.jpg format). Invoke Document.SaveToImages method to save specified page of RTF file to image. The overloads to this method are pageIndex and ImageType. Because I want to save the image as .jpg format, so the ImageType is set as Bitmap. The other image types provided are shown as the following screenshot.

Assign the method result as value for Image instance and then invoke Image.Save method to save this converted image. The overloads passed are string fileName and ImageFormat. Select ImageFormat as Jpeg.

CODE:

C#

using System.Drawing;

using System.Drawing.Imaging;

using Spire.Doc;

using Spire.Doc.Documents;

 

namespace RTF2Image

{

    class Program

    {

        static void Main(string[] args)

        {

            //Load RTF Document

            Document document = new Document();

            document.LoadFromFile(@”E:\Work\Documents\WordDocuments\Blues Introduction.rtf”, FileFormat.Rtf);

 

            //Save to Image

            Image image = document.SaveToImages(0, ImageType.Bitmap);

            image.Save(“RTF2Image.jpg”, ImageFormat.Jpeg);

            System.Diagnostics.Process.Start(“RTF2Image.jpg”);

        }

    }

}

 

Visual Basic

Imports System.Drawing

Imports System.Drawing.Imaging

Imports Spire.Doc

Imports Spire.Doc.Documents

 

Namespace RTF2Image

    Friend Class Program

        Shared Sub Main(ByVal args() As String)

            ‘Load RTF Document

            Dim document As New Document()

            document.LoadFromFile(“E:\Work\Documents\WordDocuments\Blues Introduction.rtf”, FileFormat.Rtf)

 

            ‘Save to Image

            Dim image As Image = document.SaveToImages(0, ImageType.Bitmap)

            image.Save(“RTF2Image.jpg”, ImageFormat.Jpeg)

            System.Diagnostics.Process.Start(“RTF2Image.jpg”)

        End Sub

    End Class

End Namespace

After debugging, we can the converted image from RTF below:

Download Spire.Doc for .NET Here

P.S. The .NET Office component, Spire.Office for .NET can be used to realize this function as well.

.NET Perform Word-Set Number List for Word Paragraphs in C# and Visual Basic

Now, please open any one instruction besides you and follow it step by step. OK, you find that there is a number or mark on front of every step to form a list. With the list, you feel that everything is presented very clearly and understand how to use on instrument quickly. Actually, when we writing to discuss one topic or reviews about some events, we often need to show some points with list format to enables readers to get key points quickly. This post focuses on introducing solution to set Word number list for paragraphs in C# and Visual Basic. And the screenshot blow shows result after formatting number list.

This solution is based on a .NET Word component, Spire.Doc for .NET so that we need to download and install this component on our system at the beginning. Then, set number list for specified paragraphs by following the steps.

Firstly, load the Word document we have prepared. Secondly, invoke Paragraph.ListFormat.ApplyNumberedStyle() method in a for loop to set number list format for paragraph 5 to 8. In this loop, we also can set character format and number position for current list level to make the list shown more obviously.

CODE:

C#

using Spire.Doc;

using Spire.Doc.Documents;

using System.Drawing;

 

namespace WordBullets

{

    class Program

    {

        static void Main(string[] args)

        {

            //Load Document

            Document doc = new Document();

            doc.LoadFromFile(@”E:\Work\Documents\WordDocuments\Microsoft Word 2013 Preview.docx”);

 

            //Set Bullet Style

            Section s = doc.Sections[0];

 

            for (int i = 5; i< 8; i++)

            {

                Paragraph p = s.Paragraphs[i];

                p.ListFormat.ApplyNumberedStyle();

                p.ListFormat.CurrentListLevel.NumberPosition = -12;

                p.ListFormat.CurrentListLevel.CharacterFormat.FontName = “Calibri”;

                p.ListFormat.CurrentListLevel.CharacterFormat.FontSize = 12;

                p.ListFormat.CurrentListLevel.CharacterFormat.Bold = true;

                p.ListFormat.CurrentListLevel.CharacterFormat.TextColor = Color.Purple;

            }

 

            //Save and Launch

            doc.SaveToFile(“Bullets.doc”, FileFormat.Doc);

            System.Diagnostics.Process.Start(“Bullets.doc”);

        }

    }

}

 

Visual Basic

Imports Spire.Doc

Imports Spire.Doc.Documents

Imports System.Drawing

 

Namespace WordBullets

    Friend Class Program

        Shared Sub Main(ByVal args() As String)

            ‘Load Document

            Dim doc As New Document()

            doc.LoadFromFile(“E:\Work\Documents\WordDocuments\Microsoft Word 2013 Preview.docx”)

 

            ‘Set Bullet Style

            Dim s As Section = doc.Sections(0)

 

            For i As Integer = 5 To 7

                Dim p As Paragraph = s.Paragraphs(i)

                p.ListFormat.ApplyNumberedStyle()

                p.ListFormat.CurrentListLevel.NumberPosition = -12

                p.ListFormat.CurrentListLevel.CharacterFormat.FontName = “Calibri”

                p.ListFormat.CurrentListLevel.CharacterFormat.FontSize = 12

                p.ListFormat.CurrentListLevel.CharacterFormat.Bold = True

                p.ListFormat.CurrentListLevel.CharacterFormat.TextColor = Color.Purple

            Next i

 

            ‘Save and Launch

            doc.SaveToFile(“Bullets.doc”, FileFormat.Doc)

            System.Diagnostics.Process.Start(“Bullets.doc”)

        End Sub

    End Class

End Namespace

 

Download Spire.Doc for .NET Here

P.S. The .NET Office component, Spire.Office for .NET can be used to realize this function as well.

.NET Edit Word-Remove Word Paragraphs with C#, VB

Do you remember how miserable you write paper with an unfamiliar topic? At first, you write and then delete what you write and then rewrite what you deleted one second ago. Actually, no matter what we write, we will modify to make it more attractive after completing. When editing, some existing paragraphs will be removed and some new will be added. In this post, I will show a method about how to remove paragraphs in Word document with C#, VB .NET.

At beginning, we need some materials: word template with several paragraphs and a .NET Word component, Spire.Doc for .NET to realize this function very easily and quickly. (P.S The following screenshot presents part of Word template contents)

Next, follow steps to use the tool to delete one or more paragraphs in this Word template.

  1. Load this Word template. 
  2. Get paragraphs in specified section of document. Use ParagraphCollection.RemoveAt method provided by Spire.Doc for .NET and pass paragraph index overload to this method to remove specified paragraphs.
  3. Save the edited document with a new name.

Ensure that Spire.Doc has been downloaded and installed on your system and the dll has been added as reference in your project. Then, you can follow the code blow to realize the function to delete Word paragraphs.

CODE:

C#

using Spire.Doc;

using Spire.Doc.Documents;

 

namespace InsertWatermark

{

    class watermark

    {

        static void Main(string[] args)

        {

            //Load File

            Document document = new Document();

            document.LoadFromFile(@”E:\Work\Documents\WordDocuments\Healthcare Questionnaire.docx”);

 

            //Remove Paragraph 2

            document.Sections[0].Paragraphs.RemoveAt(1);

 

            //Remove Paragraph 3

            document.Sections[0].Paragraphs.RemoveAt(2);

 

            //Save and Launch

            document.SaveToFile(“RemovePara.docx”, FileFormat.Docx);

            System.Diagnostics.Process.Start(“RemovePara.docx”);

        }

    }

}

 

Visual Basic

Imports Spire.Doc

Imports Spire.Doc.Documents

 

Namespace InsertWatermark

    Friend Class watermark

        Shared Sub Main(ByVal args() As String)

            ‘Load File

            Dim document As New Document()

            document.LoadFromFile(“E:\Work\Documents\WordDocuments\Healthcare Questionnaire.docx”)

 

            ‘Remove Paragraph 2

            document.Sections(0).Paragraphs.RemoveAt(1)

 

            ‘Remove Paragraph 3

            document.Sections(0).Paragraphs.RemoveAt(2)

 

            ‘Save and Launch

            document.SaveToFile(“RemovePara.docx”, FileFormat.Docx)

            System.Diagnostics.Process.Start(“RemovePara.docx”)

        End Sub

    End Class

End Namespace

After running, we can find that paragraph 2 and 3 has been removed as screenshot shown blow.

Download Spire.Doc for .NET Here

Download Word Template in Post Here