spring-mvc:合并pdf文件并在Web浏览器中显示它们

时间:2016-12-02 13:38:23

标签: spring-mvc pdfbox

我需要将多个pdf文件合并为一个pdf并在我的网络浏览器中显示。 我知道如何显示一个文件:

File file = new File(activite.getLienUploadUn());
FileInputStream inputStream = new FileInputStream(file);

byte[] buffer = new byte[8192];
ByteArrayOutputStream baos = new ByteArrayOutputStream();

int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1)
{
     baos.write(buffer, 0, bytesRead);
}


response.setHeader("Content-Disposition","inline; filename=\""+file.getName()+"\"");
response.setContentType("application/pdf");
ServletOutputStream outputStream = response.getOutputStream();
baos.writeTo(outputStream);
outputStream.flush();

我想我将使用PdfBox及其PDFMergerUtility类来合并文件:

PDFMergerUtility mergePdf = new PDFMergerUtility();
mergePdf.addSource(file);
mergePdf.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());

但是从那里我如何将合并的文档转换为byteArrayOutputStream?

1 个答案:

答案 0 :(得分:1)

您可以致电PDFMergerUtility.setDestinationStream(OutputStream destStream)传递输出流(javadoc)。