Note that there are some explanatory texts on larger screens.

plurals
  1. POCreate pdf and merge with pdfbox
    text
    copied!<p>This is what I want to do:</p> <ol> <li><p>Make 2 different pdf files using pdfbox</p></li> <li><p>Merge these two files together using pdfmerger</p></li> </ol> <p>I know how to do this if I'm saving #1 to the server side local hard drive and load the files for the #2. But what I want to do is using "directly from the memory". I've searched all the methods from this pdfboxes but still couldn't find it.</p> <p>This is my code getting from the local file</p> <p>Thank you.</p> <pre><code>import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import org.apache.pdfbox.exceptions.COSVisitorException; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.edit.PDPageContentStream; import org.apache.pdfbox.pdmodel.font.PDFont; import org.apache.pdfbox.pdmodel.font.PDTrueTypeFont; import org.apache.pdfbox.pdmodel.font.PDType1Font; import org.apache.pdfbox.util.PDFMergerUtility; /** * This is an example that creates a simple document * with a ttf-font. * * @author &lt;a href="mailto:m.g.n@gmx.de"&gt;Michael Niedermair&lt;/a&gt; * @version $Revision: 1.2 $ */ public class Test2 { /** * create the second sample document from the PDF file format specification. * * @param file The file to write the PDF to. * @param message The message to write in the file. * @param fontfile The ttf-font file. * * @throws IOException If there is an error writing the data. * @throws COSVisitorException If there is an error writing the PDF. */ public void doIt(final String file, final String message) throws IOException, COSVisitorException { // the document PDDocument doc = null; try { doc = new PDDocument(); PDPage page = new PDPage(); doc.addPage(page); PDFont font = PDType1Font.HELVETICA_BOLD; PDPageContentStream contentStream = new PDPageContentStream(doc, page); contentStream.beginText(); contentStream.setFont(font, 12); contentStream.moveTextPositionByAmount(100, 700); contentStream.drawString(message); contentStream.endText(); contentStream.close(); doc.save(file); System.out.println(file + " created!"); } finally { if (doc != null) { doc.close(); } } } /** * This will create a hello world PDF document * with a ttf-font. * &lt;br /&gt; * see usage() for commandline * * @param args Command line arguments. */ public static void main(String[] args) { Test2 app = new Test2(); Test2 app2 = new Test2(); try { app.doIt("C:/here.pdf", "hello"); app2.doIt("C:/here2.pdf", "helloagain"); PDFMergerUtility merger = new PDFMergerUtility(); merger.addSource("C:/here.pdf"); merger.addSource("C:/here2.pdf"); OutputStream bout2 = new BufferedOutputStream(new FileOutputStream("C:/hereisthefinal.pdf")); merger.setDestinationStream(bout2); merger.mergeDocuments(); } catch (COSVisitorException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload