Created
April 20, 2024 09:51
-
-
Save highel/f77f265fdc1ca0223fd71a4ebce87bff to your computer and use it in GitHub Desktop.
Command line tool to remove unneeded PDF pages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.example; | |
import com.lowagie.text.pdf.PdfReader; | |
import com.lowagie.text.pdf.PdfStamper; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.nio.file.InvalidPathException; | |
import java.nio.file.Path; | |
//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or | |
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter. | |
public class Main { | |
public static void main(String[] args) { | |
System.out.println("Добро пожаловать! " + String.join(";", args)); | |
if (args.length < 1) { | |
System.err.println("Пожалуйста, укажите имя файла"); | |
System.exit(-1); | |
} | |
String filename = args[0]; | |
System.out.println("Проверяем наличие файла {" + filename + "}"); | |
Path filePath = null; | |
Path newPath = null; | |
try { | |
filePath = Path.of(filename); | |
newPath = filePath.resolveSibling(filePath.getFileName() + ".strip.pdf"); | |
} catch (InvalidPathException e) { | |
System.err.println("Не удалось распознать путь {" + filename+"}"); | |
System.exit(-1); | |
} | |
if (!Files.exists(filePath)) { | |
System.err.println("Файл не существует: {" + filePath+"}"); | |
System.exit(-1); | |
} | |
System.out.println("Найден файл {" + filePath.toAbsolutePath() + "}\nНовый файл будет записан в {" + newPath.toAbsolutePath() + "}"); | |
if (Files.exists(newPath)) { | |
System.err.println("Файл уже существует {" + newPath.toAbsolutePath() + "}"); | |
System.exit(-1); | |
} | |
int pageNumberFrom = 1; | |
if (args.length > 2) { | |
try { | |
pageNumberFrom = Integer.parseInt(args[2].trim()); | |
} catch (NumberFormatException e) { | |
System.err.print("Некорректно указан номер страницы"); | |
System.exit(-1); | |
} | |
} | |
System.out.println("Читаем файл {" + filename + "}"); | |
try { | |
try (PdfReader pdfReader = new PdfReader(filePath.toString()); | |
FileOutputStream fos = new FileOutputStream(newPath.toFile()); | |
PdfStamper pdfStamper = new PdfStamper(pdfReader, fos);) { | |
pdfReader.selectPages((pageNumberFrom + 1) + "-" + pdfReader.getNumberOfPages()); | |
} | |
System.out.println("Записан файл {" + newPath + "}"); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
System.err.print("Не удалось прочитать файл " + filename); | |
System.exit(-1); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>org.example</groupId> | |
<artifactId>pdf</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<dependencies> | |
<dependency> | |
<groupId>com.github.librepdf</groupId> | |
<artifactId>openpdf</artifactId> | |
<version>2.0.2</version> | |
</dependency> | |
</dependencies> | |
<properties> | |
<maven.compiler.source>20</maven.compiler.source> | |
<maven.compiler.target>20</maven.compiler.target> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
</properties> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment