Monday, 15 October 2012 07:24

Convert RTF to HTML in WPF

This section will show you a detail solution to easily convert RTF to HTML in your WPF application via a .NET Word component. Only two lines of core code in total will be used to realize your RTF to HTML task in this solution.

Spire.Doc for WPF, as a professional MS Word component on WPF, enables you to accomplish RTF to HTML task through following two methods: Document.LoadFromFile(string fileName, FileFormat fileFormat) called to load your RTF file from system and Document. SaveToFile(string ilename, FileFormat fileFormat) is used to save the RTF file as HTML.

Now, you can download Spire.Doc for WPF and then, view the effect of RTF to HTML task as below picture:

RTF to HTML

Sample Code:

[C#]
using Spire.Doc;
using Spire.Doc.Documents;

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

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //Load RTF file
            Document document = new Document();
            document.LoadFromFile(@"..\wpfrtftohtml.rtf", FileFormat.Rtf);
            //Convert rtf to html
            document.SaveToFile("rtftohtml.html", FileFormat.Html);
        }
    }
}
[VB.NET]
Imports Spire.Doc
Imports Spire.Doc.Documents

Namespace wpfrtftohtml
    
    Public Class MainWindow
        Inherits Window
        Public Sub New()
            MyBase.New
            InitializeComponent
        End Sub
        
        Private Sub button1_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
            'Load RTF file
            Dim document As Document = New Document
            document.LoadFromFile("..\wpfrtftohtml.rtf", FileFormat.Rtf)
            'Convert rtf to html
            document.SaveToFile("rtftohtml.html", FileFormat.Html)
        End Sub
    End Class
End Namespace
Monday, 08 October 2012 05:33

Convert CSV to Excel in C#, VB.NET

This section will show you a very simple solution to quickly convert CSV to Excel(xls/xlsx) with C#, VB.NET. The whole CSV to Excel task can be realized by only three lines' of key code via a .NET Excel component.

Spire.XLS for .NET, as a professional .NET Excel component built from C#, VB.NET, enables you to mainly convert CSV to Excel by calling two methods: One method is Spire.Xls.Workbook.LoadFromFile(string fileName, string separator, int row, int column) which is used to load CSV file from system. The other is Workbook. SaveToFile(string fileName, ExcelVersion version) to save the CSV file just loaded as Excel. In the solution, Sheet1 is named to be "csv to excel" by setting the Property: Spire.Xls.Worksheet.Name. Besides, Excel background color is added in the target Excel file by Worksheet.Range[].Style.Color.

Before displaying the whole code, please download Spire.XLS for .NET and view the target Excel file as below picture:

CSV to Excel

