Example usage for com.lowagie.text.pdf PdfCopyFields close

List of usage examples for com.lowagie.text.pdf PdfCopyFields close

Introduction

In this page you can find the example usage for com.lowagie.text.pdf PdfCopyFields close.

Prototype

public void close() 

Source Link

Document

Closes the output document.

Usage

From source file:de.intranda.test_ics.ImageHelper.java

License:Apache License

@SuppressWarnings("unused")
private void addFrontPage(File frontPage, File pdfFile) throws IOException, DocumentException {
    File tempFile = new File(pdfFile.getParent(), System.currentTimeMillis() + ".pdf");
    pdfFile.renameTo(tempFile);// ww  w .  j a v  a2 s . c  om
    PdfReader reader1 = new PdfReader(frontPage.getAbsolutePath());
    PdfReader reader2 = new PdfReader(tempFile.getAbsolutePath());
    PdfCopyFields copy = new PdfCopyFields(new FileOutputStream(pdfFile));
    copy.addDocument(reader1);
    copy.addDocument(reader2);
    copy.close();
    if (tempFile != null && tempFile.isFile()) {
        tempFile.delete();
    }
}

From source file:it.jugpadova.blo.ParticipantBadgeBo.java

License:Apache License

/**
 * Build a PDF with the badges of confirmed participants.
 *//*from  w  w w.  j  av a 2  s  . c  om*/
public byte[] buildPDFBadges(Event event) throws IOException, DocumentException {
    List<Participant> participants = participantDao
            .findConfirmedParticipantsByEventIdOrderByLastNameAndFirstName(event.getId());
    int participantsPerPage = getParticipantsPerPage(event);
    int pages = (participants.size() / participantsPerPage) + 2; // prints a more page with empty badges
    ByteArrayOutputStream pdfMergedBaos = new ByteArrayOutputStream();
    PdfCopyFields pdfMerged = new PdfCopyFields(pdfMergedBaos);
    int newIndex = 1;
    for (int i = 0; i < pages; i++) {
        InputStream templateIs = getBadgePageTemplateInputStream(event);
        RandomAccessFileOrArray rafa = new RandomAccessFileOrArray(templateIs);
        PdfReader template = new PdfReader(rafa, null);
        ByteArrayOutputStream pdfPageBaos = new ByteArrayOutputStream();
        PdfStamper pdfPage = new PdfStamper(template, pdfPageBaos);
        AcroFields pdfPageForm = pdfPage.getAcroFields();
        for (int j = 1; j <= participantsPerPage; j++) {
            pdfPageForm.renameField("title" + j, "title" + newIndex);
            pdfPageForm.renameField("firstName" + j, "firstName" + newIndex);
            pdfPageForm.renameField("lastName" + j, "lastName" + newIndex);
            pdfPageForm.renameField("firm" + j, "firm" + newIndex);
            pdfPageForm.renameField("role" + j, "role" + newIndex);
            newIndex++;
        }
        pdfPage.close();
        template.close();
        pdfMerged.addDocument(new PdfReader(pdfPageBaos.toByteArray()));
    }
    pdfMerged.close();
    ByteArrayOutputStream resultBaos = new ByteArrayOutputStream();
    PdfReader mergedReader = new PdfReader(pdfMergedBaos.toByteArray());
    PdfStamper mergedStamper = new PdfStamper(mergedReader, resultBaos);
    AcroFields mergedForm = mergedStamper.getAcroFields();
    int count = 1;
    for (int i = 0; i < pages; i++) {
        for (int j = 1; j <= participantsPerPage; j++) {
            mergedForm.setField("title" + count, event.getTitle());
            count++;
        }
    }
    count = 1;
    for (Participant participant : participants) {
        mergedForm.setField("firstName" + count, participant.getFirstName());
        mergedForm.setField("lastName" + count, participant.getLastName());
        count++;
    }
    mergedStamper.setFormFlattening(true);
    mergedStamper.close();
    return resultBaos.toByteArray();
}

From source file:net.mitnet.tools.pdf.book.publisher.BookPublisher.java

License:Open Source License

/**
 * TODO: review concat process and compare to PdfCopy.
 *///from   ww w.  j  a v a 2 s  .  c om
