/** * */ package at.gv.egiz.pdfas.impl.input.correction; import java.io.ByteArrayOutputStream; import java.io.IOException; import at.gv.egiz.pdfas.exceptions.ErrorCode; import at.gv.egiz.pdfas.exceptions.framework.CorrectorException; import at.gv.egiz.pdfas.framework.input.PdfDataSource; import at.gv.egiz.pdfas.framework.input.correction.Corrector; import at.gv.egiz.pdfas.impl.input.ByteArrayPdfDataSourceImpl; import com.lowagie.text.DocumentException; import com.lowagie.text.pdf.PdfReader; import com.lowagie.text.pdf.PdfStamper; /** * Corrects a document using iText. * * @author wprinz */ public class InternalCorrector implements Corrector { /** * @see at.gv.egiz.pdfas.framework.input.correction.Corrector#correctDocument(at.gv.egiz.pdfas.framework.input.PdfDataSource) */ public PdfDataSource correctDocument(PdfDataSource document) throws CorrectorException { try { byte[] pdf = document.getAsByteArray(); PdfReader reader = new PdfReader(pdf); ByteArrayOutputStream baos = new ByteArrayOutputStream(pdf.length); PdfStamper stamper = new PdfStamper(reader, baos, '\0', false); stamper.close(); baos.close(); byte[] corrected_pdf = baos.toByteArray(); return new ByteArrayPdfDataSourceImpl(corrected_pdf); } catch (DocumentException e) { throw new CorrectorException(ErrorCode.CORRECTOR_EXCEPTION, e); } catch (IOException e) { throw new CorrectorException(ErrorCode.CORRECTOR_EXCEPTION, e); } } }