You sometimes need to convert the source code files to other formats. It may be for sharing or analysis purposes. This article discusses how to convert Python, PHP, Java, C#, C/C++ source code files to PDF format within the .NET applications. Furthermore, we will programmatically password-protect the converted files.
- Source Code Conversion .NET API
- Java to PDF using C#
- Python to PDF using C#
- PHP to PDF - Protect the PDF file
.NET API for Source Code Conversion
GroupDocs.Viewer for .NET is the document viewer API and allows rendering the documents into PDF, HTML, and images with the .NET application. Today, we will use this API in examples to convert the source code files of different languages into PDF format.
PM> Install-Package GroupDocs.Viewer
Convert Java Code to PDF using C#
No need to involve in complex configurations, just load the Java file, and transform it into PDF. The following steps guide you on how to convert the Java source code file to PDF using C#.
- Load the Java file using the Viewer class.
- Set the output file and its options using the PdfViewOptions class.
- Convert the file to PDF using the appropriate View() method.
The following C# example converts the complete Java source code file to PDF format.
Here is the converted PDF of the Java file with highlighting using the above C# code. You can also add security to the converted PDF file. To learn about securing the files, jump below where the PHP files are converted.
Convert Python Code to PDF using C#
Why change the code if your source file format is changed? Let the API bear this pain. Just provide the .py file to the right method. The following steps show how to convert the Python code to PDF using C#.
- Load the Python source file using the Viewer class.
- Define the output file path and configurations using the PdfViewOptions class.
- Convert the .py file to PDF using the right View() method.
The following C# code example converts the Python source code file into PDF format.
Convert PHP to PDF using C#
Similarly, you can also convert the PHP files. Additionally, while converting your source code files, you can add security to the PDF files. Let’s secure the code when it is converted. The following steps show the conversion of PHP files to PDF format with security in C#.
- Load the PHP file using the Viewer class.
- Define the intended security of the PDF file using the Security class.
- Set the passwords for opening and editing the resultant file.
- Define the output file using the PdfViewOptions class.
- Call the View() method to render the loaded PHP file into the protected PDF.
The following C# code snippet converts the PHP source code file into a password-protected PDF file.
Likewise, you can use this code for the source codes files of other supported programming languages like C#, C/C++, JS, Ruby, and more.
Get a Free API License
You can get a free temporary license to use the API without the evaluation limitations.
To sum up, we learned to convert the source code files of various programming languages into PDF format using C#. The examples have shown the conversion of Java, Python, and PHP files to PDF format. Furthermore, we learned to secure the resultant PDF file. Using this API, you can start building your own source code viewer .NET application.