public void concatPdf(File firstPdf, File secondPdf, File concatPdf) throws IOException, DocumentException {

    if (isDebugEnabled()) {
        debug("firstPdf: " + firstPdf);
        debug("secondPdf: " + secondPdf);
        debug("concatPdf: " + concatPdf);
        debug("concat PDFs");
    }

    PdfReader firstReader = new PdfReader(firstPdf.getPath());
    PdfReader secondReader = new PdfReader(secondPdf.getPath());
    PdfCopyFields copy = new PdfCopyFields(new FileOutputStream(concatPdf.getPath()));
    copy.addDocument(firstReader);
    copy.addDocument(secondReader);
    copy.close();
}

From source file:net.sourceforge.fenixedu.presentationTier.servlets.filters.ProcessCandidacyPrintAllDocumentsFilter.java

License:Open Source License

private ByteArrayOutputStream concatenateDocs(byte[] originalDoc, Person person)
        throws IOException, DocumentException {
    ByteArrayOutputStream concatenatedPdf = new ByteArrayOutputStream();
    PdfCopyFields copy = new PdfCopyFields(concatenatedPdf);

    try {/*from w  ww. java  2  s .c o m*/
        copy.addDocument(new PdfReader(createAcademicAdminProcessSheet(person).toByteArray()));
    } catch (JRException e) {
        logger.error(e.getMessage(), e);
    }
    copy.addDocument(new PdfReader(originalDoc));
    for (PdfFiller pdfFiller : pdfFillersSet) {
        copy.addDocument(new PdfReader(pdfFiller.getFilledPdf(person).toByteArray()));
    }
    copy.close();

    return concatenatedPdf;
}

From source file:org.fenixedu.academic.servlet.ProcessCandidacyPrintAllDocumentsFilter.java

License:Open Source License

