Spire.Doc .NET Library For Cross File Format Document Creation

Developers usually waste 90% of their time thinking how to generate Office format documents. Perhaps that is what is being used by most companies and developers right now. Microsoft Word, PDF, Rich Text Format and other similar formats are in high demand these days.

Creating a custom interoperability service is a very time-consuming and a less efficient function when the solution type for your application is enterprise. Third-party applications provide a very good solution to such tasks that can enable us to kick-start our applications in a matter of a few minutes.

Spire.Doc (by E-Iceblue) is one of them. I have personally tried the software library for the .NET Framework and it is kind of a helpful tool for .NET developers (no matter whether you are a Windows Forms, Windows Presentation Foundation or ASP.NET developer) for generating cross-file format files.

Note: By cross-file format I mean you can generate the output in a number of formats, file based (under files, this library supports PDF, Microsoft Word, HTML web pages, Rich Text Format and more), it also supports generating images for your documents and to get the streams for your document resources.

Spire.Doc API: I have used Spire.Doc (and other connected APIs and namespaces) to provide it a quick look and start. If someone has a great understanding of how these documents are, what a paragraph is and what can be added to various components.

The very basic document in Microsoft Word would require only:

  1. Single document object.

  2. Single section to contain the data. This is also the page of your applications document.

  3. A number of paragraphs, depending on whether and what you want to add to the document. The paragraph would allow you to add images or other stuff, such as text and characters; symbols in Unicode format you just need to the integer value for the Unicode character.

  4. Tables are also added to the section object, if required.

The Spire.Doc namespace also allows you to save the document, in any number of formats, ranging from PDFs to a wide variety of Microsoft Word formats.

Note for E-Iceblue developers

I have had a few intuition problems using the product, for a .NET developer, adding a new element to the current child hierarchy is something like the following.

  1. var element = new Element();  
  2. // Any properties or similar editions  
  3. parentElement.Children.Add(element); 
Whereas, Spire.Doc supports adding the the objects to the document as in the following:
  1. var element = parentElement.AddChild();  
  2. // Child type is determined by the function called, AddParagraph, AddTable etc.  
  3. // Then element is edited out.
Perhaps, this is just my opinion and problem understanding. I would believe that most readers would not agree with me on this point. Also, the API guide is a good one, I loved it. But it lacks the description of the class that is open at the moment.

A few code samples for readers

Since review-only posts are not my style, I would provide a few code samples to the readers also, as if to show how easy it is to use the Spire.Doc library for their personal use. As already said, this is a library for the .NET Framework, the library is consumable by every framework (although, E-Iceblue also ships the library in various versions, one for every framework on .NET).

The same code sample would work on Windows Forms, Windows Presentation Foundation, SilverLight, ASP.NET and (as I have tested it enormous times on) Console applications.

Documentation of the Spire.Doc is available along with the package when you download it (and it is a very great and intuitive documentation, with a style pretty much as that of the MSDN). You can always use the documentation application, to read the API and continue where you get stuck when developing the application.

Loading a new document

Loading the new document is similar, whether you are trying to create a new document, or load it from an existing file. Spire.Doc provides you with constructors that can be used to initialize the Document object.
  1. // First method, new document  
  2. var document = new Document(); // works
  3. // Second method, existing document  
  4. var document = new Document("path", FileFormat.Docx); 
In the preceding examples, there are two documents being generated (please ignore the document that already exists in the current context for a moment). The first one is the constructor used to generate a new document (perhaps for creating a new file), whereas the second one is used to load an existing file as a document object in your application. You can load files of any type as explained above.

Once loaded, you can then work with a document to add a new section, paragraph or other similar object or to edit the existing objects in the document.

Editing the document

Editing the document is another key feature of this library, because it not only lets you create new documents but also allows you to edit already existing documents and files, no matter what data type they hold. The key points are that you can edit the document (of any type) and save it (in any format).

You can add new paragraphs or sections to your document that in turn allow you to add images and text to the document. (As already said above) I do have a little problem with this process in the Spire.Doc namespace, but still it is a great way of handle-oriented programmers that have a great intuition for creating the object then saving the handle for further processes.
  1. // Creating a document, adding a section and a new Paragraph.
  2. var document = new Document();  
  3. var section = document.AddSection();  
  4. // update the section, if required  
  5. var paragraph = section.AddParagraph();  
  6. // Update the paragraph, for example.
  7. paragraph.AppendText("Love for all, hatred for none!");  
  8. paragraph.AppendPicture(imageObject); // accepts System.Drawing.Image object.
Now the preceding code is enough to generate a simple document that has 1 section (a page in the document) and a very basic paragraph followed by an image. Please look into Windows Forms image controls for more on the System.Drawing.Image object.

Saving the document

The final stage is saving the document on the file system, or processing it for further processes. Usually, web developers would want to send dynamically generated Word or PDF files to their clients at runtime. Spire.Doc is a great way to complete this task.

Spire.Doc exposes functions to store the generated document in a couple of formats, you can use just one function, the type and you're done! See the following (continuing the previous example of document):
  1. // Continuing from the previous example  
  2. document.SaveToFile("MyNewFile.pdf", FileFormat.PDF); // extension required
The preceding code would get the details from the document object and would generate a PDF file with the name MyNewFile. You need to understand that in this function you need to write the file extension also, otherwise it would generate a plain file in your file system.

Plus: The library also allows you to get results in stream and image formats. You should try them also!

Tested sample: The following code is a sample example of what I explained previously (using the library for applications to generate PDFs and to send them to the clients at runtime using an SMTP server for emails.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using Spire.Doc;  
  7. using Spire.Doc.Documents;  
  8. using Spire.CompoundFile;  
  9. using System.Net;  
  10. using System.Net.Mail;  
  11. namespace SpireConsoleApplication   
  12. {  
  13.     class Program {  
  14.         static void Main(string[] args)   
  15.         {  
  16.             // Create the document  
  17.             var document = new Document();  
  18.             // Add a section to document  
  19.             var section = document.AddSection();  
  20.             // Add paragraph to the section  
  21.             var paragraph = section.AddParagraph();  
  22.             paragraph.AppendText("Love for all, hatred for none!");  
  23.             paragraph.AppendPicture(  
  24.             System.Drawing.Image.FromFile("<location>.jpg"));  
  25.             document.SaveToFile("<location>.pdf", FileFormat.PDF);  
  26.             new Program().SendEmail("<location>.pdf");  
  27.         }  
  28.         private void SendEmail(string fileName)   
  29.         {  
  30.             using(SmtpClient sc = new SmtpClient("smtp.gmail.com", 25))   
  31.             {  
  32.                 sc.Credentials = new NetworkCredential("<username>""<word>");  
  33.                 sc.EnableSsl = true;  
  34.                 var message = new MailMessage(  
  35.                     "<from>""<recipient>",  
  36.                     "Spire.Doc PDF file""Email contains the attachment.");  
  37.                 message.Attachments.Add(new Attachment(fileName));  
  38.                 sc.Send(message);  
  39.             }  
  40.         }  
  41.     }  
  42. }
The preceding program would generate the PDF, then send the PDF to the client that is hard-coded in this example. You can use variables for giving out emails to the clients required.

The following is the email received:


Figure 1: Receiving the mail

The email with the attachment generated at runtime in PDF format using the Spire.Doc library.

Perhaps this now shows how simple it can be use the library to generate runtime PDF documents and send them to client. Yes, I love the Cut the Rope game and the Om Nom character so I used its picture to be sent. The library can be used in WPF, Windows Forms, ASP.NET or other .NET Framework applications, to generate the files at runtime, then they can be sent to the clients, opened as files on the desktop or shown to the users. I have personally enjoyed using it, ignore the intuition problems that I said.

Pro tip: You can convert a Microsoft Word document to HTML format to show it in your web application also. So it also solves the “How to show Microsoft Word document in website” problem.

Note: I did not use the license, you should use the license to remove the Red watermark on the documents.

Review Excerpt

I would quote what I was saying to myself (when I was using the library to test the quality.

Spire.Doc is a very fast and efficient solution to enterprise applications for generating office documents in desktop and web applications on .NET Framework. It removes the pain of interoperability from minds of .NET developers and provides a very agile solution to developers. It is a very easy, recommended and efficient solution for cross-file format document generation.

References

The library is available on E-Iceblue's website, you can also install the packages by executing the NuGet command in the NuGet package manager console.

PM> Install-Package Spire.Doc

The packages (and others by E-Iceblue for PDF and Excel based solutions) are available on the E-Iceblue's account on NuGet.

You can also download the product from their website to use them in your own applications. I would leave the rest of the application and resources up to you. Happy coding.


Similar Articles