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