java - 创建 pdf 并与 pdfbox 合并

标签 java pdf merge pdfbox

这就是我想要做的:

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

  2. 使用 pdfmerger 将这两个文件合并在一起

如果我将#1 保存到服务器端本地硬盘驱动器并为#2 加载文件,我知道如何执行此操作。但我想做的是“直接从内存中”使用。我已经从这个 pdfboxes 中搜索了所有方法,但仍然找不到。

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

谢谢。

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)从输入流而不是物理文件添加源的方法。

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

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
}

关于java - 创建 pdf 并与 pdfbox 合并,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13808090/

相关文章:

java - Java 中可能的 native 内存释放错误

java - Android 动态壁纸视差滚动效果

C#/ASP.NET - 从 PDF/DOC 文件获取缩略图

php - 使用MySQL数据库中的数据填写PDF表单

pdf - 在 mupdf 中从右到左阅读 PDF

java - 在java代码中用一个字符串替换多个字符串

python - 合并两个 pandas 数据框并根据条件创建一个新的二进制列

java - Java中的快速LinkedList搜索和删除

java - 打开现有的嵌入式 Neo4j 数据库

svn - 最好的锁定版本控制系统是什么?