Return to Snippet

Revision: 72668
at October 13, 2017 00:02 by markt22


Initial Code
/**
 * @param args the command line arguments
 * @throws java.io.IOException
 */
public static void main(String[] args) throws IOException {
    PDDocument document1 = getFirstDoc();
    PDDocument document2 = getSecondDoc();
    
    Overlay overlay = new Overlay();
    overlay.setOverlayPosition(Overlay.Position.FOREGROUND);
    overlay.setInputPDF(document1);
    overlay.setAllPagesOverlayPDF(document2);
    
    Map<Integer, String> ovmap = new HashMap<Integer, String>();            
    overlay.overlay(ovmap);
    
    document1.save("");
    
    document1.close();
    document2.close();
}

static PDDocument getFirstDoc() throws IOException {
    PDDocument document = new PDDocument();
    PDPage page = new PDPage(PDRectangle.A4);
 
    document.addPage(page);
 
    PDPageContentStream contentStream = new PDPageContentStream(document, page);
 
    contentStream.setNonStrokingColor(Color.RED);
    contentStream.addRect(0, 0, page.getMediaBox().getWidth(), page.getMediaBox().getHeight());
    contentStream.fill();
 
    contentStream.close();
    
    return document;
}

static PDDocument getSecondDoc() throws IOException {
    PDDocument document = new PDDocument();
    PDPage page = new PDPage(PDRectangle.A4);
 
    document.addPage(page);
 
    PDPageContentStream contentStream = new PDPageContentStream(document, page);
 
    contentStream.beginText(); 
 
    contentStream.setFont(PDType1Font.TIMES_ROMAN, 12);
    contentStream.newLineAtOffset(25, 500);
    contentStream.showText("Hello World");
 
    contentStream.endText();
 
    contentStream.close();

    return document;
}

Initial URL


Initial Description
Generates two PDF documents and then overlays the second onto the first.

Initial Title
PDFBox: Overlaying one PDF on another

Initial Tags


Initial Language
Java