우리가 사용하고 레거시 라이브러리로 생성 한 3 개의 PDF 문서가 디스크에 쓰여 있습니다. JAVA 서버 코드가이 3 가지 문서를 가져 와서 하나의 긴 PDF 문서로 변환하는 가장 쉬운 방법은 무엇입니까?PDF 문서 모음을 Java의 하나의 큰 PDF 문서로 병합 (서버 측)하는 것이 가장 쉬운 방법입니다.
이상적으로는 나는 이것을 클라이언트에서 스트림으로 반환 할 수 있도록 메모리에서 발생시키고 싶지만 디스크에 쓰는 것도 옵션입니다.
우리가 사용하고 레거시 라이브러리로 생성 한 3 개의 PDF 문서가 디스크에 쓰여 있습니다. JAVA 서버 코드가이 3 가지 문서를 가져 와서 하나의 긴 PDF 문서로 변환하는 가장 쉬운 방법은 무엇입니까?PDF 문서 모음을 Java의 하나의 큰 PDF 문서로 병합 (서버 측)하는 것이 가장 쉬운 방법입니다.
이상적으로는 나는 이것을 클라이언트에서 스트림으로 반환 할 수 있도록 메모리에서 발생시키고 싶지만 디스크에 쓰는 것도 옵션입니다.
@JD OConal 팁 주셔서 감사합니다, 당신은 나를 보내신 문서는 매우 오래된 이었지만,이 한 점 나 iText 향해. http://java-x.blogspot.com/2006/11/merge-pdf-files-with-itext.html
다른 답변을 주셔서 감사합니다.하지만이를 피할 수 있다면 다른 프로세스를 생성하고 싶지는 않지만 프로젝트에 이미 포함되어 있습니다. iText.jar를, 그래서 코드를 외부 의존성 여기
있어 추가 아니에요 내가 쓰기 결국 :public class PdfMergeHelper {
/**
* Merges the passed in PDFs, in the order that they are listed in the java.util.List.
* Writes the resulting PDF out to the OutputStream provided.
*
* Sample Usage:
* List<InputStream> pdfs = new ArrayList<InputStream>();
* pdfs.add(new FileInputStream("/location/of/pdf/OQS_FRSv1.5.pdf"));
* pdfs.add(new FileInputStream("/location/of/pdf/PPFP-Contract_Genericv0.5.pdf"));
* pdfs.add(new FileInputStream("/location/of/pdf/PPFP-Quotev0.6.pdf"));
* FileOutputStream output = new FileOutputStream("/location/to/write/to/merge.pdf");
* PdfMergeHelper.concatPDFs(pdfs, output, true);
*
* @param streamOfPDFFiles the list of files to merge, in the order that they should be merged
* @param outputStream the output stream to write the merged PDF to
* @param paginate true if you want page numbers to appear at the bottom of each page, false otherwise
*/
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();
}
}
}
}
저는 pdftk을 사용했습니다. 그것은 당신이 자바 애플 리케이션에서 실행해야 할 외부 응용 프로그램입니다.
이 list of Java open source PDF libraries을 살펴보십시오.
this article도 확인하십시오.
[편집 :이 항상 사용하기 쉬운 고스트 스크립트,하지만 누가 더 종속성을 원하는]
iText를 변경 한 것을 지금 commer있다 cial 라이센스 요구 사항, 그 좋은 도움이 아니라 (설명서를 원하십니까? 우리 책 구입!).
PDFSharp http://www.pdfsharp.net/을 찾아서 사용했습니다. 물건을 만드는 코드 내에서 PDFMerger라는 유틸리티가, http://www.pdfsharp.net/wiki/ConcatenateDocuments-sample.ashx
이 임의
PDFBox는 지금까지이를 달성하는 가장 쉬운 방법입니다 즐기십시오 : 함께 여러 PDF 문서을 연결하기위한 샘플 따라 간단하고 쉬운 아주 쉽게, 나를 데려다 줄 모든 루프와 2 줄의 코드가 포함되어있었습니다. :)
나는 iText를 강력히 권고합니다. 그들은 마케팅/법률 활동을하고 있으며, 사람들은 오래된 LGPL 버전을 새로운 상업용 버전으로 업그레이드 (읽기 : 위협)합니다. 나는 OSS 라이브러리를 원한다. 언젠가는 itextPdf.com의 메일 박스에서 매우 불쾌한 이메일을 발견했다. 블러! – Gab