If you want to share your code snippets, you can convert them into PDF format. In order to do this, we will discuss how to convert Java, PHP, Python, C#, C++ source code to PDF format within Java application. Additionally, we will also learn to password-protect the converted PDF files.
The following topics are discussed below:
- Source Code Conversion Java API
- Java to PDF using Java
- Python to PDF using Java
- PHP to PDF – Protect the PDF file
Source Code Converter Java API
GroupDocs.Viewer provides Java API to render files into different formats like PDF, HTML, and image formats. I will use this API to convert source code files of different languages into PDF format.
You can download the JAR file from the downloads section or use the latest repository and dependency Maven configurations within your Java applications.
<repository>
<id>GroupDocsArtifactRepository</id>
<name>GroupDocs Artifact Repository</name>
<url>https://repository.groupdocs.com/repo/</url>
</repository>
<dependency>
<groupId>com.groupdocs</groupId>
<artifactId>groupdocs-viewer</artifactId>
<version>21.11</version>
</dependency>
Convert Java Code to PDF
Let’s quickly come to the objective and see the results. The following steps allow you to convert the Java source code file to PDF.
- Load the Java file using the Viewer class.
- Define the output file and options using the PdfViewOptions class.
- Convert the loaded Java file to PDF using the appropriate view() method.
The following Java code snippet converts the complete Java source code file to PDF format.
Here is the converted PDF of the Java file using the above Java code. If you want to add security to the resultant PDF file, jump to the section below where the PHP file gets converted.
Convert Python Code to PDF using Java
Do you really think, there will be a separate code for converting the Python code to PDF and it will be somewhat different from converting a Java file? No, it’s the same, just hand over the right .py file. The following steps allow you to convert the Python code to PDF.
- Load the Python file using the Viewer class.
- Set the output file path and options using the PdfViewOptions class.
- Convert the loaded .py file to PDF using the suitable view() method.
The following Java code snippet converts the complete Python source code file into PDF format.
Convert PHP to PDF using Java
Similarly, you can also convert the PHP files. One more thing to add here; while converting your source code files, you can add security to the PDF files. Let’s protect the PHP code when it is converted to PDF format. The following steps show how to convert the PHP file to PDF.
- Load the PHP file using the Viewer class.
- Define the security using Security class.
- Set the passwords for opening and editing the resultant PDF file.
- Define the output file and its options using the PdfViewOptions class.
- Call the view() method to render the loaded PHP file to the protected PDF.
The following Java code example converts the PHP source code file to a password-protected PDF file.
Similarly, you can use this code for other source codes of supported programming languages like C#, C/C++, GROOVY, Ruby, and more.
Get a Free API License
You can get a free temporary license to use the API without the evaluation limitations.
Conclusion
To conclude, we learned to convert source code files of different programming languages to PDF format using Java. We converted the Java, Python, and PHP files into PDF format. Additionally, we learned how to password-protect the resultant PDF file. Using this API, you can start building your own source code viewer Java application.
To learn more about GroupDocs.Viewer, visit the documentation. For queries, contact us via the forum.