This example demonstrates how to images to a document.
Image are designed to visually represent concepts in a synthetic. It make clearer way.
Providing visual context ensures a greater readability and comprehension of documentation
We have versions of this representation in words ,using dropdown you can make a choice within the following
- Word Doc
- Word Docx
- Word ML
- RTF
- MS Text format
- E-book format
- DLS based xml file format
- HTML
After clicking a generate button will launch a file with above choosen version of word with demo sample to use images in documents.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using OfficeComponent.Word;
namespace OfficeComponent.Samples
{
class ImageExample : WordExampleBase
{
public ImageExample(string commonDataPath, string outputDir)
: base(commonDataPath, outputDir)
{
}
public ImageExample(string commonDataPath, string outputDir, string xmlFile) : base(commonDataPath, outputDir, xmlFile)
{
}
public override string Execute()
{
// Create a new instance of PdfDocument class.
WordDocument document = new WordDocument();
Section section = document.AddSection();
Paragraph para = section.AddParagraph();
para.AppendText("Images");
para.ApplyStyle(StyleIdentifier.Title);
para = section.AddParagraph();
para.AppendText("GIF Image");
para.ApplyStyle(StyleIdentifier.Heading2);
para.AppendPicture(System.Drawing.Image.FromFile(CommonDataPath + "\\Logo.gif"));
para = section.AddParagraph();
para.AppendText("TIF Image");
para.ApplyStyle(StyleIdentifier.Heading2);
para.AppendPicture(System.Drawing.Image.FromFile(CommonDataPath + "\\256.tif"));
para = section.AddParagraph();
para.AppendText("EMF Image");
para.ApplyStyle(StyleIdentifier.Heading2);
para.AppendPicture(System.Drawing.Image.FromFile(CommonDataPath + "\\Mapping2.emf"));
para = section.AddParagraph();
para.AppendText("JPG Image");
para.ApplyStyle(StyleIdentifier.Heading2);
para.AppendPicture(System.Drawing.Image.FromFile(CommonDataPath + "\\Spring.jpg"));
string fileName = Path.Combine(OutputDir, this.GetType().Name + "_" + Guid.NewGuid().ToString() + GetExtension(SaveAsFormat));
// Save the document.
document.Save(fileName, SaveAsFormat);
// Close the document.
document.Close();
return fileName;
}
}
}
Imports System.Drawing.Imaging
Imports System.IO
Imports System.Text
Imports OfficeComponent.Word
Namespace OfficeComponent.Samples
Friend Class ImageExample
Inherits WordExampleBase
Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String)
MyBase.New(commonDataPath, outputDir)
End Sub
Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String, ByVal xmlFile As String)
MyBase.New(commonDataPath, outputDir, xmlFile)
End Sub
Public Overrides Function Execute() As String
' Create a new instance of PdfDocument class.
Dim document As New WordDocument()
Dim section As Section = document.AddSection()
Dim para As Paragraph = section.AddParagraph()
para.AppendText("Images")
para.ApplyStyle(StyleIdentifier.Title)
para = section.AddParagraph()
para.AppendText("GIF Image")
para.ApplyStyle(StyleIdentifier.Heading2)
para.AppendPicture(System.Drawing.Image.FromFile(CommonDataPath & "\Logo.gif"))
para = section.AddParagraph()
para.AppendText("TIF Image")
para.ApplyStyle(StyleIdentifier.Heading2)
para.AppendPicture(System.Drawing.Image.FromFile(CommonDataPath & "\256.tif"))
para = section.AddParagraph()
para.AppendText("EMF Image")
para.ApplyStyle(StyleIdentifier.Heading2)
para.AppendPicture(System.Drawing.Image.FromFile(CommonDataPath & "\Mapping2.emf"))
para = section.AddParagraph()
para.AppendText("JPG Image")
para.ApplyStyle(StyleIdentifier.Heading2)
para.AppendPicture(System.Drawing.Image.FromFile(CommonDataPath & "\Spring.jpg"))
Dim fileName As String = Path.Combine(OutputDir, Me.GetType().Name & "_" & Guid.NewGuid().ToString() & GetExtension(SaveAsFormat))
' Save the document.
document.Save(fileName, SaveAsFormat)
' Close the document.
document.Close()
Return fileName
End Function
End Class
End Namespace