View Word as PDF using C#

The ability to convert documents to PDF is almost a required skill in this digital age. PDF being the universally accepted standard, we often need to convert Word processing documents into this portable format while sharing. In this article, we’ll be looking into the programmatic rendering of Word documents to view as PDF using C#. Additionally, we will see how to transform these word-processing documents into secured PDFs using C# within the .NET application.

Word & PDF Viewer – .NET API

GroupDocs.Viewer for .NET is an API that allows developers to create powerful .NET-based applications. The API enables the rendering of documents into PDF, HTML, and image formats within the application. In this article, we will demonstrate how to use it to view Word documents as PDFs.

The API can be downloaded as DLLs or an MSI installer from the download section or installed in your .NET application using NuGet.

PM> Install-Package GroupDocs.Viewer

How to View Word Documents as PDF using C#

Let’s start with the default rendering options. Here are the steps that show how you can transform a Word document into a PDF with just a few lines of C# code.

  • Load the Word document using the Viewer class.
  • Prepare the default PDF View Options.
  • Invoke the View method to save the loaded Word Document as PDF.

Here is the C# code that renders the Word document as a PDF file:

The following is the output of the above code:

Word as PDF

Word Document as Password-Protected PDF with Disabled Printing Option using C#

Here, you will find the answer to your following two questions in just a few lines of code:

  1. How to convert a Word Document into a Password-Protected PDF ?
  2. How to make a PDF with having Disabled Printing Option ?

These steps will guide you on how to transform the Word document into a protected PDF having limited permissions using C#:

  • Load the Word (DOC/DOCX) document using the Viewer class.
  • Prepare the security settings using the Security class.
    • Restrict access to the document by setting DocumentOpenPassword.
    • Restrict PDF printing, modification, and data extraction by setting PermissionsPassword.
    • Allow / Deny permissions by setting the Permissions attribute.
  • Now set the PDF View Options and add the security settings to it.
  • Call the View method to view the loaded Word document as a password-protected PDF with limited permissions.

The following C# code renders a Word document as a Protected PDF file having no printing permissions:

Password-Protected PDF

Now, every time the PDF is opened, it will ask you to enter the password to open it.

Enter Password for Secured PDF

PDF with Disabled Printing Option

As per the set permission during the conversion, the output PDF will have limited permissions. The user will not be able to print this PDF file due to disabled printing options.

Word as Protected PDF

Get Free Temporary License

You can get a free temporary license to try the library without evaluation limitations.

Conclusion

In summary, we have explored how to use C# to programmatically view and convert Word documents into PDFs, including the option to password-protect the resulting PDF. We also learned to restrict PDF file printing by settings its permissions. With this knowledge, you can now easily create your own Word and PDF viewer application similar to the GroupDocs.Viewer Online App.

To gain further understanding of GroupDocs.Viewer for .NET, consult its documentation. Should you have any questions or inquiries, reach out to us through the forum.

See Also