Last active
March 2, 2023 06:44
-
-
Save basinilya/f9bc604fc2be4ec5a66a919703cfe7ec to your computer and use it in GitHub Desktop.
Best effort to copy everything from a secured PDF to a new PDF
This file contains hidden or 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.foo.unsecure; | |
| import java.io.FileOutputStream; | |
| import java.util.HashMap; | |
| import java.util.List; | |
| import com.itextpdf.text.Document; | |
| import com.itextpdf.text.pdf.PdfCopy; | |
| import com.itextpdf.text.pdf.PdfDictionary; | |
| import com.itextpdf.text.pdf.PdfName; | |
| import com.itextpdf.text.pdf.PdfObject; | |
| import com.itextpdf.text.pdf.PdfPageLabels; | |
| import com.itextpdf.text.pdf.PdfPageLabels.PdfPageLabelFormat; | |
| import com.itextpdf.text.pdf.PdfReader; | |
| import com.itextpdf.text.pdf.SimpleBookmark; | |
| import com.itextpdf.text.pdf.internal.PdfViewerPreferencesImp; | |
| /** | |
| * Best effort to copy everything from a secured PDF to a new PDF | |
| * | |
| * <pre> | |
| * {@code | |
| <dependency> | |
| <groupId>com.itextpdf</groupId> | |
| <artifactId>itextpdf</artifactId> | |
| <version>5.5.0</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.bouncycastle</groupId> | |
| <artifactId>bcprov-jdk15on</artifactId> | |
| <version>1.70</version> | |
| </dependency> | |
| * } | |
| * </pre> | |
| */ | |
| public class Unsecure { | |
| public static void main(final String[] args) throws Exception { | |
| PdfReader.unethicalreading = true; // allow extract everything | |
| final PdfReader reader = new PdfReader(args[0]); | |
| final FileOutputStream os = new FileOutputStream(args[1]); | |
| final Document document = new Document(reader.getPageSizeWithRotation(1)); | |
| // preserve Roman page numbers | |
| PdfPageLabels pageLabels = null; | |
| final PdfPageLabelFormat[] pageLabelFormats = PdfPageLabels.getPageLabelFormats(reader); | |
| if (pageLabelFormats != null) { | |
| pageLabels = new PdfPageLabels(); | |
| for (final PdfPageLabelFormat format : pageLabelFormats) { | |
| pageLabels.addPageLabel(format); | |
| } | |
| } | |
| // preserve bookmarks | |
| final List<HashMap<String, Object>> bookmarks = SimpleBookmark.getBookmark(reader); | |
| // preserve title | |
| final PdfObject title = reader.getTrailer().getAsDict(PdfName.INFO).get(PdfName.TITLE); | |
| // preserve ViewerPreferences | |
| final int simpleVp = reader.getSimpleViewerPreferences(); | |
| final PdfViewerPreferencesImp vp = PdfViewerPreferencesImp.getViewerPreferences(reader | |
| .getCatalog()); | |
| final PdfCopy copy = new PdfCopy(document, os); | |
| document.open(); | |
| // copy automatically | |
| copy.addDocument(reader); | |
| copy.setOutlines(bookmarks); | |
| if (pageLabels != null) { | |
| copy.setPageLabels(pageLabels); | |
| } | |
| if (title != null) { | |
| copy.getInfo().put(PdfName.TITLE, title); | |
| } | |
| copy.setViewerPreferences(simpleVp); | |
| final PdfDictionary vpdict = vp.getViewerPreferences(); | |
| for (final PdfName key : vpdict.getKeys()) { | |
| final PdfObject value = vpdict.get(key); | |
| copy.addViewerPreference(key, value); | |
| } | |
| document.close(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment