Skip to content

Instantly share code, notes, and snippets.

@jittagornp
Created January 27, 2018 07:27
Show Gist options
  • Save jittagornp/6e15b8e6d8b46433e82ced2d4f9c7ffd to your computer and use it in GitHub Desktop.
Save jittagornp/6e15b8e6d8b46433e82ced2d4f9c7ffd to your computer and use it in GitHub Desktop.
/*
* Copyright 2018 Pamarin.com
*/
package com.pamarin.util;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author jittagornp <http://jittagornp.me> create : 2018/01/26
*/
public class PdfSizeUtils {
private static final Logger LOG = LoggerFactory.getLogger(PdfSizeUtils.class);
private static final float A4_WIDTH = PageSize.A4.getWidth();
private static final float A4_HEIGHT = PageSize.A4.getHeight();
private PdfSizeUtils() {
}
private static boolean isA4(float width, float height) {
return (width == A4_WIDTH && height == A4_HEIGHT);
}
public static boolean isA4(File srcPdf) {
PdfReader reader = null;
try (InputStream intputStream = new FileInputStream(srcPdf)) {
reader = new PdfReader(intputStream);
Rectangle pageSize = reader.getPageSize(1);//first page
boolean isA4 = isA4(pageSize.getWidth(), pageSize.getHeight());
LOG.debug("PDF \"{}\"", srcPdf.getAbsolutePath());
LOG.debug("Size {}x{} (A4 : {}x{}) is A4 => {}",
pageSize.getWidth(),
pageSize.getHeight(),
A4_WIDTH,
A4_HEIGHT,
isA4
);
return isA4;
} catch (IOException ex) {
throw new UncheckedIOException(ex);
} finally {
if (reader != null) {
reader.close();
}
}
}
public static File convertToA4(File srcPdf, File destFile) {
try (InputStream intputStream = new FileInputStream(srcPdf);
OutputStream outputStream = new FileOutputStream(destFile)) {
PdfReader reader = null;
PdfWriter writer = null;
Document document = null;
try {
reader = new PdfReader(intputStream);
document = new Document(new Rectangle(A4_WIDTH, A4_HEIGHT));
writer = PdfWriter.getInstance(document, outputStream);
document.open();
PdfContentByte content = writer.getDirectContent();
int numberOfPages = reader.getNumberOfPages();
for (int i = 1; i <= numberOfPages; i++) {
PdfImportedPage page = writer.getImportedPage(reader, i);
float scale = getA4Scale(page);
content.addTemplate(page, scale, 0, 0, scale, 0, 0);
document.newPage();
}
} finally {
if (document != null) {
document.close();
}
if (writer != null) {
writer.close();
}
if (reader != null) {
reader.close();
}
}
return destFile;
} catch (DocumentException | IOException ex) {
throw new RuntimeException(ex);
}
}
private static float getA4Scale(PdfImportedPage page) {
float scaleX = A4_WIDTH / page.getWidth();
float scaleY = A4_HEIGHT / page.getHeight();
return Math.min(scaleX, scaleY);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment