Summary of methods on how to convert Word to PDF in C#
This article mainly summarizes the C# method of converting Word into PDF, two solutions based on Office and WPS, which have certain reference value. Interested readers Friends, please refer to
Sometimes, we need to preview word documents online. Of course, we can use NPOI to extract text and tables from Word, and then display them on the web page, but this will The original formatting and pictures in Word are lost. A better way is to convert word to pdf and then let customers preview it. Let's take a look at two solutions based on Office and WPS.
1. Office-based solution
As the title says, Office-based requires installation of Office on the server . We call the COM interface through C# code to convert Word into PDF. Let’s take a look at the specific implementation. First reference Microsoft.Office.Interop.Word.dll, and then write the following code:
public bool WordToPDF(string sourcePath, string targetPath) { bool result = false; Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application(); Document document = null; try { application.Visible = false; document = application.Documents.Open(sourcePath); document.ExportAsFixedFormat(targetPath, WdExportFormat.wdExportFormatPDF); result = true; } catch (Exception e) { Console.WriteLine(e.Message); result = false; } finally { document.Close(); } return result; }
2. Based on WPS solution
#The biggest advantage of WPS is of course that it is free and it is small in size. To convert Word to PDF, of course, this requires WPS to be installed on the server. We still call the COM interface, and then write the following code:
public bool WordToPdfWithWPS(string sourcePath, string targetPath) { WPS.ApplicationClass app = new WPS.ApplicationClass(); WPS.Document doc = null; try { doc = app.Documents.Open(sourcePath, true, true, false, null, null, false, "", null, 100, 0, true, true, 0, true); doc.ExportPdf(targetPath, "", ""); } catch (Exception ex) { Console.WriteLine(ex.Message); return false; } finally { doc.Close(); } return true; }
3. Enterprise-level solution
For large enterprises, there are often multiple servers, and it is impossible to install Office or WPS on each server, or the company simply does not want to install these useless software on the server. . What should we do at this time? After all, installing these software on the server is a waste of resources.
Of course, the function still needs to be implemented, so how to solve it? In fact, we can install ofiice or WPS software on a server, and then deploy WebService such as WCF service or remoting. Other servers can call this service to convert Word to PDF.
The above is the detailed content of Summary of methods on how to convert Word to PDF in C#. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Guide to C# Serialization. Here we discuss the introduction, steps of C# serialization object, working, and example respectively.

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.
