Search in sources :

Example 1 with PdfReader

use of com.lowagie.text.pdf.PdfReader in project spring-framework by spring-projects.

the class AbstractPdfStamperView method renderMergedOutputModel.

@Override
protected final void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
    // IE workaround: write into byte array first.
    ByteArrayOutputStream baos = createTemporaryOutputStream();
    PdfReader reader = readPdfResource();
    PdfStamper stamper = new PdfStamper(reader, baos);
    mergePdfDocument(model, stamper, request, response);
    stamper.close();
    // Flush to HTTP response.
    writeToResponse(response, baos);
}
Also used : PdfStamper(com.lowagie.text.pdf.PdfStamper) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PdfReader(com.lowagie.text.pdf.PdfReader)

Example 2 with PdfReader

use of com.lowagie.text.pdf.PdfReader in project adempiere by adempiere.

the class AEnv method mergePdf.

/**
     *
     * @param pdfList
     * @param outFile
     * @throws IOException
     * @throws DocumentException
     * @throws FileNotFoundException
     */
public static void mergePdf(List<File> pdfList, File outFile) throws IOException, DocumentException, FileNotFoundException {
    Document document = null;
    PdfWriter copy = null;
    for (File f : pdfList) {
        PdfReader reader = new PdfReader(f.getAbsolutePath());
        if (document == null) {
            document = new Document(reader.getPageSizeWithRotation(1));
            copy = PdfWriter.getInstance(document, new FileOutputStream(outFile));
            document.open();
        }
        int pages = reader.getNumberOfPages();
        PdfContentByte cb = copy.getDirectContent();
        for (int i = 1; i <= pages; i++) {
            document.newPage();
            PdfImportedPage page = copy.getImportedPage(reader, i);
            cb.addTemplate(page, 0, 0);
        }
    }
    document.close();
}
Also used : PdfImportedPage(com.lowagie.text.pdf.PdfImportedPage) PdfWriter(com.lowagie.text.pdf.PdfWriter) FileOutputStream(java.io.FileOutputStream) PdfContentByte(com.lowagie.text.pdf.PdfContentByte) PdfReader(com.lowagie.text.pdf.PdfReader) Document(com.lowagie.text.Document) File(java.io.File)

Example 3 with PdfReader

use of com.lowagie.text.pdf.PdfReader in project dhis2-core by dhis2.

the class PdfDataEntryFormImportUtil method importProgramStage.

// -------------------------------------------------------------------------
// METHODS
// -------------------------------------------------------------------------
@SuppressWarnings("unchecked")
public void importProgramStage(InputStream in, I18nFormat format) throws Exception {
    int programStageInstanceId;
    ProgramStageInstance programStageInstance;
    PdfReader reader = new PdfReader(in, null);
    AcroFields form = reader.getAcroFields();
    String strOrgID = form.getField(PdfDataEntryFormUtil.LABELCODE_ORGID);
    int organisationUnitId = Integer.parseInt(strOrgID);
    String strPeriodID = form.getField(PdfDataEntryFormUtil.LABELCODE_PERIODID);
    Date executionDateInput = format.parseDate(strPeriodID);
    Calendar executeDateCal = Calendar.getInstance();
    executeDateCal.setTime(executionDateInput);
    int programStageId = Integer.valueOf(form.getField(PdfDataEntryFormUtil.LABELCODE_PROGRAMSTAGEIDTEXTBOX));
    ProgramStageInstanceDataManager programStageInstanceDataManager = new ProgramStageInstanceDataManager();
    // Loop Through the Fields and get data.
    Set<String> fldNames = form.getFields().keySet();
    // Create Organized data
    for (String fldName : fldNames) {
        // If the value in the text field is not empty, proceed to add it.
        if (!form.getField(fldName).trim().isEmpty()) {
            if (fldName.startsWith(PdfDataEntryFormUtil.LABELCODE_DATADATETEXTFIELD)) {
                String[] strArrFldName = fldName.split("_");
                int rowNumber = Integer.parseInt(strArrFldName[1]);
                programStageInstanceDataManager.addDateData(Integer.parseInt(form.getField(fldName)), rowNumber);
            } else if (fldName.startsWith(PdfDataEntryFormUtil.LABELCODE_DATAENTRYTEXTFIELD)) {
                String[] strArrFldName = fldName.split("_");
                int dataElementId = Integer.parseInt(strArrFldName[1]);
                int rowNumber = Integer.parseInt(strArrFldName[2]);
                programStageInstanceDataManager.addDataElementData(dataElementId, form.getField(fldName), rowNumber);
            }
        }
    }
    // For each row, add new programStageInstance and add data elements to it.
    for (Map.Entry<Integer, ProgramStageInstanceStorage> entry : programStageInstanceDataManager.getProgramStageInstanceData().entrySet()) {
        ProgramStageInstanceStorage programStageInstanceStorage = entry.getValue();
        int date = programStageInstanceStorage.getDate();
        executeDateCal.set(Calendar.DATE, date);
        Date executionDate = executeDateCal.getTime();
        // Step 2. Create Program Stage Instance - If does not exists
        // already.
        programStageInstanceId = addNewProgramStageInstance(programStageId, organisationUnitId, executionDate);
        programStageInstance = programStageInstanceService.getProgramStageInstance(programStageInstanceId);
        for (Map.Entry<Integer, String> dataElementsEntry : programStageInstanceStorage.getDataElementsValue().entrySet()) {
            Integer dataElementId = dataElementsEntry.getKey();
            String value = dataElementsEntry.getValue();
            // Step 3. Insert Data
            insertValueProgramStageDataElement(programStageInstance, dataElementId, value);
        }
    }
    reader.close();
}
Also used : Calendar(java.util.Calendar) PdfReader(com.lowagie.text.pdf.PdfReader) Date(java.util.Date) AcroFields(com.lowagie.text.pdf.AcroFields) HashMap(java.util.HashMap) Map(java.util.Map) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance)

