br.unifor.mia.xmpsemantico.xmp.MetadataXmp.java Source code

Java tutorial

Introduction

Here is the source code for br.unifor.mia.xmpsemantico.xmp.MetadataXmp.java

Source

package br.unifor.mia.xmpsemantico.xmp;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.text.Anchor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.xml.xmp.XmpWriter;

/**
* This class was created to discipline the Semantic Web 
* MSc in Applied Informatics of Unifor - University of Fortaleza 
* As part of the research on semantic processing files 
* PDF's using based on the standard ISO XMP, and the iText framework 
* that is about the AGPL license, product version 5.5.2 
 * written by: @author patrick.cunha [adrianopatrick@gmail.com]
 * @since 10/09/2014
 */
@SuppressWarnings("deprecation")
public class MetadataXmp {

    public static final String ARQUIVO_PDF = "pdfsemantico/arquivo.pdf";

    public static final String ARQUIVO_XMP = "pdfsemantico/xmp.xml";

    private String texto;
    private String descricao;
    private String disciplina;
    private String professor;
    private String cargaHoraria;
    private String creditos;
    private String pathPdf;
    private String pathXMP;

    /**
     * create PDF document
     * @param filename of new archive PDF
     * @throws DocumentException 
     * @throws IOException 
     */
    public void createPdf() throws IOException {

        Document document = new Document();
        PdfWriter writer = null;
        try {
            writer = PdfWriter.getInstance(document, new FileOutputStream(pathPdf));

            ByteArrayOutputStream os = new ByteArrayOutputStream();
            XmpWriter xmp = new XmpWriter(os);

            MiaSchema miaSchema = new MiaSchema();
            miaSchema.addDescription(this.descricao);
            miaSchema.setProperty(MiaSchema.DISCIPLINA, this.disciplina);
            miaSchema.setProperty(MiaSchema.PROFESSOR, this.professor);
            miaSchema.setProperty(MiaSchema.CARGA_HORARIA, this.cargaHoraria);
            miaSchema.setProperty(MiaSchema.CREDITOS, this.creditos);
            xmp.addRdfDescription(miaSchema);

            xmp.close();
            writer.setXmpMetadata(os.toByteArray());

            document.open();
            document.addAuthor("docsemantico");
            Paragraph paragraph = new Paragraph("Quick brown ");
            Anchor foxRefence = new Anchor("fox");
            foxRefence.setReference("#fox");
            paragraph.add(foxRefence);
            paragraph.add(" jumps over the lazy dog.");
            document.add(paragraph);
            document.newPage();
            Anchor foxName = new Anchor("This is the FOX");
            foxName.setName("fox");
            document.add(foxName);
            document.add(new Paragraph(this.texto));

            document.close();
        } catch (DocumentException e) {
            e.printStackTrace();
        }

        //teste commit
    }

    /**
     * Reads the XML stream inside a PDF file into an XML file.
     * @param src  A PDF file containing XMP data
     * @param dest XML file containing the XMP data extracted from the PDF
     * @throws IOException
     */
    public void readXmpMetadata() throws IOException {
        PdfReader reader = new PdfReader(pathPdf);
        FileOutputStream fos = new FileOutputStream(pathXMP);
        byte[] b = reader.getMetadata();
        fos.write(b, 0, b.length);
        fos.flush();
        fos.close();
        reader.close();
    }

    public MetadataXmp criaDisciplina(String disciplina) {
        this.disciplina = disciplina;
        return this;
    }

    public MetadataXmp comDescricao(String descricao) {
        this.descricao = descricao;
        return this;
    }

    public MetadataXmp cujoProfessor(String professor) {
        this.professor = professor;
        return this;
    }

    public MetadataXmp possuiCargaHoraria(String cargaHoraria) {
        this.cargaHoraria = cargaHoraria;
        return this;
    }

    public MetadataXmp comCreditos(String creditos) {
        this.creditos = creditos;
        return this;
    }

    public void setTexto(String texto) {
        this.texto = texto;
    }

    public MetadataXmp comPathPdf(String pathPdf) {
        this.pathPdf = pathPdf;
        return this;
    }

    public MetadataXmp comPathXMP(String pathXMP) {
        this.pathXMP = pathXMP;
        return this;
    }

    /**
     * Main method.
     *
     * @param    args    no arguments needed
     * @throws DocumentException 
     * @throws IOException
     */
    //    public static void main(String[] args) throws IOException, DocumentException {
    //        MetadataXmp metadata = new MetadataXmp();
    //        metadata.createPdf(ARQUIVO_PDF);
    //        metadata.readXmpMetadata(ARQUIVO_PDF, ARQUIVO_XMP);
    //    }
}