Wednesday, May 18, 2016

How to view document using Visual Studio

There are numerous scenarios in which you need just to display Word documents with a limited interaction from the user, like when providing a service of access to documents that don't need to be converted to another format to be accessed by the users. In cases like this, you could try the tool Spire.DocViewer for .NET. This is a tool to manipulate Word documents in .NET, and it allows us to:
  • View Word Documents
  • Easily print Word documents
  • Convert document formats
Now I'll give you a quick review of this tool, based in a simple example, where I'll walk through step by step. So, let's check this E-IceBlue product.

Requirements:

For these two last requirements, you might want to check instructions used for similar tools from E-IceBlue. Please check the instructions to create new projects and add references here and here.

Steps:

1. Add ToolStrip to the form, and inside it, add two buttons one to open a document and the other to close it. Use this button:
Add elements

to add the buttons. You should see something like this when you're done:

Let's say that the first button is to open and the second is to close the document. You can change the default image if you want.

2. Double click Form1.Designer.cs. Here, we'll add the code for a field to display the document. This is not in the toolbox because is a Spire.DocViewer element. Here's the complete code of that file:


Now, you should see this in the designer view of the form:
Form with viewer

3. Next, we have to add code to the Form1.cs file. Basically, what I do here is:
  • Add the code in the button for opening documents
  • Add the code in the button for closing documents
Following, the complete code for Form1.cs:


Save everything. Now, just click Start and you'll see you'r app working:
App Running
Now, let's get it work:
Press the first button at the left. A window will opened and you'll be able to browse a doc file. Select it and it will be displayed in the Doc Viewer area:
Select a file

Loading doc

Open document

To close the document, just click the second button.

Conclusions

As with other E-IceBlue products, my experience was positive, mainly because the easy usage of the product. However, I spent some time coding the Doc View in the design file because there was no element in the toolbox for this. I think it would be great if they automatically add the design element to the toolbox when the installation happens. Other than that, I enjoyed using this product. It's easy enough to learn from the demo coding files, but it would be good to have a little tutorial for the design part. You can get more information about this product in the official webpage: http://www.e-iceblue.com/Introduce/word-viewer-net-introduce.html#.VzjSFWgrKhd

Tuesday, May 17, 2016

To handle your PDFs in Visual Studio

PDF is a popular format for distributing documents, and it is practically a standard in the industry. Taking that into account, it's important to have an easy-to-use tool at hand, especially in environments when deadlines and pressure are every day things. Spire.PDF can be that tool. It allows us to manipulate PDF files with an easy syntax and lots of documentation. But let's see it action with a detailed tutorial/demo. What we are going to do is to join PDF files into one. I have been requested this before, and I know it's useful. Let's begin.

Requirements:
  • Visual Studio. Download it at:
  • Spire.PDF installed. Download it at:
  • A new Windows Forms project. I called mine "TestoolsPDF".
  • The Spire.PDF.dll file added to the references of the project
  • Two PDF files to join in a folder that you know. I placed mine at: C:\testoolspdf. My files are don-quijote.pdf and My-Twisted-World.pdf.
Steps:

1. Add a button to the form
2. Select the button and change the "Text" property to "Join PDFs".
Join PDF
3. Double click the button and the code view will be displayed. Now the cursor is inside the button1_Click method:
Button click method  
4. Inside the button1_Click method, you need to:
  • Create an array with the files that you'll join:
            //Paths of the files
     String[] files = new String[]
     {
         @"C:\testoolspdf\don-quijote.pdf",
         @"C:\testoolspdf\My-Twisted-World.pdf"
     };
  • Create an array of PDF documents to store the files obtained from the paths
     //Create an array of PDFs
     PdfDocument[] docs = new PdfDocument[files.Length];
  • Convert the paths into PDF files and store them:
     //Convert the paths into files and store them
     for (int i = 0; i < files.Length; i++)
     {
         docs[i] = new PdfDocument(files[i]);
     }
  • Append and save the documents:
     //Append and save the documents
     docs[0].AppendPage(docs[1]);
     docs[0].SaveToFile(@"C:\testoolspdf\Twisted-World-Don-Quijote.pdf");
  • Close the documents
     //close
     foreach (PdfDocument doc in docs)
     {
         doc.Close();
     }
  • Finally, you can open the merged document:
    //Open the merged document
    System.Diagnostics.Process.Start(@"C:\testoolspdf\Twisted-World-Don-Quijote.pdf");