[C#]
using Spire.Xls;

namespace csvtoxls
{
    class Program
    {
        static void Main(string[] args)
        {
         Workbook workbook = new Workbook();
         workbook.LoadFromFile(@"D:\michelle\my file\csv2xls.csv", ",", 1, 1);
         Worksheet sheet = workbook.Worksheets[0];
         sheet.Name = "csv to excel";
         //set the backgroundcolor=LightBlue of Range["A1:E1"]
         sheet.Range["A1:E1"].Style.Color = Color.Chocolate;
         //set the backgroundcolor=LightBlue of Range["A2:E6"]
         sheet.Range["A2:E6"].Style.Color = Color.SpringGreen;
         //set the backgroundcolor=Silver of Range["A7:E12"]
         sheet.Range["A7:E12"].Style.Color = Color.SkyBlue;
         //set the backgroundcolor=Silver of Range["A13:E19"]
         sheet.Range["A13:E19"].Style.Color = Color.Yellow;
         workbook.SaveToFile("result.xls", ExcelVersion.Version97to2003);        
        }
    }
}
[VB.NET]
Imports Spire.Xls

Namespace csvtoxls
    
    Class Program
        
        Private Shared Sub Main(ByVal args() As String)
            Dim workbook As Workbook = New Workbook
            workbook.LoadFromFile("D:\michelle\my file\csv2xls.csv", ",", 1, 1)
            Dim sheet As Worksheet = workbook.Worksheets(0)
            sheet.Name = "csv to excel"
            'set the backgroundcolor=LightBlue of Range["A1:E1"]
            sheet.Range("A1:E1").Style.Color = Color.Chocolate
            'set the backgroundcolor=LightBlue of Range["A2:E6"]
            sheet.Range("A2:E6").Style.Color = Color.SpringGreen
            'set the backgroundcolor=Silver of Range["A7:E12"]
            sheet.Range("A7:E12").Style.Color = Color.SkyBlue
            'set the backgroundcolor=Silver of Range["A13:E19"]
            sheet.Range("A13:E19").Style.Color = Color.Yellow
            workbook.SaveToFile("result.xls", ExcelVersion.Version97to2003)
        End Sub
    End Class
End Namespace
Friday, 21 September 2012 05:34

Freeze Excel Panes in C#, VB.NET

Excel Freeze Pane allows users to freeze row or column to keep one area always visible when scrolling. It is usual to freeze top row or left column. Also, users can freeze specified rows or columns in one area. For example, we can freeze the first three rows which contain labels to keep them always visible when scrolling down to get other data information in worksheet.

Spire.XLS for .NET, a professional component to operate Excel files, enables uses to freeze panes in Excel. And this guide will show you how to freeze by using C#, VB.NET. Users can invoke sheet.FreezePanes(int rowIndex, int columnIndex) method to set freezing area. The following screenshot shows the result after freezing panes.

Freeze Excel Panes

Download and install Spire.XLS for .NET and use the following code to freeze panes.

Code Sample:

[C#]
using System;
using Spire.Xls;
using System.Drawing;

namespace FreezePane
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load File
            Workbook workbook = new Workbook();
            workbook.LoadFromFile

(@"E:\Work\Documents\ExcelFiles\PartSalesInfo.xlsx");
            Worksheet sheet = workbook.Worksheets[0];

            //Freeze Top Row
            sheet.FreezePanes(2, 1);

            //Save and Launch
            workbook.SaveToFile("FreezePane.xlsx", ExcelVersion.Version2010);
            System.Diagnostics.Process.Start(workbook.FileName);
        }
    }
}
[VB.NET]
Imports System
Imports Spire.Xls
Imports System.Drawing

Namespace FreezePane
    Friend Class Program
        Shared Sub Main(ByVal args() As String)
            'Load File
            Dim workbook As New Workbook()
            workbook.LoadFromFile

("E:\Work\Documents\ExcelFiles\PartSalesInfo.xlsx")
            Dim sheet As Worksheet = workbook.Worksheets(0)

            'Freeze Top Row
            sheet.FreezePanes(2, 1)

            'Save and Launch
            workbook.SaveToFile("FreezePane.xlsx", ExcelVersion.Version2010)
            System.Diagnostics.Process.Start(workbook.FileName)
        End Sub
    End Class
End Namespace

The above code is used to freeze top row, if you want to freeze the first column, please use this line of code: sheet.FreezePanes(1,2);

Spire.XLS is a professional Excel component which enables developers/programmers to fast generate, read, write and modify Excel document for .NET and Silverlight. It supports C#, VB.NET, ASP.NET, ASP.NET MVC and Silverlight.

Wednesday, 19 September 2012 01:15

Find and Highlight Data in Excel in C#, VB.NET

Find function in Excel enables developers/programmers to find appointed data in Excel worksheet, and highlight function makes the data founded more observably. Find and highlight function gives developers and programmers more convenience of analyzing the data. This section demonstrates a clear introduction of find and highlight data in Excel worksheet via a .NET Excel component with C#, VB.NET.

Spire.XLS for .NET, as a .NET Excel component, enables users to quickly and easily find and highlight data in excel file. By calling Worksheet class method: public CellRange[] FindAllNumber(double doubleValue, bool formulaValue), users can find a certain value in cell range and then, highlight the value by setting the style property CellRange.Style.Color.