private ByteArrayOutputStream concatenateDocs(byte[] originalDoc, Person person)
        throws IOException, DocumentException {
    ByteArrayOutputStream concatenatedPdf = new ByteArrayOutputStream();
    PdfCopyFields copy = new PdfCopyFields(concatenatedPdf);

    //if no documents are added there is nothing to close
    boolean isToClose = false;
    if (!FirstTimeDocumentsConfiguration.getInstance().isToExclude("adminProcessSheet")) {
        try {//from   w  w w. j  av  a  2 s . co m
            copy.addDocument(new PdfReader(createAcademicAdminProcessSheet(person)));
            isToClose = true;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }
    if (originalDoc.length > 0) {
        copy.addDocument(new PdfReader(originalDoc));
        isToClose = true;
    }
    for (PdfFiller pdfFiller : pdfFillersSet) {
        if (!isPdfFillerToExclude(pdfFiller.getClass().getName())) {
            copy.addDocument(new PdfReader(pdfFiller.getFilledPdf(person).toByteArray()));
            isToClose = true;
        }
    }
    if (isToClose) {
        copy.close();
    }

    return concatenatedPdf;
}

From source file:org.fenixedu.idcards.ui.candidacydocfiller.BPIPdfFiller.java

License:Open Source License

@Override
public ByteArrayOutputStream getFilledPdf(Person person) throws IOException, DocumentException {
    ByteArrayOutputStream concatenatedBPIPdf = new ByteArrayOutputStream();
    PdfCopyFields copy = new PdfCopyFields(concatenatedBPIPdf);

    copy.addDocument(new PdfReader(getFilledPdfBPICardAEIST(person).toByteArray()));
    copy.addDocument(new PdfReader(getFilledPdfBPIDigitalDoc(person).toByteArray()));
    copy.addDocument(new PdfReader(getFilledPdfBPIPersonalInformation(person).toByteArray()));
    copy.addDocument(new PdfReader(getFilledPdfBPIProductsandServices(person).toByteArray()));

    copy.close();

    return concatenatedBPIPdf;
}

From source file:org.fenixedu.idcards.ui.candidacydocfiller.SantanderPdfFiller.java

License:Open Source License

@Override
public ByteArrayOutputStream getFilledPdf(Person person) throws IOException, DocumentException {
    ByteArrayOutputStream concatenatedBPIPdf = new ByteArrayOutputStream();
    PdfCopyFields copy = new PdfCopyFields(concatenatedBPIPdf);

    copy.addDocument(new PdfReader(getFilledPdfSantanderApplication(person).toByteArray()));

    copy.close();

    return concatenatedBPIPdf;
}

From source file:org.fenixedu.ulisboa.specifications.ui.firstTimeCandidacy.DocumentsPrintController.java

License:Open Source License

private static ByteArrayOutputStream concatenateDocs(byte[] existingDoc, byte[] newDoc) {
    ByteArrayOutputStream concatenatedPdf = new ByteArrayOutputStream();
    try {/*from   w  ww .  j  a  v a 2s. c o  m*/
        PdfCopyFields copy = new PdfCopyFields(concatenatedPdf);
        copy.addDocument(new PdfReader(existingDoc));
        copy.addDocument(new PdfReader(newDoc));
        copy.close();
    } catch (DocumentException | IOException e) {
        throw new RuntimeException(e);
    }
    return concatenatedPdf;
}

From source file:org.kuali.continuity.service.JasperReportServiceImpl.java

License:Educational Community License

public void mergePdf(String outfile, List<String> pdfs) throws DocumentException, IOException {
    //      File test = new File("/");
    //      String[] flist = test.list();
    //      for (String f : flist) {
    // System.out.println(f);
    //      }/*from   w  ww.ja va 2s  .  c om*/
    FileOutputStream baos = new FileOutputStream(new File(outfile));
    PdfCopyFields copy = new PdfCopyFields(baos);

    //      int i = 0;
    for (String pdf : pdfs) {
        copy.addDocument(new PdfReader(pdf));
    }
    copy.close();
}

From source file:org.kuali.continuity.service.ReportServiceJasperImpl.java

License:Educational Community License

@SuppressWarnings("unchecked")
public void runPrint(int pid, String acronymPlusName, String institutionId, String section, String filePath,
        OutputStream outStream) {
    // jasperReportService = new JasperReportServiceImpl();
    // String indir="C:/ws/wsjasper/JasperAssist1/";

    String indir = filePath + srcFolder;
    String fontDir = filePath + fontFolder;
    String imageDir = filePath + imageFolder + "begin-report.png";
    String reportDir = filePath + report;

    String thisrpt = "starting";
    ArrayList<String> pdfs = new ArrayList<String>();
    String fs = System.getProperty("file.separator");
    fontFolder = fs + "WEB-INF" + fs + "classes" + fs;

    GregorianCalendar now = new GregorianCalendar();
    System.out.println("Start time: " + now.getTime());

    Map startParameterMap = getStartParameters(pid, startSql); //rpd.getParameters(jasperReportService.getConnection(), startSql, new Integer(436));

    boolean hasInstructionRows = false;
    boolean hasInformationRowsIt2 = false;
    boolean hasInformationRowsIt3 = false;
    boolean hasCriticalFunction = false;
    hasInstructionRows = hasRows(pid, instructionSql);
    hasInformationRowsIt2 = hasRows(pid, informationSqlIt2);
    hasInformationRowsIt3 = hasRows(pid, informationSqlIt3);
    hasCriticalFunction = hasRows(pid, criticalFunctionSql);

    String instructionManageTool = getInstructionManageTool(institutionId, instructionManageSql);
    Map<String, String> infoMap = getInfo(institutionId, getInfoSql);
    String optionSection = "FACULTY PREPAREDNESS";

    System.out.println("   Is instruction size > 0?  " + hasInstructionRows);

    if (section.equals("fr") && ((String) startParameterMap.get("OPTION_SECTION")).equals("INSTRUCTION")
            && !hasInstructionRows) {
        section = "in-no-data";
    } else if (section.equals("fr")
            && ((String) startParameterMap.get("OPTION_SECTION")).equals("INSTRUCTION")) {
        section = "in";
    } else if (section.equals("all") && ((String) startParameterMap.get("OPTION_SECTION")).equals("INSTRUCTION")
            && hasInstructionRows) {
        all[3] = "in";
    } else if (section.equals("all") && ((String) startParameterMap.get("OPTION_SECTION")).equals("INSTRUCTION")
            && !hasInstructionRows) {
        all[3] = "in-no-data";
    }/*from w  w  w . j  av a 2s . c  o  m*/

    if (!hasCriticalFunction) {
        ((String[]) listMap.get("cf"))[0] = "cf1-no-data";
    } else {
        ((String[]) listMap.get("cf"))[0] = "cf1";
    }

    if (!hasInformationRowsIt2) {
        ((String[]) listMap.get("it"))[1] = "IT2-no-data";
    } else {
        ((String[]) listMap.get("it"))[1] = "IT2";
    }

    if (!hasInformationRowsIt3) {
        ((String[]) listMap.get("it"))[2] = "IT3-no-data";
    } else {
        ((String[]) listMap.get("it"))[2] = "IT3";
    }

    boolean isLbnl = this.systemDomainService.getById(Integer.parseInt(institutionId)).isLBNL();

    if (((String) startParameterMap.get("OPTION_SECTION")).equals("FACULTY PREPAREDNESS")) {
        ((String[]) listMap.get("front"))[1] = "Front2-prepareness";
        if (isLbnl) {
            optionSection = "PI PREPAREDNESS";
        } else {
            optionSection = "FACULTY PREPAREDNESS";
        }
    } else if (((String) startParameterMap.get("OPTION_SECTION")).equals("INSTRUCTION")) {
        ((String[]) listMap.get("front"))[1] = "Front2";
        optionSection = "INSTRUCTION";
    }

    String systemName = this.systemDomainService.getSystemName(Integer.parseInt(institutionId));
    boolean customImage = false;

    if (this.systemDomainUIImageService.getImageByOwnerIdAndImageEnum(Integer.parseInt(institutionId),
            UIImageEnum.BANNER) != null) {
        customImage = true;
    }

    startParameterMap.put(ReportServiceEnum.IN_DIR.toString(), indir);
    startParameterMap.put(ReportServiceEnum.IMAGE_DIR.toString(), imageDir);
    startParameterMap.put(ReportServiceEnum.INSTITUTION_ID.toString(), instructionManageTool);
    startParameterMap.put(ReportServiceEnum.OPTION_SECTION.toString(), optionSection);
    startParameterMap.put(ReportServiceEnum.REPORT_DIR.toString(), reportDir);
    startParameterMap.put(ReportServiceEnum.TEAMS_SCREEN_FLAG.toString(), infoMap.get("TeamsScreenFlag"));
    startParameterMap.put(ReportServiceEnum.SKILLS_SCREEN_FLAG.toString(), infoMap.get("SkillsScreenFlag"));
    startParameterMap.put(ReportServiceEnum.STAFFING_SCREEN_FLAG.toString(), infoMap.get("StaffingScreenFlag"));
    startParameterMap.put(ReportServiceEnum.ACRONYM_PLUS_NAME.toString(), acronymPlusName);
    startParameterMap.put(ReportServiceEnum.SYSTEM_NAME.toString(), systemName);
    startParameterMap.put(ReportServiceEnum.DOMAIN_ID.toString(), institutionId);
    startParameterMap.put(ReportServiceEnum.LNBL.toString(), new Boolean(isLbnl));
    startParameterMap.put(ReportServiceEnum.CUSTOM_IMAGE.toString(), new Boolean(customImage));

    List<String> reports = reportList(section);
    try {
        int pg = 1;
        loadFonts(fontDir);
        PdfCopyFields copy = new PdfCopyFields(outStream);

        for (String report : reports) {
            thisrpt = report;
            System.out.println("runPrint: Processing: " + thisrpt);
            String pdfout = outFolder + report + ".pdf";

            pg = pg + jasperReportService.runAdd(indir + report + ".jrxml", copy, pg, startParameterMap);

            pdfs.add(pdfout);

        }
        copy.close();
        jasperReportService.closeStream(outStream);
    } catch (Exception e) {
        System.out.println("runPrint2: Run Exception: " + e + " processing " + thisrpt);
        e.printStackTrace();
    }
    GregorianCalendar then = new GregorianCalendar();
    Long diff = then.getTimeInMillis() - now.getTimeInMillis();
    Double difd = diff / 1000.0;
    System.out.println("RSJI: End time: " + then.getTime() + " difference: " + difd);

}