The complete code for the Form1.cs file is as follows:


5. Click he start button:
Start button

The app should start running:
App running

6. Click the "Join PDFs" button. The merged document should be opened:
Merged document
7. Go to the folder that you picked to save the merged document. Verify if it was successfully created:
Generated document

As you can see, it takes just some simple steps to create an app that can make basic operations with PDF files. Spire.PDF is a great tool and it simply delivers what it promises. I encourage you to try it, since it has a free trial period. You can download the tool at: http://www.e-iceblue.com/Download/download-pdf-for-net-now.html

For more information about this tool and others from the same vendor, please refer to: http://www.e-iceblue.com/

Handling PPT files with Spire.Presentation for .NET

Spire.Presentation is a powerful tool to handle PowerPoint files in an easy way. The main features of this tool are:
  • Conversion of presentation slides to different formats
  • All the operations to handle presentation files: Create, open and edit PPT files
One of the first usages of this tool that came to my mind was to use it to convert a presentation of multiple slides into a collection of images. This is something that would be useful to store data and share parts of the presentation. So, I'll use it as an example in this review.

Requirements:
  • Visual Studio 2015 Community Edition (you can download it from here)
  • Any PowerPoint presentation. In my case, I placed a presentation called "Leonardo-da-Vinci.pptx" at C:\testools.
  • Spire.Presentation installed (download it from here)
  • A new Windows Forms project called "TestoolsPresentation"
  • Testools presentation
  • The Spire.DocViewer.Forms.dll  reference added to the project.
  • Spire presentation dll
  • Spire presentation reference added
If you want to see a more detailed explanation about adding the references, check the related articles here and here.

Steps:

1. Add a simple button to the form. If you want, you can change the text property so it looks nicer :)
Form with button
2. Double click the button to go to the code. The Form1.cs file will be opened. The complete code that should be in that file is:

Note: Be careful to specify the full path to the file you want to convert at:

presentation.LoadFromFile(@"C:\testools\Leonardo-da-Vinci.pptx");

Also, you must specify the path and name pattern for the resulting images at:

String fileName = String.Format(@"C:\testools\result-img-{0}.png", i);
image.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);

Click Start. You should see your app running:
App running
If you click the button in the app, you should see a set of images in the desired folder:


Spire.Presentation is a great and powerful tool when it comes to handle PowerPoint files in .NET applications. Also, it's easy enough to be understood even by beginners. Another point to take into account is the great amount of documentation available for the tool. There are any tutorials available to the public, so I encourage you to take a look and explore the tool by yourself. You can find the information I mentioned at: http://www.e-iceblue.com/Introduce/presentation-for-net-introduce.html#.VzpJEWgrKhc

Tuesday, May 3, 2016

Manage Excel files programmatically with Spire.XLS

The Excel files are already an important and traditional part of the office work practically everywhere in the business world. So, the ability of handling this type of files from the code can be critical, depending on the project you are working on.

Spire.XLS is a tool that allows us to work with Excel files from the code. Some of its features are:
  • It allows to create and edit Excel files
  • Supports conversion from and to Excel
  • Supports both for the old Excel 97-2003 format (.xls) and for the new Excel 2007, Excel 2010 and Excel 2013 (.xlsx, .xlsb, .xlsm), along with Open Office(.ods) format.
  • Several formatting options.
I'm going to test some of those features using a simple example of the usage of the tool.

Prerequisites:
Hands on:

Create a Windows Application form

1. Go to File > New > Project...
2. Select Windows Forms Application and give a name to your project.
New test application
3. Add a button to the form. Since this is a review, we'll keep things simple.



Add the Spire.XLS reference

