Skip to content

Commit

Permalink
Use system look and feel, Detect system memory
Browse files Browse the repository at this point in the history
If low memory, disable caching
  • Loading branch information
dangowans committed Dec 14, 2018
1 parent 1866f24 commit 5f20fb1
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
Binary file added bin/ca/saultstemarie/pdfflattener/FlattenPDF$1.class
Binary file not shown.
Binary file modified bin/ca/saultstemarie/pdfflattener/FlattenPDF.class
Binary file not shown.
49 changes: 44 additions & 5 deletions src/ca/saultstemarie/pdfflattener/FlattenPDF.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.filechooser.FileNameExtensionFilter;

import org.apache.pdfbox.cos.COSObject;
import org.apache.pdfbox.io.MemoryUsageSetting;
import org.apache.pdfbox.pdmodel.DefaultResourceCache;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.graphics.PDXObject;
import org.apache.pdfbox.pdmodel.graphics.image.LosslessFactory;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.apache.pdfbox.rendering.ImageType;
Expand All @@ -25,8 +30,16 @@ public class FlattenPDF {
public static void main(String[] args) {

// Settings

System.setProperty("sun.java2d.cmm", "sun.java2d.cmm.kcms.KcmsServiceProvider");

try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
// ignore and use default
}

/*
* Get the source file
*/
Expand Down Expand Up @@ -104,12 +117,32 @@ public static void main(String[] args) {
*/
public static int flattenPDF (File sourceFile, File destinationFile) {

long startMillis = System.currentTimeMillis();

PDDocument sourceDoc = null;
PDDocument destDoc = new PDDocument();

try {

sourceDoc = PDDocument.load(sourceFile);

long maxAvailableMemoryInMB = Runtime.getRuntime().maxMemory() / 1024 / 1024;

// If less than 1GB available, be more memory conscious
if (maxAvailableMemoryInMB < 2048) {

log("Max memory limited to " + maxAvailableMemoryInMB + "MB. Resource cache will be disabled.");

sourceDoc = PDDocument.load(sourceFile, MemoryUsageSetting.setupTempFileOnly());

sourceDoc.setResourceCache(new DefaultResourceCache() {
public void put (COSObject indirect, PDXObject xobject) {
// discard
}
});
}
else {
sourceDoc = PDDocument.load(sourceFile);
}

PDFRenderer pdfRenderer = new PDFRenderer(sourceDoc);

final int pageCount = sourceDoc.getDocumentCatalog().getPages().getCount();
Expand All @@ -134,8 +167,14 @@ public static int flattenPDF (File sourceFile, File destinationFile) {

log(" Image added successfully.");

/*
* Close and clear images
*/

imagePageContentStream.close();

imgObj = null;

img.flush();
img = null;
}
Expand All @@ -147,7 +186,7 @@ public static int flattenPDF (File sourceFile, File destinationFile) {
* Remove links to the source document before saving.
* (Get back as much memory as possible.)
*/

pdfRenderer = null;

sourceDoc.close();
Expand All @@ -162,10 +201,10 @@ public static int flattenPDF (File sourceFile, File destinationFile) {
destDoc.save(destinationFile);
destDoc.close();

log("Saved successfully.");
log("Saved successfully (" + ((System.currentTimeMillis() - startMillis) / 1000.0) + " seconds).");
}
catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
log("Error: " + e.getMessage());
return 1;
}
finally {
Expand Down

0 comments on commit 5f20fb1

Please sign in to comment.