This article takes Java code as an example to introduce how to convert a color PDF file into a grayscale (black and white) PDF file, that is, convert the color image or text in the PDF document into gray by calling the PdfGrayConverter.toGrayPdf() method. It can reduce the size of the document by adjusting the document without color effect. The following is the program running environment and code samples.
Configure the program environment
- IntelliJ IDEA
- Spire.PDF for Java 5.3.1
- PDF document
About how to import jar files:
1. Download from the Maven repository and configure Pom.xml as follows:
<repositories> <repository> <id>com.e-iceblue</id> <url>https: // repo.e-iceblue.cn/repository/maven-public/</url> </repository> </repositories > <dependencies> <dependency> <groupId> e-iceblue </groupId> <artifactId>spire.pdf</artifactId> <version>5.3.1</version> </dependency> </dependencies>
2. Manually add the jar
Download the jar package locally, then unzip it, and find Spire.Pdf.jar in the lib folder. Then open the following interface in IDEA, and add the jar file in the local path to the Java program.
Convert color PDF to grayscale
This conversion requires only the following two steps:
- Use the PdfGrayConverter class to load PDF files.
- Call the PdfGrayConverter.toGrayPdf() method to convert the PDF to grayscale.
Java
import com.spire.pdf.conversion.* ; public class ToGrayPDF { public static void main(String[] args) { // Create a PdfGrayConverter instance and load the PDF document PdfGrayConverter converter = new PdfGrayConverter("Booklet.pdf" ); / / Convert color PDF to grayscale converter.toGrayPdf("ToGray.pdf" ); converter.dispose(); } }
Conversion result:
Translated from https://www.cnblogs.com/Yesi/p/16038524.html
Read More:
- How to convert a Java string into a number (stringtonumber)
- JAVA 8: How to Convert List to Map
- Java: How to use itext to export PDF text absolute positioning (implementation method)
- [Solved] Failed to convert value of type ‘java.lang.String‘ to required type ‘java.util.Date‘;
- Java will convert Excel to list set or JSON, export excel file to local, excel import and export, easyexcel tool class
- [Solved] Read the resources resource and convert it to file error: java.io.filenotfoundexception
- [Solved] stream Convert list to map Error: java.lang.IllegalStateException: Duplicate key
- Caused by: java.lang.IllegalStateException (How to Fix)
- How to Solve IntelliJ IDEA Error: Cannot determine path to ‘tools.jar‘ library for 17 (C:\Program Files\Java\jd…
- How to Solve Hbase JAVA import Package Error
- How to Converte Java objects to jsonnode in Jackson (Four Methods)
- JAVA: How to Use Minio to upload pictures
- How to Solve java server error (java application Run Normally)
- How to Solve ERROR: Java 1.7 or later is required to run Apache Drill
- JAVA: How to Use Multipartfile to upload Files
- How to Close the Current Form in JAVA Swing
- [Solved] SpringBoot Date Convert Error: JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime`
- AlmaLinux 9.1: How to Install java11
- How to Solve Java Runtime (class file version 53.0)ERROR
- JAVA Error: Error attempting to get column ‘XXX’ from result set. Cause java.sql.