From 1ed9b8800d27fb7e20f68965ea421c548ebfa579 Mon Sep 17 00:00:00 2001 From: Pavel Alay Date: Fri, 11 Jan 2019 10:53:57 +0300 Subject: [PATCH] =?UTF-8?q?=EF=BB=BFSet=20unethical=20flag=20by=20default?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/itextpdf/rups/Rups.java | 2 +- .../rups/controller/RupsController.java | 26 ++++++++++++++++--- .../java/com/itextpdf/rups/model/PdfFile.java | 22 ++++++++++++---- .../rups/view/itext/FormTreeTest.java | 3 ++- 4 files changed, 42 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/itextpdf/rups/Rups.java b/src/main/java/com/itextpdf/rups/Rups.java index c714814b..5098496a 100644 --- a/src/main/java/com/itextpdf/rups/Rups.java +++ b/src/main/java/com/itextpdf/rups/Rups.java @@ -101,7 +101,7 @@ public void run() { static void initApplication(JFrame frame, RupsController controller, final int onCloseOperation) { // title bar - frame.setTitle("iText RUPS " + ITextCoreProductData.getInstance().getVersion()); + frame.setTitle("☠️ iText RUPS " + ITextCoreProductData.getInstance().getVersion()); frame.setIconImages(FrameIconUtil.loadFrameIcons()); frame.setDefaultCloseOperation(onCloseOperation); // the content diff --git a/src/main/java/com/itextpdf/rups/controller/RupsController.java b/src/main/java/com/itextpdf/rups/controller/RupsController.java index 726b1d07..1a76adc0 100644 --- a/src/main/java/com/itextpdf/rups/controller/RupsController.java +++ b/src/main/java/com/itextpdf/rups/controller/RupsController.java @@ -44,8 +44,13 @@ This file is part of the iText (R) project. import com.itextpdf.kernel.actions.data.ITextCoreProductData; import com.itextpdf.kernel.exceptions.PdfException; +import com.itextpdf.kernel.pdf.CompressionConstants; import com.itextpdf.kernel.pdf.PdfDocument; +import com.itextpdf.kernel.pdf.PdfName; +import com.itextpdf.kernel.pdf.PdfNumber; +import com.itextpdf.kernel.pdf.PdfObject; import com.itextpdf.kernel.pdf.PdfReader; +import com.itextpdf.kernel.pdf.PdfStream; import com.itextpdf.kernel.utils.CompareTool; import com.itextpdf.rups.event.*; import com.itextpdf.rups.model.*; @@ -304,7 +309,7 @@ public void loadRawContent(byte[] contents, String fileName, File directory, boo startObjectLoader(); if (!pluginMode) { String directoryPath = directory == null ? "" : directory.getCanonicalPath() + File.separator; - ownedFrame.setTitle("iText RUPS - " + directoryPath + fileName + " - " + ITextCoreProductData.getInstance().getVersion()); + ownedFrame.setTitle("☠️ iText RUPS - " + directoryPath + fileName + " - " + ITextCoreProductData.getInstance().getVersion()); } readerController.getParser().setDocument(pdfFile.getPdfDocument()); } catch (IOException | PdfException | com.itextpdf.io.exceptions.IOException ioe) { @@ -406,14 +411,27 @@ public void closeRoutine() { setChanged(); super.notifyObservers(new CloseDocumentEvent()); if (docToClose != null) { + docToClose.getWriter().setCompressionLevel(0); + uncompressStreams(docToClose); docToClose.close(); } if (!pluginMode) { - ownedFrame.setTitle("iText RUPS " + ITextCoreProductData.getInstance().getVersion()); + ownedFrame.setTitle("☠️ iText RUPS " + ITextCoreProductData.getInstance().getVersion()); } readerController.getParser().setDocument(null); } + private static void uncompressStreams(PdfDocument document) { + PdfNumber num = document.getTrailer().getAsNumber(PdfName.Size); + int xrefSize = num != null ? num.intValue() : 64000; + for (int i = 1; i < xrefSize; i++) { + PdfObject object = document.getPdfObject(i); + if (object != null && object.isStream()) { + ((PdfStream) object).setCompressionLevel(CompressionConstants.NO_COMPRESSION); + } + } + } + public CompareTool.CompareResult compareWithDocument(PdfDocument document) { if (getPdfFile() == null || getPdfFile().getPdfDocument() == null) { LoggerHelper.warn(LoggerMessages.NO_OPEN_DOCUMENT, getClass()); @@ -429,7 +447,7 @@ public CompareTool.CompareResult compareWithDocument(PdfDocument document) { } public CompareTool.CompareResult compareWithFile(File file) { - try (PdfReader readerPdf = new PdfReader(file.getAbsolutePath()); + try (PdfReader readerPdf = new PdfReader(file.getAbsolutePath()).setUnethicalReading(true); PdfDocument cmpDocument = new PdfDocument(readerPdf)) { return compareWithDocument(cmpDocument); } catch (IOException e) { @@ -439,7 +457,7 @@ public CompareTool.CompareResult compareWithFile(File file) { } public CompareTool.CompareResult compareWithStream(InputStream is) { - try (PdfReader reader = new PdfReader(is); + try (PdfReader reader = new PdfReader(is).setUnethicalReading(true); PdfDocument cmpDocument = new PdfDocument(reader)) { reader.setCloseStream(false); return compareWithDocument(cmpDocument); diff --git a/src/main/java/com/itextpdf/rups/model/PdfFile.java b/src/main/java/com/itextpdf/rups/model/PdfFile.java index 6dfa6ed8..79f02959 100644 --- a/src/main/java/com/itextpdf/rups/model/PdfFile.java +++ b/src/main/java/com/itextpdf/rups/model/PdfFile.java @@ -44,16 +44,25 @@ This file is part of the iText (R) project. import com.itextpdf.kernel.exceptions.BadPasswordException; import com.itextpdf.kernel.exceptions.PdfException; +import com.itextpdf.kernel.pdf.CompressionConstants; import com.itextpdf.kernel.pdf.PdfDocument; import com.itextpdf.kernel.pdf.PdfReader; import com.itextpdf.kernel.pdf.PdfWriter; import com.itextpdf.kernel.pdf.ReaderProperties; +import com.itextpdf.kernel.pdf.WriterProperties; -import com.ibm.icu.text.StringPrepParseException; import com.ibm.icu.text.StringPrep; -import javax.swing.*; -import java.io.*; +import com.ibm.icu.text.StringPrepParseException; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.UnsupportedEncodingException; import java.nio.charset.StandardCharsets; +import javax.swing.JOptionPane; +import javax.swing.JPasswordField; /** * Wrapper for both iText's PdfReader (referring to a PDF file to read) @@ -186,12 +195,15 @@ protected void readFile(InputStream fis, boolean checkPass, boolean readOnly) th } else { password = null; } - reader = new PdfReader(fis, readerProps); + reader = new PdfReader(fis, readerProps).setUnethicalReading(true); baos = new ByteArrayOutputStream(); if (readOnly) { document = new PdfDocument(reader); } else { - writer = new PdfWriter(baos); + //TODO: change writer mechanism + writer = new PdfWriter(baos, + new WriterProperties().setCompressionLevel(CompressionConstants.NO_COMPRESSION) + .setFullCompressionMode(false)); document = new PdfDocument(reader, writer); } // we have some extra work to do if the document was encrypted diff --git a/src/test/java/com/itextpdf/rups/view/itext/FormTreeTest.java b/src/test/java/com/itextpdf/rups/view/itext/FormTreeTest.java index c7e8a674..912542ba 100644 --- a/src/test/java/com/itextpdf/rups/view/itext/FormTreeTest.java +++ b/src/test/java/com/itextpdf/rups/view/itext/FormTreeTest.java @@ -46,6 +46,7 @@ This file is part of the iText (R) project. import com.itextpdf.kernel.pdf.PdfName; import com.itextpdf.kernel.pdf.PdfObject; import com.itextpdf.kernel.pdf.PdfReader; +import com.itextpdf.kernel.pdf.ReaderProperties; import com.itextpdf.rups.controller.PdfReaderController; import com.itextpdf.rups.model.IndirectObjectFactory; import com.itextpdf.rups.model.TreeNodeFactory; @@ -68,7 +69,7 @@ public class FormTreeTest extends ExtendedITextTest { public void testLoadXfa() throws IOException { File inPdf = new File(sourceFolder + "cmp_purchase_order_filled.pdf"); - PdfDocument pdfDocument = new PdfDocument(new PdfReader(inPdf)); + PdfDocument pdfDocument = new PdfDocument(new PdfReader(inPdf).setUnethicalReading(true)); IndirectObjectFactory indirectObjectFactory = new IndirectObjectFactory(pdfDocument); while (indirectObjectFactory.storeNextObject());