page 283

Convert Excel to PDF in WPF

2012-07-24 08:22:28 Written by Koohji

Software developers are often asked to find a way to convert Microsoft Excel into PDF as PDF files are widely used for exchanging documents between organizations, government sectors and individuals. This solution demonstrates a way of converting Excel  to PDF for WPF developers and maintains high visual fidelity in the conversion.

Spire.XLS for WPF is a WPF Excel component which enables your WPF applications to fast generate, read, write and modify Excel document without Microsoft Office Excel Automation. It also fully supports converting files from Excel to PDF, Excel to HTML, Excel to CSV, Excel to Text, Excel to Image and Excel to XML. All the conversion is independent of any other software.

Any kind of trial and evaluation is always welcomed. Please feel free to download Spire.XLS for WPF to have a trial and convert your Excel to PDF for personal use. Below is a screenshot of the source Excel file we load in this demo. At the end, a screenshot of PDF will be demonstrated for comparison with the source Excel file.

Excel to PDF

Now this is the key procedure for converting Excel to PDF in WPF.

Step 1: Load Excel file or Create Excel from scratch.

In this step, instantiate an object of the Workbook class by calling its empty constructor and then you may open/load an existing template file or skip this step if you are creating the workbook from scratch.

[C#]
// load Excel file
Workbook workbook = new Workbook();
workbook.LoadFromFile("D:\\test.xlsx");                  

[VB.NET]

' load Excel file
Dim workbook As New Workbook()
workbook.LoadFromFile("D:\test.xlsx")

Step 2: Set PDF template.

In this step, we will set PDF template which will be used below.

[C#]
// Set PDF template
PdfDocument pdfDocument = new PdfDocument();
pdfDocument.PageSettings.Orientation = PdfPageOrientation.Landscape;
pdfDocument.PageSettings.Width = 970;
pdfDocument.PageSettings.Height = 850;                  
[VB.NET]
' Set PDF template
Dim pdfDocument As New PdfDocument()
pdfDocument.PageSettings.Orientation = PdfPageOrientation.Landscape
pdfDocument.PageSettings.Width = 970
pdfDocument.PageSettings.Height = 850

Step 3: Convert Excel to PDF in WPF.

Now we will use the template which we have already set above to convert Excel to PDF in WPF.

[C#]
//Convert Excel to PDF using the template above
PdfConverter pdfConverter = new PdfConverter(workbook);
PdfConverterSettings settings = new PdfConverterSettings();
settings.TemplateDocument = pdfDocument;
pdfDocument = pdfConverter.Convert(settings);            
[VB.NET]
'Convert Excel to PDF using the template above
Dim pdfConverter As New PdfConverter(workbook)
Dim settings As New PdfConverterSettings()
settings.TemplateDocument = pdfDocument
pdfDocument = pdfConverter.Convert(settings)

Step 4: Save and preview PDF.

Please use the codes below to save and preview PDF.

[C#]
// Save and preview PDF
pdfDocument.SaveToFile("sample.pdf");
System.Diagnostics.Process.Start("sample.pdf");        
[VB.NET]
' Save and preview PDF
pdfDocument.SaveToFile("sample.pdf")
System.Diagnostics.Process.Start("sample.pdf")

The picture below is a screenshot of the PDF, Please see:

Excel to PDF

Draw PDF Image in WPF

2012-07-24 05:41:51 Written by Koohji
Drawing PDF image in WPF is not as easy as most of developers imagine. Getting a beautifully  matched PDF text and image requires them to properly set both the image size and image position in the PDF document. Any unsuitably handled in each aspect may lead the text covered by the PDF image. This solution is offered for you to draw PDF image in WPF applications without the worry above.

 

Spire.PDF for WPF is a WPF PDF component which provides great convenience in reading, writing and modifying PDF documents without using Adobe Acrobat or any third party component library. Spire.PDF for WPF contains a namespace “Spire.Pdf.Graphics” in which there are four versions of DrawImage method that can be applied to insert PdfImage in PDF document.

 

Please feel free to Download Spire.PDF for WPF and draw PDF Image in WPF, Below is an effective screenshot of the PDF image.

 

Draw Image in PDF

 

Now this is the key procedure of PDF image in WPF.

 

1, Load PDF and image file from system.
Two files are demanded in this step: a PDF file in which the image will be inserted and an image file.

 

[C#]
doc.LoadFromFile(@"..\Sample.pdf");
System.Drawing.Image image = System.Drawing.Image.FromFile(@"..\MS.jpg");
[VB.NET]
doc.LoadFromFile("..\Sample.pdf")
Dim image As System.Drawing.Image = System.Drawing.Image.FromFile("..\MS.jpg")

 

2,Adjust image size and draw image in PDF file.
In this step, one way to use DrawImage method is demonstrated with two parameters: the source image and its position.

 

[C#]
//adjust image size
int width = image.Width;
int height = image.Height;
float schale = 0.7f;
System.Drawing.Size size = new System.Drawing.Size((int)(width * schale), (int)(height * schale));
Bitmap schaleImage = new Bitmap(image, size);
//draw image into the first PDF page at specific position 
PdfImage pdfImage = PdfImage.FromImage(schaleImage);
PdfPageBase page0 = doc.Pages[0];
PointF position = new PointF((page0.Canvas.ClientSize.Width - schaleImage.Width) / 8, 10);
page0.Canvas.DrawImage(pdfImage, position);

[VB.NET]
'adjust image size
Dim width As Integer = image.Width
Dim height As Integer = image.Height
Dim schale As Single = 0.7F
Dim size As New System.Drawing.Size(CInt(Math.Truncate(width * schale)), CInt(Math.Truncate(height * schale)))
Dim schaleImage As New Bitmap(image, size)
'draw image into the first PDF page at specific position 
Dim pdfImage__1 As PdfImage = PdfImage.FromImage(schaleImage)
Dim page0 As PdfPageBase = doc.Pages(0)
Dim position As New PointF((page0.Canvas.ClientSize.Width - schaleImage.Width) / 8, 10)
page0.Canvas.DrawImage(pdfImage__1, position)

 

It is obvious that Spire.PDF for WPF can realize PDF image task in minutes. Besides, Spire.PDF for WPF also has the functions of PDF protect, PDF conversion, PDF drawing, click to know more about this Professional WPF PDF Components.

 

Move Worksheet in WPF

2012-07-19 07:34:04 Written by Koohji

It is not an easy task for WPF developers moving worksheet to another location in a workbook, as calculations or charts that are based on worksheet data might become inaccurate if you move the worksheet in WPF application. Here presents a solution that saves you from these worries. Apply Spire.Xls for WPF in your application,  and you easily can move worksheet in your WPF application.

Spire.XLS for WPF is a professional Excel component which enables developers/programmers to fast generate, read, write and modify Excel document in WPF applications. Spire.XLS for .NET embed a method - Spire.Xls.WorkShee.MoveWorksheet(int destIndex) in its class design used to move a worksheet to another location in the spreadsheet. The method takes the target worksheet index as a parameter and lead no inaccuracy on these calculations or charts.

Now Feel free to download Spire.XLS for WPF and use  the code samples below to move worksheet in WPF application.

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

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            //open Excel
            Workbook mywbk = new Workbook();
            mywbk.LoadFromFile(@"..\test.xls");

            // Locate the Worksheet
            Worksheet mysht = mywbk.Worksheets[0];

            //Move Worksheet
            mysht.MoveWorksheet(2);

            //Save and Launch
            mywbk.SaveToFile("result.xls");
            System.Diagnostics.Process.Start("result.xls");

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

Namespace WpfApplication1
	Public Partial Class MainWindow
		Inherits Window
		Public Sub New()
			InitializeComponent()
		End Sub
		Private Sub button2_Click(sender As Object, e As RoutedEventArgs)
			'open Excel
			Dim mywbk As New Workbook()
			mywbk.LoadFromFile("..\test.xls")

			' Locate the Worksheet
			Dim mysht As Worksheet = mywbk.Worksheets(0)

			'Move Worksheet
			mysht.MoveWorksheet(2)

			'Save and Launch
			mywbk.SaveToFile("result.xls")
			System.Diagnostics.Process.Start("result.xls")

		End Sub
	End Class
End Namespace
page 283