Viral Patel's tutorial on how to merge and split PDF files is useful. Unfortunately, I need something more than merging the files but appending a PDF to another PDF.
I am generating reports and one component is a graph using JFreeCharts. The graph I make is turned into PDF and read again by the merge code. Let us say I have main.pdf
for the text and tables PDF and chart.pdf
for the latter.
I am certain that main.pdf
has an extra space that another figure can fit into which is chart.pdf
and I want the latter to be scaled and placed there and not bloated in another page.
I have the code below and I can't seem to implement this:
code to create
chart.pdf
import java.awt.Color; import java.awt.Graphics2D; import java.awt.Paint; import java.awt.geom.Rectangle2D; import java.io.FileOutputStream; import org.jfree.chart.ChartFactory; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.renderer.category.BarRenderer; import org.jfree.chart.renderer.category.CategoryItemRenderer; import org.jfree.data.category.DefaultCategoryDataset; import com.lowagie.text.Document; import com.lowagie.text.Rectangle; import com.lowagie.text.pdf.DefaultFontMapper; import com.lowagie.text.pdf.PdfContentByte; import com.lowagie.text.pdf.PdfTemplate; import com.lowagie.text.pdf.PdfWriter; public class ChartDemo { public static void main(String[] args) { writeChartToPDF(generateBarChart(), 500, 400, "D://chart.pdf"); } public static void writeChartToPDF(JFreeChart chart, int width, int height, String fileName) { PdfWriter writer = null; Document document = new Document(new Rectangle(width, height)); try { writer = PdfWriter.getInstance(document, new FileOutputStream( fileName)); document.open(); PdfContentByte contentByte = writer.getDirectContent(); PdfTemplate template = contentByte.createTemplate(width, height); Graphics2D graphics2d = template.createGraphics(width, height, new DefaultFontMapper()); Rectangle2D rectangle2d = new Rectangle2D.Double(0, 0, width, height); chart.draw(graphics2d, rectangle2d); graphics2d.dispose(); contentByte.addTemplate(template, 0, 0); } catch (Exception e) { e.printStackTrace(); } document.close(); } public static JFreeChart generateBarChart() { DefaultCategoryDataset dataSet = new DefaultCategoryDataset(); dataSet.setValue(100, "Population", "2"); dataSet.setValue(78, "Population", "4"); dataSet.setValue(62, "Population", "6"); dataSet.setValue(50, "Population", "8"); dataSet.setValue(39, "Population", "10"); JFreeChart chart = ChartFactory.createBarChart("", "Team number", "Solved problems (%)", dataSet, PlotOrientation.VERTICAL, false, true, false); chart.setBackgroundPaint(Color.WHITE); final CategoryPlot plot = chart.getCategoryPlot(); plot.setBackgroundPaint(Color.WHITE); plot.setDomainGridlinePaint(new Color(204, 204, 204)); plot.setRangeGridlinePaint(new Color(204, 204, 204)); final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); final BarRenderer renderer1 = (BarRenderer) plot.getRenderer(); renderer1.setDrawBarOutline(false); renderer1.setShadowVisible(false); renderer1.setDrawBarOutline(false); renderer1.setSeriesPaint(0, Color.gray); renderer1.setSeriesPaint(1, Color.RED); return chart; } }
code for merging
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import com.lowagie.text.Document; import com.lowagie.text.pdf.BaseFont; import com.lowagie.text.pdf.PdfContentByte; import com.lowagie.text.pdf.PdfImportedPage; import com.lowagie.text.pdf.PdfReader; import com.lowagie.text.pdf.PdfWriter; public class MergePdf { public static void main(String[] args) { try { List<InputStream> pdfs = new ArrayList<InputStream>(); pdfs.add(new FileInputStream("D:\\main.pdf")); pdfs.add(new FileInputStream("D:\\chart.pdf")); OutputStream output = new FileOutputStream("D:\\merge.pdf"); MergePdf.concatPDFs(pdfs, output, true); } catch (Exception e) { e.printStackTrace(); } } public static void concatPDFs(List<InputStream> streamOfPDFFiles, OutputStream outputStream, boolean paginate) { Document document = new Document(); try { List<InputStream> pdfs = streamOfPDFFiles; List<PdfReader> readers = new ArrayList<PdfReader>(); int totalPages = 0; Iterator<InputStream> iteratorPDFs = pdfs.iterator(); // Create Readers for the pdfs. while (iteratorPDFs.hasNext()) { InputStream pdf = iteratorPDFs.next(); PdfReader pdfReader = new PdfReader(pdf); readers.add(pdfReader); totalPages += pdfReader.getNumberOfPages(); } // Create a writer for the outputstream PdfWriter writer = PdfWriter.getInstance(document, outputStream); document.open(); BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); PdfContentByte cb = writer.getDirectContent(); // Holds the PDF // data PdfImportedPage page; int currentPageNumber = 0; int pageOfCurrentReaderPDF = 0; Iterator<PdfReader> iteratorPDFReader = readers.iterator(); // Loop through the PDF files and add to the output. while (iteratorPDFReader.hasNext()) { PdfReader pdfReader = iteratorPDFReader.next(); // Create a new page in the target for each source page. while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) { document.newPage(); pageOfCurrentReaderPDF++; currentPageNumber++; page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF); cb.addTemplate(page, 0, 0); // Code for pagination. if (paginate) { cb.beginText(); cb.setFontAndSize(bf, 9); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "" + currentPageNumber + " of " + totalPages, 520, 5, 0); cb.endText(); } } pageOfCurrentReaderPDF = 0; } outputStream.flush(); document.close(); outputStream.close(); } catch (Exception e) { e.printStackTrace(); } finally { if (document.isOpen()) document.close(); try { if (outputStream != null) outputStream.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } } }