Esempio n. 1
0
  private File convertPdfToPdfA(final InputStream source) throws IOException, DocumentException {

    final File pdfAFile =
        TempFileProvider.createTempFile("digitalSigning-" + System.currentTimeMillis(), ".pdf");

    // Reads a PDF document.
    PdfReader reader = new PdfReader(source);
    // PdfStamper: Applies extra content to the pages of a PDF document. This extra content can be
    // all the objects allowed in
    // PdfContentByte including pages from other Pdfs.
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(pdfAFile));
    // A generic Document class.
    Document document = new Document();
    // we create a writer that listens to the document
    PdfWriter writer = stamper.getWriter();
    int numberPages = reader.getNumberOfPages();
    writer.setPDFXConformance(PdfWriter.PDFA1A);
    document.open();

    // PdfDictionary:A dictionary is an associative table containing pairs of objects.
    // The first element of each pair is called the key and the second  element is called the value
    // <CODE>PdfName</CODE> is an object that can be used as a name in a PDF-file
    PdfDictionary outi = new PdfDictionary(PdfName.OUTPUTINTENT);
    outi.put(PdfName.OUTPUTCONDITIONIDENTIFIER, new PdfString("sRGB IEC61966-2.1"));
    outi.put(PdfName.INFO, new PdfString("sRGB IEC61966-2.1"));
    outi.put(PdfName.S, PdfName.GTS_PDFA1);
    writer.getExtraCatalog().put(PdfName.OUTPUTINTENTS, new PdfArray(outi));

    // Add pages
    PdfImportedPage p = null;
    Image image;
    for (int i = 0; i < numberPages; i++) {
      p = writer.getImportedPage(reader, i + 1);
      image = Image.getInstance(p);
      document.add(image);
    }
    writer.createXmpMetadata();

    document.close();

    // Add Metadata from source pdf
    HashMap<String, String> info = reader.getInfo();
    stamper.setMoreInfo(info);
    stamper.close();

    return pdfAFile;
  }