How To Merge a Landscape Document in the PDF Document Resizing and Fitting within Border Using Jasper


I have a program in java which uses Jasper to generate the Reports. It merges the Bill Receipt while merging them in the main Report. Bill Receipt can be in Landscape or Portrait form.Below is the code to Merge. If its a Portrait Receipt its merging fine. But for Landscape Receipt, the borders are exceeding the A4 size leading to loss of receipt at both sides. Therefore I want to Resize the receipt if its in a Landscape format and fit it within the boundaries of the PDF document while merging. Also giving some spaces at borders.


public void mergeCtcDocument(List<InputStream> list, OutputStream outputStream) throws DocumentException, IOException {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, outputStream);;
    PdfContentByte cb = writer.getDirectContent();
    for (InputStream in : list) {
        PdfReader reader = new PdfReader(in);
        for (int i = 1; i <= reader.getNumberOfPages(); i++) {
            //import the page from source pdf
            PdfImportedPage page = writer.getImportedPage(reader, i);
            //Set content to the center
            float x = (document.getPageSize().getWidth() - page.getWidth())/2;
            float y = (document.getPageSize().getHeight() - page.getHeight())/2;
            //add the page to the destination pdf
            cb.addTemplate(page, x, y);
    for(InputStream in : list) {
ashutoshbajpai1909's picture
Joined: Jul 17 2019 - 2:27am
Last seen: 11 months 3 weeks ago

0 Answers:

No answers yet