mirror of
https://github.com/nasa/trick.git
synced 2025-06-19 23:53:54 +00:00
Replace iText Java PDF library with PDFBox. (#664)
This commit is contained in:
@ -9,21 +9,22 @@
|
|||||||
|
|
||||||
EXTERNAL_JARS = \
|
EXTERNAL_JARS = \
|
||||||
bsaf-1.9.2.jar \
|
bsaf-1.9.2.jar \
|
||||||
itext-2.1.7.jar \
|
|
||||||
jcommon-1.0.23.jar \
|
jcommon-1.0.23.jar \
|
||||||
jfreechart-1.0.19.jar \
|
jfreechart-1.0.19.jar \
|
||||||
jfreesvg-2.1.jar \
|
jfreesvg-2.1.jar \
|
||||||
jopt-simple-4.8.jar \
|
jopt-simple-4.8.jar \
|
||||||
junit-4.12.jar \
|
junit-4.12.jar \
|
||||||
swingx-1.6.1.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}
|
external_jars : ${EXTERNAL_JARS}
|
||||||
|
|
||||||
bsaf-1.9.2.jar :
|
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
|
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 :
|
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
|
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 :
|
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
|
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 :
|
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
|
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:
|
clean:
|
||||||
rm -f ${EXTERNAL_JARS}
|
rm -f ${EXTERNAL_JARS}
|
||||||
|
@ -40,12 +40,14 @@ import org.jfree.data.xy.XYSeries;
|
|||||||
import org.jfree.data.xy.XYSeriesCollection;
|
import org.jfree.data.xy.XYSeriesCollection;
|
||||||
import org.xml.sax.SAXException;
|
import org.xml.sax.SAXException;
|
||||||
|
|
||||||
import com.lowagie.text.Document;
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||||
import com.lowagie.text.DocumentException;
|
import org.apache.pdfbox.pdmodel.PDPage;
|
||||||
import com.lowagie.text.PageSize;
|
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
||||||
import com.lowagie.text.pdf.DefaultFontMapper;
|
import org.apache.pdfbox.pdmodel.common.PDRectangle;
|
||||||
import com.lowagie.text.pdf.PdfContentByte;
|
import org.apache.pdfbox.pdmodel.font.PDFont;
|
||||||
import com.lowagie.text.pdf.PdfWriter;
|
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.TrickApplication;
|
||||||
import trick.common.ui.UIUtils;
|
import trick.common.ui.UIUtils;
|
||||||
@ -115,17 +117,7 @@ public class JXPlotApplication extends TrickApplication {
|
|||||||
try {
|
try {
|
||||||
int dpFileCount = productTreeRoot.getChildCount();
|
int dpFileCount = productTreeRoot.getChildCount();
|
||||||
if (dpFileCount > 0) {
|
if (dpFileCount > 0) {
|
||||||
Document document = new Document(PageSize.A4);
|
PDDocument document = new PDDocument();
|
||||||
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();
|
|
||||||
}
|
|
||||||
for (int i = 0; i < dpFileCount; i++) {
|
for (int i = 0; i < dpFileCount; i++) {
|
||||||
DefaultMutableTreeNode eachDPFile = (DefaultMutableTreeNode)productTree.getModel().getChild(productTreeRoot, i);
|
DefaultMutableTreeNode eachDPFile = (DefaultMutableTreeNode)productTree.getModel().getChild(productTreeRoot, i);
|
||||||
int pageCount = eachDPFile.getChildCount();
|
int pageCount = eachDPFile.getChildCount();
|
||||||
@ -133,14 +125,12 @@ public class JXPlotApplication extends TrickApplication {
|
|||||||
DefaultMutableTreeNode eachPage = (DefaultMutableTreeNode)productTree.getModel().getChild(eachDPFile, j);
|
DefaultMutableTreeNode eachPage = (DefaultMutableTreeNode)productTree.getModel().getChild(eachDPFile, j);
|
||||||
if (eachPage.getUserObject() instanceof TrickChartFrame) {
|
if (eachPage.getUserObject() instanceof TrickChartFrame) {
|
||||||
TrickChartFrame theFrame = (TrickChartFrame)eachPage.getUserObject();
|
TrickChartFrame theFrame = (TrickChartFrame)eachPage.getUserObject();
|
||||||
theFrame.writePDFPage(pdfContent, new DefaultFontMapper());
|
theFrame.writePDFPage(document);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// document needs to be closed after the PDF file is saved
|
// document needs to be closed after the PDF file is saved
|
||||||
if (document != null) {
|
document.save(file);
|
||||||
document.close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
@ -8,6 +8,7 @@ import java.awt.Image;
|
|||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
import java.awt.event.KeyEvent;
|
import java.awt.event.KeyEvent;
|
||||||
import java.awt.geom.Rectangle2D;
|
import java.awt.geom.Rectangle2D;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -21,15 +22,16 @@ import javax.swing.JSeparator;
|
|||||||
|
|
||||||
import org.jfree.chart.ChartPanel;
|
import org.jfree.chart.ChartPanel;
|
||||||
|
|
||||||
import com.lowagie.text.Document;
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||||
import com.lowagie.text.DocumentException;
|
import org.apache.pdfbox.pdmodel.PDPage;
|
||||||
import com.lowagie.text.PageSize;
|
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
||||||
import com.lowagie.text.pdf.BaseFont;
|
import org.apache.pdfbox.pdmodel.common.PDRectangle;
|
||||||
import com.lowagie.text.pdf.DefaultFontMapper;
|
import org.apache.pdfbox.pdmodel.font.PDFont;
|
||||||
import com.lowagie.text.pdf.FontMapper;
|
import org.apache.pdfbox.pdmodel.font.PDType1Font;
|
||||||
import com.lowagie.text.pdf.PdfContentByte;
|
import org.apache.pdfbox.pdmodel.graphics.image.JPEGFactory;
|
||||||
import com.lowagie.text.pdf.PdfTemplate;
|
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
|
||||||
import com.lowagie.text.pdf.PdfWriter;
|
|
||||||
|
import org.jfree.chart.JFreeChart;
|
||||||
|
|
||||||
import trick.common.TrickApplication;
|
import trick.common.TrickApplication;
|
||||||
import trick.common.ui.UIUtils;
|
import trick.common.ui.UIUtils;
|
||||||
@ -179,23 +181,10 @@ public class TrickChartFrame extends TrickFrame {
|
|||||||
*/
|
*/
|
||||||
public void saveChartsAsPDF() throws IOException {
|
public void saveChartsAsPDF() throws IOException {
|
||||||
File file = UIUtils.chooseSaveFile(null, "plot_", "pdf", this);
|
File file = UIUtils.chooseSaveFile(null, "plot_", "pdf", this);
|
||||||
if (file == null) {
|
if (file != null) {
|
||||||
return;
|
PDDocument document = new PDDocument(); // Default Paper size is US Letter.
|
||||||
}
|
writePDFPage(document);
|
||||||
Document document = new Document(PageSize.A4);
|
document.save(file);
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -203,76 +192,78 @@ public class TrickChartFrame extends TrickFrame {
|
|||||||
/**
|
/**
|
||||||
* Writes all plots shown to one page of PDF.
|
* 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 doc An Apache-PDFBox PDDocument object.
|
||||||
* @param mapper mapping between AWT fonts and PDF fonts.
|
|
||||||
* @throws IOException IOException
|
* @throws IOException IOException
|
||||||
*/
|
*/
|
||||||
public void writePDFPage(PdfContentByte pdfContent, FontMapper mapper) throws IOException {
|
public void writePDFPage(PDDocument doc) 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();
|
|
||||||
|
|
||||||
BaseFont pageTitleFont = BaseFont.createFont(BaseFont.HELVETICA_BOLD, "Cp1252", false);
|
PDPage pdfPage = new PDPage(); // Default page size is 'US Letter' (8.5x11 inches).
|
||||||
|
|
||||||
// show page title text
|
// This is the size of the page in PDF 'points'. One point = 1/72 inch.
|
||||||
pdfContent.beginText();
|
PDRectangle mediaBox = pdfPage.getMediaBox();
|
||||||
pdfContent.setFontAndSize(pageTitleFont, 12);
|
|
||||||
pdfContent.showTextAligned(PdfContentByte.ALIGN_CENTER, getChartTitle(), pageWidth / 2 , pageHeight - chartLocOffset / 2, 0);
|
|
||||||
pdfContent.endText();
|
|
||||||
|
|
||||||
// draw all plots
|
float inches = 72F; // Conversion from inches to points.
|
||||||
PdfTemplate pdfTemplate = pdfContent.createTemplate(pageWidth, pageHeight);
|
|
||||||
Graphics2D g2 = pdfTemplate.createGraphics(pageWidth, pageHeight, mapper);
|
|
||||||
int rows = 1;
|
|
||||||
int columns = 1;
|
|
||||||
|
|
||||||
if (getContentPane().getLayout() instanceof GridLayout) {
|
float topMargin = 0.5F * inches;
|
||||||
|
float bottomMargin = 0.75F * inches;
|
||||||
|
float leftMargin = 0.75F * inches;
|
||||||
|
float rightMargin = 0.5F * inches;
|
||||||
|
|
||||||
|
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();
|
rows = ((GridLayout)getContentPane().getLayout()).getRows();
|
||||||
columns = ((GridLayout)getContentPane().getLayout()).getColumns();
|
cols = ((GridLayout)getContentPane().getLayout()).getColumns();
|
||||||
}
|
}
|
||||||
|
|
||||||
int eachChartWidth = pageWidth;
|
// Calculate the size of the cells, into which each plot will be dawn.
|
||||||
int eachChartHeight = pageHeight - chartLocOffset;
|
int cellWidth = (int)chartBoxWidth / cols;
|
||||||
if (columns != 0) {
|
int cellHeight = (int)chartBoxHeight/ rows;
|
||||||
eachChartWidth = pageWidth / columns;
|
|
||||||
}
|
|
||||||
if (rows != 0) {
|
|
||||||
eachChartHeight = (pageHeight - chartLocOffset) / rows;
|
|
||||||
}
|
|
||||||
|
|
||||||
int xLoc = 0;
|
for (int i = 0 ; i < rows ; ++i) {
|
||||||
int yLoc = chartLocOffset;
|
int cellLowerLeftY = (int)chartBoxUpperRightY - (i+1) * cellHeight;
|
||||||
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;
|
|
||||||
|
|
||||||
}
|
for (int j = 0 ; j < cols ; ++j) {
|
||||||
g2.dispose();
|
int cellLowerLeftX = (int)chartBoxLowerLeftX + j * cellWidth;
|
||||||
pdfContent.addTemplate(pdfTemplate, 0, 0);
|
|
||||||
}
|
|
||||||
catch (DocumentException de) {
|
|
||||||
de.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
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.
|
* Sets this frame invisible.
|
||||||
|
Reference in New Issue
Block a user