Example 4 with PdfReader

use of com.lowagie.text.pdf.PdfReader in project dhis2-core by dhis2.

the class PdfDataEntryFormUtil method getDataValueSet.

/**
     * Creates data value set from Input Stream (PDF) for PDF data import
     */
public static DataValueSet getDataValueSet(InputStream in) {
    PdfReader reader = null;
    DataValueSet dataValueSet = new DataValueSet();
    List<org.hisp.dhis.dxf2.datavalue.DataValue> dataValueList = new ArrayList<>();
    try {
        reader = new PdfReader(in);
        AcroFields form = reader.getAcroFields();
        if (form != null) {
            // Process OrgUnitUID and PeriodID from the PDF Form
            String orgUnitUid = form.getField(PdfDataEntryFormUtil.LABELCODE_ORGID).trim();
            String periodId = form.getField(PdfDataEntryFormUtil.LABELCODE_PERIODID).trim();
            if (periodId == null || periodId.isEmpty()) {
                throw new InvalidIdentifierReferenceException(ERROR_EMPTY_PERIOD);
            }
            if (orgUnitUid == null || orgUnitUid.isEmpty()) {
                throw new InvalidIdentifierReferenceException(ERROR_EMPTY_ORG_UNIT);
            }
            Period period = PeriodType.getPeriodFromIsoString(periodId);
            if (period == null) {
                throw new InvalidIdentifierReferenceException(ERROR_INVALID_PERIOD + periodId);
            }
            // Loop Through the Fields and get data.
            @SuppressWarnings("unchecked") Set<String> fldNames = form.getFields().keySet();
            for (String fldName : fldNames) {
                if (fldName.startsWith(PdfDataEntryFormUtil.LABELCODE_DATAENTRYTEXTFIELD)) {
                    String[] strArrFldName = fldName.split("_");
                    org.hisp.dhis.dxf2.datavalue.DataValue dataValue = new org.hisp.dhis.dxf2.datavalue.DataValue();
                    dataValue.setDataElement(strArrFldName[1]);
                    dataValue.setCategoryOptionCombo(strArrFldName[2]);
                    dataValue.setOrgUnit(orgUnitUid);
                    dataValue.setPeriod(period.getIsoDate());
                    dataValue.setValue(fieldValueFormat(strArrFldName, form.getField(fldName)));
                    dataValue.setStoredBy(DATAVALUE_IMPORT_STOREBY);
                    dataValue.setComment(DATAVALUE_IMPORT_COMMENT);
                    dataValue.setFollowup(false);
                    dataValue.setLastUpdated(DateUtils.getMediumDateString());
                    dataValueList.add(dataValue);
                }
            }
            dataValueSet.setDataValues(dataValueList);
        } else {
            throw new RuntimeException("Could not generate PDF AcroFields form from input");
        }
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    } finally {
        if (reader != null) {
            reader.close();
        }
    }
    return dataValueSet;
}
Also used : DataValueSet(org.hisp.dhis.dxf2.datavalueset.DataValueSet) ArrayList(java.util.ArrayList) Period(org.hisp.dhis.period.Period) PdfReader(com.lowagie.text.pdf.PdfReader) InvalidIdentifierReferenceException(org.hisp.dhis.common.exception.InvalidIdentifierReferenceException) AcroFields(com.lowagie.text.pdf.AcroFields) InvalidIdentifierReferenceException(org.hisp.dhis.common.exception.InvalidIdentifierReferenceException)

Aggregations

PdfReader (com.lowagie.text.pdf.PdfReader)4 AcroFields (com.lowagie.text.pdf.AcroFields)2 Document (com.lowagie.text.Document)1 PdfContentByte (com.lowagie.text.pdf.PdfContentByte)1 PdfImportedPage (com.lowagie.text.pdf.PdfImportedPage)1 PdfStamper (com.lowagie.text.pdf.PdfStamper)1 PdfWriter (com.lowagie.text.pdf.PdfWriter)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 InvalidIdentifierReferenceException (org.hisp.dhis.common.exception.InvalidIdentifierReferenceException)1 DataValueSet (org.hisp.dhis.dxf2.datavalueset.DataValueSet)1 Period (org.hisp.dhis.period.Period)1 ProgramStageInstance (org.hisp.dhis.program.ProgramStageInstance)1