Please download Spire.XLS for .NET and install it on your system, add Spire.XLS.dll as reference in the downloaded Bin folder thought the below path: "..\Spire.XLS\Bin\NET4.0\ Spire.XLS.dll". And then, preview the effect of find and highlight task as below picture:

Example:

Find and Highlight Excel Data

[C#]
using Spire.Xls;

namespace fandh
{
    class Program
    {
        static void Main(string[] args)
        {
            //load an excel file from system
            Workbook workbook = new Workbook();
            workbook.LoadFromFile(@"D:\my file\FandH.xlsx", ExcelVersion.Version2010);

            //find and highlight excel data
            Worksheet sheet = workbook.Worksheets[0];
            foreach (CellRange range in sheet.FindAllNumber(1502,true))
            {
                range.Style.Color = Color.LawnGreen;
            }
            //save and launch the project
            workbook.SaveToFile("result.xlsx", ExcelVersion.Version2010);
            System.Diagnostics.Process.Start("result.xlsx");

        }
    }
}
[VB.NET]
Imports Spire.Xls

Namespace fandh
    
    Class Program
        
        Private Shared Sub Main(ByVal args() As String)
            'load an excel file from system
            Dim workbook As Workbook = New Workbook
            workbook.LoadFromFile("D:\my file\FandH.xlsx", ExcelVersion.Version2010)
            'find and highlight excel data
            Dim sheet As Worksheet = workbook.Worksheets(0)
            For Each range As CellRange In sheet.FindAllNumber(1502, true)
                range.Style.Color = Color.LawnGreen
            Next
            'save and launch the project
            workbook.SaveToFile("result.xlsx", ExcelVersion.Version2010)
            System.Diagnostics.Process.Start("result.xlsx")
        End Sub
    End Class
End Namespace
Thursday, 13 September 2012 09:21

Remove and Change Excel Comments in C#, VB.NET

This section will introduce a very simple solution to remove and change excel comment to better satisfy the need of deleting and modifying excel comment information. By using a .NET Excel component, the whole procedure can be realized by main two parts: remove excel comment and change excel comment.

Spire.XLS for .NET allows users to generate, read, write and modify Excel files without using MS Excel Automation. Using Spire.XLS for .NET, you can remove excel comment by calling the method:

Spire.Xls.Workbook.Worksheets[0].Comments[1].Remove();

Change excel comment by setting the Property:

Spire.Xls.Workbook.Worksheets[0].Comments[0]. Text= value;

Please view the original excel file which contains two excel comments as below picture:

Remove and Change Excel Comments

Now, please download Spire.XLS for .NET and see the key code of the task below:

Remove Excel Comment

[C#]
workbook.Worksheets[0].Comments[1].Remove()
[VB.NET]
workbook.Worksheets(0).Comments(1).Remove()

Change Excel Comment

[C#]
ExcelComment comment0 = workbook.Worksheets[0].Comments[0];
Comment0.Text = "The Sales Manager is Mishirna"
[VB.NET]
Dim comment0 As ExcelComment = workbook.Worksheets(0).Comments(0)
Comment0.Text = "The Sales Manager is Mishirna"

Friendly Reminder: workbook.Worksheets[0].Comments[0] means the first comment you insert in the first worksheet you created. So the first comment here may be not the first comment that you see in worksheet. In this Excel file, A8 is the first comment inserted in the worksheet, while A5 is the second comment.

After perform the code, please look at the effect of removing and changing excel comment task:

Remove and Change Excel Comments

Tuesday, 11 September 2012 02:26

Insert Word Hyperlink in Silverlight

Word hyperlink can guide users to get other related information to contents. It can be Email address, website link or even other documents. When inserting hyperlink, users can add for specified text or enter link address directly.

Spire.Doc for Silverlight, a powerful component on manipulating Word documents with Silverlight, enables users to insert hyperlink in Word with Silverlight. This guide will show the method to realize inserting word hyperlinks in Silverlight. Users can invoke paragraph.AppendHyperlink(link string, text string, hyperlink type) method which is provided by Spire.Doc for Silverlight directly to insert hyperlink in Word.

Download Spire.Doc for Silverlight. Create a Silverlight application. Add a button in MainPage.xaml, and then double it to use the following code to insert hyperlink.

The following screenshot demonstrates the result after inserting hyperlink to Word in Silverlight. The hyperlink is located at the bottom of the picture.

Insert Word Hyperlink

Sample code:

[C#]
using System;
using System.Windows;
using System.Windows.Controls;
using System.IO;
using System.Reflection;
using Spire.Doc;
using Spire.Doc.Documents;

namespace InsertHyperlink
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //Declare SaveFileDialog
            SaveFileDialog save = new SaveFileDialog();
            save.Filter = "Word Document(*.docx)|*.docx";

            //Load Document
            Document document = new Document();
            Assembly assembly = this.GetType().Assembly;
            foreach (String name in assembly.GetManifestResourceNames())
            {
                if (name.EndsWith("Spire.Doc for Silverlight.docx"))
                {
                    using (Stream docStream = assembly.GetManifestResourceStream(name))
                    {
                        document = new Document(docStream, FileFormat.Docx);
                    }
                }
            }

            //Add Hyperlink
            Section section = document.Sections[0];
            Paragraph LinkPara = section.AddParagraph();
            LinkPara.AppendHyperlink("http://www.e-iceblue.com/Introduce/word-for-silverlight-introduce.html", "Learn More about Spire.Doc for Siverlight", HyperlinkType.WebLink);

            //Set Paragrah Style
            ParagraphStyle style = new ParagraphStyle(document);
            style.Name = "LinkStyle";
            style.CharacterFormat.FontName = "Britannic Bold";
            style.CharacterFormat.FontSize = 13;
            style.ParagraphFormat.LineSpacingRule = LineSpacingRule.Multiple;
            style.ParagraphFormat.LineSpacing = 15F;
            document.Styles.Add(style);
            LinkPara.ApplyStyle(style.Name);

            //Save Document
            bool? result = save.ShowDialog();
            if (result.HasValue && result.Value)
            {
                using (Stream stream = save.OpenFile())
                {
                    document.SaveToStream(stream, FileFormat.Docx);
                }
            }  
        }
    }
}
[VB.NET]
Imports System.IO
Imports System.Reflection
Imports System.Drawing
Imports Spire.Doc
Imports Spire.Doc.Documents

Partial Public Class MainPage
    Inherits UserControl

    Public Sub New()
        InitializeComponent()
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
        'Declare SaveFileDialog
        Dim save As New SaveFileDialog()
        save.Filter = "Word Document(*.docx)|*.docx"

        'Load Document
        Dim document As New Document()
        Dim [assembly] As System.Reflection.Assembly = Me.GetType().Assembly
        For Each name As String In [assembly].GetManifestResourceNames()
            If name.EndsWith("Spire.Doc for Silverlight.docx") Then
                Using docStream As Stream = [assembly].GetManifestResourceStream(name)
                    document = New Document(docStream, FileFormat.Docx)
                End Using
            End If
        Next name

        'Add Hyperlink
        Dim section As Section = document.Sections(0)
        Dim LinkPara As Paragraph = section.AddParagraph()
        LinkPara.AppendHyperlink("http://www.e-iceblue.com/Introduce/word-for-silverlight-introduce.html", "Learn More about Spire.Doc for Siverlight", HyperlinkType.WebLink)

        'Set Paragrah Style
        Dim style As New ParagraphStyle(document)
        style.Name = "LinkStyle"
        style.CharacterFormat.FontName = "Britannic Bold"
        style.CharacterFormat.FontSize = 13
        style.ParagraphFormat.LineSpacingRule = LineSpacingRule.Multiple
        style.ParagraphFormat.LineSpacing = 15.0F
        document.Styles.Add(style)
        LinkPara.ApplyStyle(style.Name)

        'Save Document
        Dim result? As Boolean = save.ShowDialog()
        If result.HasValue AndAlso result.Value Then
            Using stream As Stream = save.OpenFile()
                document.SaveToStream(stream, FileFormat.Docx)
            End Using
        End If
    End Sub
End Class

Spire.Doc is an Microsoft Word component which enables users to perform a wide range of Word document processing tasks directly, such as generate, read, write and modify Word document in WPF.NET and Silverlight.

Monday, 10 September 2012 06:41

Add PDF Hyperlink in WPF

This section will introduce a solution to add two types of hyperlinks in PDF document via a WPF PDF component. One is a hyperlink directly displayed as url address in the PDF file, suppose it is named Hyperlink1; The other is a hyperlink in place of text, you can call it hyperlink2. Both of the two hyperlinks can automatically take you to a webpage, a file or an image when you click them. Before you start, it is very necessary to know some information of this PDF component

Spire.PDF for WPF enables you to directly generate, read, write and manipulate PDF files in your WPF applications without installing Adobe Acrobat or any third party library. Using Spire.PDF for WPF, you can easily add PDF hyperlink by three key steps. Please Download Spire.PDF for WPF and view the effective screenshot of this task as below picture:

Add Hyperlink in PDF

Step 1: Set the link location in PDF page.

After you loading an existing PDF file or creating a new PDF file(a PDF file is imported from system in this step), please set the approximate location of hyperlink in PDF page by calling the Spire.Pdf.PdfPageBase: Canvas.ClientSize.Height.

[C#]
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile(@"..\image to pdf.pdf");
            PdfPageBase page = doc.Pages[0];
      
            PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
            PdfMargins margin = new PdfMargins();
            margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Bottom = margin.Top;
            margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Right = margin.Left;
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Verdana", 17));
            float space = font.Height * 0.75f;
            float y = page.Canvas.ClientSize.Height*0.6f - margin.Bottom + space;
[VB.NET]
           Dim doc As New PdfDocument()
	   doc.LoadFromFile("D:\michelle\my file\image to pdf.pdf")
	   Dim page As PdfPageBase = doc.Pages(0)
	   Dim unitCvtr As New PdfUnitConvertor()
	   Dim margin As New PdfMargins()
	   margin.Top = unitCvtr.ConvertUnits(2.54F, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point)
	   margin.Bottom = margin.Top
	   margin.Left = unitCvtr.ConvertUnits(3.17F, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point)
	   margin.Right = margin.Left
	   Dim font As New PdfTrueTypeFont(New Font("Verdana", 17))
	   Dim space As Single = font.Height * 0.75F
	   Dim y As Single = page.Canvas.ClientSize.Height * 0.6F - margin.Bottom + space

Step 2: Add Hyperlink1 in PDF.

In this step, you can draw a string in PDF page by calling Spire.Pdf. PdfPageBase method: PdfPageBase.Canvas.DrawString(string s, PdfFontBase font, PdfBrush brush, float x, float y, PdfStringFormat format) method. As the string format is a url address, a link can be drawn. Also by calculating the string width of label, link and PDF page, both link label location and link location can be set. In this method, link label and link are set in the middle.

[C#]
            String label = "Image Source: ";
            PdfStringFormat format = new PdfStringFormat();
            format.MeasureTrailingSpaces = true;
            float x1 = font.MeasureString(label, format).Width;
            PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Verdana", 17, System.Drawing.FontStyle.Bold));
            String url1 = "http://www.e-iceblue.com";
            float x2 = font2.MeasureString(url1, format).Width;
            float x =( page.Canvas.ClientSize.Width-x1-x2)/2;
            page.Canvas.DrawString(label, font, PdfBrushes.DeepSkyBlue, x, y, format);
            x += x1;
            page.Canvas.DrawString(url1, font2, PdfBrushes.DeepSkyBlue, x, y);
            y = y + font2.MeasureString(url1).Height;
[VB.NET]
           Dim label As [String] = "Image Source: "
	   Dim format As New PdfStringFormat()
	   format.MeasureTrailingSpaces = True
	   Dim x1 As Single = font.MeasureString(label, format).Width
	   Dim font2 As New PdfTrueTypeFont(New Font("Verdana", 17, System.Drawing.FontStyle.Bold))
	   Dim url1 As [String] = "http://www.e-iceblue.com"
	   Dim x2 As Single = font2.MeasureString(url1, format).Width
	   Dim x As Single = (page.Canvas.ClientSize.Width - x1 - x2) / 2
	   page.Canvas.DrawString(label, font, PdfBrushes.DeepSkyBlue, x, y, format)
	   x += x1
	   page.Canvas.DrawString(url1, font2, PdfBrushes.DeepSkyBlue, x, y)
	   y = y + font2.MeasureString(url1).Height

Step 3: Add hyperlink 2 in place of text.

Use the method in step2 to draw a link label and add hyperlink2 in place of text by the method Spire.Pdf.Annotations. PdfTextWebLink to set link properties such as text, url and so on. Finally, invoke DrawTextWebLink(PdfCanvas graphics, PointF location) method to draw the PDF hyperlink in place of text.

[C#]
            label = "Simple Link: ";
            x -= x1;
            page.Canvas.DrawString(label, font, PdfBrushes.DarkViolet, x, y, format);
            float xoffset2 = font.MeasureString(label, format).Width;
            x += xoffset2;
            String text = "e-iceblue";
            PdfTextWebLink link2 = new PdfTextWebLink();
            link2.Text = text;
            link2.Url = url1;
            link2.Font = font2;
            link2.Brush = PdfBrushes.DarkViolet;
            link2.DrawTextWebLink(page.Canvas, new PointF(x, y));
[VB.NET]
        label = "Simple Link: "
	x -= x1
	page.Canvas.DrawString(label, font, PdfBrushes.DarkViolet, x, y, format)
	Dim xoffset2 As Single = font.MeasureString(label, format).Width
	x += xoffset2
	Dim text As [String] = "e-iceblue"
	Dim link2 As New PdfTextWebLink()
	link2.Text = text
	link2.Url = url1
	link2.Font = font2
	link2.Brush = PdfBrushes.DarkViolet
	link2.DrawTextWebLink(page.Canvas, New PointF(x, y))

Spire.PDF is a PDF document creation component that enables your WPF applications to read, write and manipulate PDF documents without using Adobe Acrobat. Now, the new version added Silverlight platform which makes it more powerful.

Monday, 10 September 2012 05:39

Set PDF Properties in Silverlight

This section is designed to introduce the quickest solution to set PDF properties in Silverlight. As is known to all, PDF property shows the basic information of a PDF document, such as PDF document name, title, author, subject, keywords, creation/ modify date and so on. The vital reason to call it the quickest solution is that all the PDF properties referred above can be respectively set by only one line of key code via a Silverlight PDF component.

Spire.PDF for Silverlight is a PDF component, which enables you to create, read, write and manipulate PDF document in your Silverlight applications. Using Spire.PDF for Silverlight, you can set your PDF properties by calling the method: public PdfDocumentInformation DocumentInformation. Please preview the effect of your PDF property setting as below picture:

Set PDF Properties

Now, please Download Spire.PDF for Silverlight and then, view the key code below.

Sample Code:

[C#]
document.DocumentInformation.Title = "image to PDF";
document.DocumentInformation.Author = "E-iceblue";
document.DocumentInformation.Subject = "convert image to PDF";
document.DocumentInformation.Producer = "Spire.PDF";
document.DocumentInformation.Keywords = "C# JPG/PNG/BMP/GIF/TIF to PDF";
document.DocumentInformation.ModificationDate = new DateTime(2012, 9, 10, 14, 30, 50);
[VB.NET]
document.DocumentInformation.Title = "image to PDF"
document.DocumentInformation.Author = "E-iceblue"
document.DocumentInformation.Subject = "convert image to PDF"
document.DocumentInformation.Producer = "Spire.PDF"
document.DocumentInformation.Keywords = "C# JPG/PNG/BMP/GIF/TIF to PDF"
document.DocumentInformation.ModificationDate = New DateTime(2012, 9, 10, 14, 30, 50)

Spire.PDF for Silverlight is a PDF Silverlight component that enables your Silverlight applications to read, write and manipulate PDF documents without using Adobe Acrobat.

Thursday, 30 August 2012 03:30

Insert Image in Word Document in WPF

Word Image can make one document more interesting and impressive. Sometimes, image can be used explain contents. For example, if one document focuses on describing appearance one kind of birds, readers can learn more clearly with a bird picture.

Spire.Doc for WPF, a professional component to manipulate Word documents with WPF, enables users to insert image in Word with WPF. And this guide will show a method about how to insert image Word in WPF quickly.

Users can invoke paragraph.AppendPicture(image) method to insert image in Word directly. If you want to set image height/width to make picture display appropriately in document, you can use Height and Width property provided by DocPicture class which Spire.Doc for .NET offers. Below, there is the result after inserting image in Word.

Insert Word Image

Download and install Spire.Doc for WPF. Then, add a button in MainWindow. Double click this button and use the following code to insert image in Word.

Code Sample:

[C#]
          //Create Document
            Document document = new Document();
            Section section = document.AddSection();
            Paragraph Imageparagraph = section.AddParagraph();

            //Insert Image
            Image image = Image.FromFile(@"E:\work\Documents\Image\street.jpg");
            DocPicture picture =Imageparagraph.AppendPicture(image);

            //Set Image
            picture.Height = 360;
            picture.Width = 525;
[VB.NET]
        'Create Document
        Dim document As New Document()
        Dim section As Section = document.AddSection()
        Dim Imageparagraph As Paragraph = section.AddParagraph()

        'Insert Image
        Dim image As Image = image.FromFile("E:\work\Documents\Image\street.jpg")
        Dim picture As DocPicture = Imageparagraph.AppendPicture(image)

        'Set Image
        picture.Height = 360
        picture.Width = 525

Spire.Doc is a Microsoft Word component, which enables users to perform a wide range of Word document processing tasks directly, such as generate, read, write and modify Word document in WPF, .NET and Silverlight.

Thursday, 30 August 2012 01:41

Clearly Convert RTF to PDF in WPF

Whatever solution you use to convert RTF to PDF before, the solution that will be introduced is the easiest method to clearly realize your RTF to PDF conversion task. The whole process can be accomplished through three lines of key code in your WPF application via a Word component Spire.Doc for WPF.

Spire.Doc for WPF, different from RTF to PDF converters, is a WPF Word component, which can generate, read, write and modify word documents in your WPF applications. Apart from converting RTF to PDF, Spire.Doc for WPF can convert word to many other commonly used formats such as PDF, HTML, Text, XML, Image and so on. Please first preview the effective screenshot of the target PDF file:

RTF to PDF

Now, please download Spire.Doc for WPF and convert your RTF to PDF by the code below:

[C#]
using Spire.Doc;
namespace WPFRTFtoPDF
{
      public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Document doc = new Document();
            doc.LoadFromFile(@"..\WPFRTFtoPDF.rtf", FileFormat.Rtf);
            doc.SaveToFile("test.pdf", FileFormat.PDF);
        }
    }
}
[VB.NET]
Imports Spire.Doc
Namespace WPFRTFtoPDF
	
	Public Partial Class MainWindow
		Inherits Window
		Public Sub New()
			InitializeComponent()
		End Sub

		Private Sub button1_Click(sender As Object, e As RoutedEventArgs)
			Dim doc As New Document()
			doc.LoadFromFile("..\WPFRTFtoPDF.rtf", FileFormat.Rtf)
			doc.SaveToFile("test.pdf", FileFormat.PDF)
		End Sub
	End Class
End Namespace

For comparison, I put the original RTF file below:

RTF to PDF

Spire.Doc is a standalone word component, which enables users to perform a wide range of word document processing tasks in WPF, .NET and Silverlight without installing MS Word on system.