1. Go to Project > Add Reference..
2. Cick Browse and select the Spire.XLS.dll file. If you are using the default path, it should be at: C:\Program Files (x86)\e-iceblue\Spire.xls\Bin
3. Select Spire.XLS.dll and click OK.


Add the code for handing Excel files

1. Double click the button you added to the form
2. Enter the code for the Excel generation in the click method. The entire code should look like this:


Easy enough, right? Now click Start in Visual Studio. Then, when your application is displayed, click "Create Excel file". After that, the Excel file that you just created should be opened. You'll see the Evaluation Warning in Sheet 4:
Evaluation Warning

Now, open Sheet 1 to see the data we entered in the code:
Application output

Let's export that data into HTML. To do that, follow these steps:

Add a new button to the form:
Second button
Double click the second button and add this code to the second button click method:


Now, click Start in Visual Studio and then click "Export Excel to HTML". A browser window will be opened and you'll see the content of the Excel file in HTML:


As with the rest of the Office related products, Spire.XLS offers a simple but powerful approach to handle Excel files programmatically. One of the things I liked most about the product is the documentation: Plenty and easy to read. It could be said that any user with basic programming experience should be able to use Spire.XLS without any problem. You can check more information about Spire.XLS here.

Friday, April 29, 2016

Review: Spire.DataExport for .NET

The data flow is a critical part of any project that handles data as part of its main functionality. Even when this is not true, the correct flow of data is an important goal that always takes, at least, some effort.

With that in mind, I'll review now a tool to do just that: export data to various formats. The tool is Spire.DataExport for .NET. Some of the features of this tool are:

Support for popular formats: MS Excel, MS Word, HTML, MS clipboard, XML, PDF, DBF, SQL Script, SYLK, DIF, CSV.
Exports data from Command, ListView and DataTable components.
Independent from data handling applications such as Microsoft Excel

This article will be both a walk through and a review. For that, I'll create a simple example that shows the real usage of the tool. Since XML is the format I worked with in many projects, I'll show the feature to export to XML.

Let's begin!

Requirements
Of course, you can use different data sources. These requirements are specific for this example.

Steps taken:

Create a new project

1. I created a simple Windows Form Application. I named it "SpireDataExportTest":
New Windows Form Aplication

2. Add a DataGridView component and a button. It should look like this:

AS you can see, I modified the text of the button. You can do other UI modifications as well if you think you need to.

3. Add the Spire.DataExport reference.
3.1 Right click References > Add Reference...
3.2 Click Browse and go to the path where you installed Spire.DataExport. The default path is C:\Program Files (x86)\e-iceblue\Spire.DataExport\Bin\NET4.0, in the case you have .NET Framework 4 (you might have to check your .NET Framework version. You can consult this if you have doubts).

Now, click OK.

4. Connect the grid to the database
There are several steps you need to take for this. Although it's not too complicated, I won't list all the steps here because the goal of the article is to do a review of the Spire.DataExport component. You might need a more detailed explanation, so I highly recommend to read this article.

However, this is the code you should have in Form1.cs up to this point:


And this is the database I used:

I called "testdb". Take into account that the Connection String in the code depends on this database and its configuration. Let's move on.

Code the action of exporting to XML

1. Double click the "Export to XML" button
2. Inside the method "button1_Click", enter the the code to export to xml. It should look like this:


As you can see, the method names are very straightforward. The only detail that you have to take care of is that xmlExport.DataTable only receives DataTable. So, what I did was declaring DataSet ds = new DataSet(); at the class level and then I converted the first table of the data set to DataTable.

The last thing left is to test the code. So, click Start and then click "Export to XML" once the program is running. You should see the data in XML:







Conclusions

The most important feature of this particular Spire product is that it's very simple to understand, learn and use. In fact, when writing this article, it was far harder to connect the database to the data grid than adding the functionality to export to XML. Another point is that the methods have clear names. This is very important, and many developers tend to forget that detail.

I really enjoy using these products, and I look forward to try the next one. More information about this tool and other similar products here.
Related Posts Plugin for WordPress, Blogger...