Replace iText Java PDF library with PDFBox. (#664)

This commit is contained in:
jmpenn 2018-09-04 16:59:06 -05:00 committed by GitHub
parent 3032aae3e4
commit c112290265
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 106 additions and 118 deletions

View File

@ -9,21 +9,22 @@
EXTERNAL_JARS = \
bsaf-1.9.2.jar \
itext-2.1.7.jar \
jcommon-1.0.23.jar \
jfreechart-1.0.19.jar \
jfreesvg-2.1.jar \
jopt-simple-4.8.jar \
junit-4.12.jar \
swingx-1.6.1.jar \
hamcrest-core-1.3.jar
hamcrest-core-1.3.jar \
pdfbox-2.0.11.jar \
fontbox-2.0.11.jar \
commons-logging-1.2.jar
external_jars : ${EXTERNAL_JARS}
bsaf-1.9.2.jar :
curl --retry 4 -O http://repo.maven.apache.org/maven2/org/jdesktop/bsaf/bsaf/1.9.2/bsaf-1.9.2.jar
itext-2.1.7.jar :
curl --retry 4 -O http://repo.maven.apache.org/maven2/com/lowagie/itext/2.1.7/itext-2.1.7.jar
jcommon-1.0.23.jar :
curl --retry 4 -O http://repo.maven.apache.org/maven2/org/jfree/jcommon/1.0.23/jcommon-1.0.23.jar
jfreechart-1.0.19.jar :
@ -38,6 +39,12 @@ swingx-1.6.1.jar :
curl --retry 4 -O http://repo.maven.apache.org/maven2/org/swinglabs/swingx/1.6.1/swingx-1.6.1.jar
hamcrest-core-1.3.jar :
curl --retry 4 -O http://repo.maven.apache.org/maven2/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar
pdfbox-2.0.11.jar :
curl --retry 4 -O http://repo.maven.apache.org/maven2/org/apache/pdfbox/pdfbox/2.0.11/pdfbox-2.0.11.jar
fontbox-2.0.11.jar :
curl --retry 4 -O http://repo.maven.apache.org/maven2/org/apache/pdfbox/fontbox/2.0.11/fontbox-2.0.11.jar
commons-logging-1.2.jar :
curl --retry 4 -O https://repo1.maven.org/maven2/commons-logging/commons-logging/1.2/commons-logging-1.2.jar
clean:
rm -f ${EXTERNAL_JARS}

View File

@ -40,12 +40,14 @@ import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.xml.sax.SAXException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.DefaultFontMapper;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
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.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.pdmodel.graphics.image.JPEGFactory;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import trick.common.TrickApplication;
import trick.common.ui.UIUtils;
@ -115,17 +117,7 @@ public class JXPlotApplication extends TrickApplication {
try {
int dpFileCount = productTreeRoot.getChildCount();
if (dpFileCount > 0) {
Document document = new Document(PageSize.A4);
PdfWriter writer = null;
PdfContentByte pdfContent = null;
try {
writer = PdfWriter.getInstance(document, new FileOutputStream(file));
document.open();
// this object can be reused which is used to write each page to the same PDF file
pdfContent = writer.getDirectContent();
} catch (DocumentException e) {
e.printStackTrace();
}
PDDocument document = new PDDocument();
for (int i = 0; i < dpFileCount; i++) {
DefaultMutableTreeNode eachDPFile = (DefaultMutableTreeNode)productTree.getModel().getChild(productTreeRoot, i);
int pageCount = eachDPFile.getChildCount();
@ -133,14 +125,12 @@ public class JXPlotApplication extends TrickApplication {
DefaultMutableTreeNode eachPage = (DefaultMutableTreeNode)productTree.getModel().getChild(eachDPFile, j);
if (eachPage.getUserObject() instanceof TrickChartFrame) {
TrickChartFrame theFrame = (TrickChartFrame)eachPage.getUserObject();
theFrame.writePDFPage(pdfContent, new DefaultFontMapper());
theFrame.writePDFPage(document);
}
}
}
// document needs to be closed after the PDF file is saved
if (document != null) {
document.close();
}
document.save(file);
}
} catch (IOException e) {
e.printStackTrace();

View File

@ -8,6 +8,7 @@ import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
@ -21,15 +22,16 @@ import javax.swing.JSeparator;
import org.jfree.chart.ChartPanel;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.DefaultFontMapper;
import com.lowagie.text.pdf.FontMapper;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;
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.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.pdmodel.graphics.image.JPEGFactory;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.jfree.chart.JFreeChart;
import trick.common.TrickApplication;
import trick.common.ui.UIUtils;
@ -38,7 +40,7 @@ import trick.dataproducts.trickqp.utils.ProductPage;
public class TrickChartFrame extends TrickFrame {
//========================================
//========================================
// Public data
//========================================
@ -95,7 +97,7 @@ public class TrickChartFrame extends TrickFrame {
}
//========================================
//========================================
// Methods
//========================================
/**
@ -179,100 +181,89 @@ public class TrickChartFrame extends TrickFrame {
*/
public void saveChartsAsPDF() throws IOException {
File file = UIUtils.chooseSaveFile(null, "plot_", "pdf", this);
if (file == null) {
return;
}
Document document = new Document(PageSize.A4);
PdfWriter writer = null;
PdfContentByte pdfContent = null;
try {
writer = PdfWriter.getInstance(document, new FileOutputStream(file));
document.open();
pdfContent = writer.getDirectContent();
} catch (DocumentException e) {
e.printStackTrace();
}
writePDFPage(pdfContent, new DefaultFontMapper());
if (document != null) {
document.close();
}
if (file != null) {
PDDocument document = new PDDocument(); // Default Paper size is US Letter.
writePDFPage(document);
document.save(file);
}
}
/**
* Writes all plots shown to one page of PDF.
*
* @param pdfContent an object containing the user positioned text and graphic contents of a page.
* @param mapper mapping between AWT fonts and PDF fonts.
* @param doc An Apache-PDFBox PDDocument object.
* @throws IOException IOException
*/
public void writePDFPage(PdfContentByte pdfContent, FontMapper mapper) throws IOException {
if (pdfContent == null) {
return;
}
Document document = pdfContent.getPdfDocument();
int chartLocOffset = 30;
try {
int pageWidth = (int)document.getPageSize().getWidth();
int pageHeight = (int)document.getPageSize().getHeight();
document.newPage();
public void writePDFPage(PDDocument doc) throws IOException {
PDPage pdfPage = new PDPage(); // Default page size is 'US Letter' (8.5x11 inches).
BaseFont pageTitleFont = BaseFont.createFont(BaseFont.HELVETICA_BOLD, "Cp1252", false);
// This is the size of the page in PDF 'points'. One point = 1/72 inch.
PDRectangle mediaBox = pdfPage.getMediaBox();
float inches = 72F; // Conversion from inches to points.
// show page title text
pdfContent.beginText();
pdfContent.setFontAndSize(pageTitleFont, 12);
pdfContent.showTextAligned(PdfContentByte.ALIGN_CENTER, getChartTitle(), pageWidth / 2 , pageHeight - chartLocOffset / 2, 0);
pdfContent.endText();
float topMargin = 0.5F * inches;
float bottomMargin = 0.75F * inches;
float leftMargin = 0.75F * inches;
float rightMargin = 0.5F * inches;
// draw all plots
PdfTemplate pdfTemplate = pdfContent.createTemplate(pageWidth, pageHeight);
Graphics2D g2 = pdfTemplate.createGraphics(pageWidth, pageHeight, mapper);
int rows = 1;
int columns = 1;
if (getContentPane().getLayout() instanceof GridLayout) {
rows = ((GridLayout)getContentPane().getLayout()).getRows();
columns = ((GridLayout)getContentPane().getLayout()).getColumns();
}
int eachChartWidth = pageWidth;
int eachChartHeight = pageHeight - chartLocOffset;
if (columns != 0) {
eachChartWidth = pageWidth / columns;
}
if (rows != 0) {
eachChartHeight = (pageHeight - chartLocOffset) / rows;
}
int xLoc = 0;
int yLoc = chartLocOffset;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
int index = i * columns + j;
if (index >= getChartSize()) {
break;
}
TrickChartControlPanel eachPanel = (TrickChartControlPanel)getContentPane().getComponent(index);
if (eachPanel.getTheChart() != null) {
Rectangle2D r2D = new Rectangle2D.Double(xLoc, yLoc, eachChartWidth, eachChartHeight);
eachPanel.getTheChart().draw(g2, r2D);
}
xLoc = xLoc + eachChartWidth;
}
xLoc = 0;
yLoc = yLoc + eachChartHeight;
}
g2.dispose();
pdfContent.addTemplate(pdfTemplate, 0, 0);
}
catch (DocumentException de) {
de.printStackTrace();
}
doc.addPage(pdfPage);
try (PDPageContentStream content = new PDPageContentStream(doc, pdfPage)) {
int fontSize = 12;
PDFont font = PDType1Font.TIMES_ROMAN;
String chartTitle = getChartTitle();
float chartTitleWidth = font.getStringWidth(chartTitle) / 1000F * fontSize;
float chartTitleHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000F * fontSize;
float chartTitleLowerLeftX = (mediaBox.getWidth() + leftMargin - rightMargin - chartTitleWidth) / 2F ;
float chartTitleLowerLeftY = mediaBox.getHeight() - topMargin - chartTitleHeight;
content.beginText();
content.setFont(font, fontSize);
content.newLineAtOffset(chartTitleLowerLeftX, chartTitleLowerLeftY);
content.showText(chartTitle);
content.endText();
// Define the box into which the chart(s) will go.
// The top of the box is 1/2 inch below the top margin.
// The rest is bounded by the left, right and bottom margins.
float chartBoxLowerLeftX = leftMargin;
float chartBoxLowerLeftY = bottomMargin;
float chartBoxUpperRightX = mediaBox.getUpperRightX() - rightMargin;
float chartBoxUpperRightY = mediaBox.getUpperRightY() - topMargin - 0.5F * inches;
float chartBoxWidth = chartBoxUpperRightX - chartBoxLowerLeftX;
float chartBoxHeight = chartBoxUpperRightY - chartBoxLowerLeftY;
int rows = 1; int cols = 1; // If we're plotting one plot on this page, otherwise ...
if ( getContentPane().getLayout() instanceof GridLayout) {
rows = ((GridLayout)getContentPane().getLayout()).getRows();
cols = ((GridLayout)getContentPane().getLayout()).getColumns();
}
// Calculate the size of the cells, into which each plot will be dawn.
int cellWidth = (int)chartBoxWidth / cols;
int cellHeight = (int)chartBoxHeight/ rows;
for (int i = 0 ; i < rows ; ++i) {
int cellLowerLeftY = (int)chartBoxUpperRightY - (i+1) * cellHeight;
for (int j = 0 ; j < cols ; ++j) {
int cellLowerLeftX = (int)chartBoxLowerLeftX + j * cellWidth;
int whichPanel = i * cols + j;
TrickChartControlPanel panel = (TrickChartControlPanel)getContentPane().getComponent(whichPanel);
JFreeChart thechart = panel.getTheChart();
BufferedImage bufferedImage = thechart.createBufferedImage((int)chartBoxWidth, (int)chartBoxHeight);
PDImageXObject image = JPEGFactory.createFromImage(doc, bufferedImage);
content.drawImage(image, cellLowerLeftX, cellLowerLeftY, cellWidth, cellHeight);
}
}
}
}
/**
* Sets this frame invisible.