From 417879d970eace849e484a2c90baa4c0df4d770d Mon Sep 17 00:00:00 2001
From: Quantum <quantum2048@gmail.com>
Date: Sat, 16 Mar 2019 17:48:17 -0400
Subject: [PATCH] Initial commit

---
 .gitignore                                    | 41 +++++++++++++
 pom.xml                                       | 30 ++++++++++
 .../xyz/quantum2/PDFSignatureRemover.java     | 59 +++++++++++++++++++
 src/main/resources/META-INF/MANIFEST.MF       |  2 +
 4 files changed, 132 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 pom.xml
 create mode 100644 src/main/java/xyz/quantum2/PDFSignatureRemover.java
 create mode 100644 src/main/resources/META-INF/MANIFEST.MF

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..00e6cae
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,41 @@
+### Java template
+# Compiled class file
+*.class
+
+# Log file
+*.log
+
+# BlueJ files
+*.ctxt
+
+# Mobile Tools for Java (J2ME)
+.mtj.tmp/
+
+# Package Files #
+*.jar
+*.war
+*.nar
+*.ear
+*.zip
+*.tar.gz
+*.rar
+
+# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
+hs_err_pid*
+
+### Maven template
+target/
+pom.xml.tag
+pom.xml.releaseBackup
+pom.xml.versionsBackup
+pom.xml.next
+release.properties
+dependency-reduced-pom.xml
+buildNumber.properties
+.mvn/timing.properties
+.mvn/wrapper/maven-wrapper.jar
+
+### IntelliJ
+.idea/
+out/
+*.iml
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..e00a0e0
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,30 @@
+<?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>PDFSignatureRemover</groupId>
+    <artifactId>PDFSignatureRemover</artifactId>
+    <version>1.0-SNAPSHOT</version>
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <configuration>
+                    <source>1.7</source>
+                    <target>1.7</target>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+
+    <dependencies>
+        <dependency>
+            <groupId>com.itextpdf</groupId>
+            <artifactId>itextpdf</artifactId>
+            <version>5.5.13</version>
+        </dependency>
+    </dependencies>
+</project>
diff --git a/src/main/java/xyz/quantum2/PDFSignatureRemover.java b/src/main/java/xyz/quantum2/PDFSignatureRemover.java
new file mode 100644
index 0000000..2eb9427
--- /dev/null
+++ b/src/main/java/xyz/quantum2/PDFSignatureRemover.java
@@ -0,0 +1,59 @@
+package xyz.quantum2;
+
+import com.itextpdf.text.DocumentException;
+import com.itextpdf.text.pdf.AcroFields;
+import com.itextpdf.text.pdf.PdfReader;
+import com.itextpdf.text.pdf.PdfStamper;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+public class PDFSignatureRemover {
+    private static void help() {
+        System.out.println("Usage: java -jar PDFSignatureRemover.jar <input PDF> <output PDF>");
+        System.exit(1);
+    }
+
+    private static void error(String error) {
+        System.err.println("Error when removing PDF signature: " + error);
+        System.exit(1);
+    }
+
+    public static void main(String... args) {
+        if (args.length != 2) {
+            help();
+        }
+
+        File input = new File(args[0]);
+        File output = new File(args[1]);
+
+        if (!input.isFile()) {
+            error("Input file not found: " + input);
+        }
+
+        PdfReader reader;
+        try {
+            reader = new PdfReader(input.toString());
+        } catch (IOException e) {
+            error("Failed to read input PDF: " + e);
+            return;
+        }
+
+        try {
+            AcroFields acroFields = reader.getAcroFields();
+            for (String name : acroFields.getFields().keySet()) {
+                acroFields.removeField(name);
+            }
+
+            try {
+                PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(output));
+                stamper.close();
+            } catch (IOException | DocumentException e) {
+                error("Failed to write output PDF: " + e);
+            }
+        } finally {
+            reader.close();
+        }
+    }
+}
diff --git a/src/main/resources/META-INF/MANIFEST.MF b/src/main/resources/META-INF/MANIFEST.MF
new file mode 100644
index 0000000..6bc9bef
--- /dev/null
+++ b/src/main/resources/META-INF/MANIFEST.MF
@@ -0,0 +1,2 @@
+Manifest-Version: 1.0
+Main-Class: xyz.quantum2.PDFSignatureRemover