创建pdf并与pdfbox合并 [英] Create pdf and merge with pdfbox

查看:227
本文介绍了创建pdf并与pdfbox合并的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我想要做的事情:

This is what I want to do:


  1. 使用pdfbox制作2个不同的pdf文件

  1. Make 2 different pdf files using pdfbox

使用pdfmerger合并这两个文件

Merge these two files together using pdfmerger

我知道该怎么做如果我将#1保存到服务器端本地硬盘驱动器并加载#2的文件。但我想要做的是直接从记忆中使用。我已经搜索了这个pdfbox中的所有方法,但仍然无法找到它。

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.

这是我从本地文件获取的代码

This is my code getting from the local file

谢谢。

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 <a href="mailto:m.g.n@gmx.de">Michael Niedermair</a>
* @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.
     * <br />
     * 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();
        }
    }

}


推荐答案

您只需使用 PdfMergeUtility.addSource(InputStream) 从输入流而不是从物理文件添加源的方法。

You just need to use the PdfMergeUtility.addSource(InputStream) method to add source from an inputstream and not from a physical file.

快速浏览一下API,你可以做的就是使用 PDDocument.save(OutputStream) 将文件写入的方法内存中的字节数组,这样的东西应该可以工作。

With a quick glance at the API, what you could do is use the PDDocument.save(OutputStream) method to write the file into a byte array in memory, something like this should work.

static byte[] doIt(String message) {
   PDDocument doc = new PDDocument();
   // add the message
   ByteArrayOutputStream baos = new ByteArrayOutputStream();
   doc.save(baos);
   return baos.toByteArray();
}

void main(String args[]) {
   byte[] pdf1 = doIt("hello");
   byte[] pdf2 = doIt("world");
   PDFMergerUtility merger = new PDFMergerUtility();
   merger.addSource(new ByteArrayInputStream(pdf1));
   merger.addSource(new ByteArrayInputStream(pdf2));
   // do the rest with the merger
}

这篇关于创建pdf并与pdfbox合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