Search in sources :

Example 11 with PdfContentByte

use of com.itextpdf.text.pdf.PdfContentByte in project clusterMaker2 by RBVI.

the class GraphicsExportPanel method pdfSave.

private void pdfSave(String format) {
    com.itextpdf.text.Rectangle pageSize = PageSize.LETTER;
    Document document = new Document(pageSize);
    try {
        OutputStream output = new BufferedOutputStream(new FileOutputStream(getFile()));
        PdfWriter writer = PdfWriter.getInstance(document, output);
        document.open();
        PdfContentByte cb = writer.getDirectContent();
        Graphics2D g = cb.createGraphics(pageSize.getWidth(), pageSize.getHeight(), new DefaultFontMapper());
        double imageScale = Math.min(pageSize.getWidth() / ((double) estimateWidth() + getBorderPixels()), pageSize.getHeight() / ((double) estimateHeight() + getBorderPixels()));
        g.scale(imageScale, imageScale);
        drawAll(g, 1.0);
        g.dispose();
    } catch (Exception e) {
        JOptionPane.showMessageDialog(this, new JTextArea("Dendrogram export had problem " + e));
    // logger.error("Exception " + e);
    // e.printStackTrace();
    }
    document.close();
}
Also used : JTextArea(javax.swing.JTextArea) PdfWriter(com.itextpdf.text.pdf.PdfWriter) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) DefaultFontMapper(com.itextpdf.awt.DefaultFontMapper) Document(com.itextpdf.text.Document) SVGGraphics2D(org.freehep.graphicsio.svg.SVGGraphics2D) Graphics2D(java.awt.Graphics2D) PSGraphics2D(org.freehep.graphicsio.ps.PSGraphics2D) FileOutputStream(java.io.FileOutputStream) PdfContentByte(com.itextpdf.text.pdf.PdfContentByte) BufferedOutputStream(java.io.BufferedOutputStream)

Example 12 with PdfContentByte

use of com.itextpdf.text.pdf.PdfContentByte in project trainning by fernandotomasio.

the class ApostilaDECEAPageFooter method onEndPage.

@Override
public void onEndPage(PdfWriter writer, Document document) {
    PdfContentByte canvas = writer.getDirectContentUnder();
    Image imageBottom;
    Image imageTop;
    Image imageCover;
    try {
        imageTop = Image.getInstance(IMAGE_TOP);
        imageTop.scaleAbsoluteHeight(42);
        imageBottom = Image.getInstance(IMAGE_BOTTOM);
        imageBottom.scaleAbsoluteHeight(14);
        imageCover = Image.getInstance(IMAGE_COVER);
        imageCover.scalePercent(30.3f);
        if (writer.getPageNumber() > 2) {
            // image.scaleAbsolute(PageSize.A4.rotate());
            imageBottom.setAbsolutePosition(0, 0);
            imageTop.setAbsolutePosition(0, 800);
            canvas.addImage(imageBottom);
            canvas.addImage(imageTop);
        } else {
            if (writer.getPageNumber() == 1) {
                imageCover.setAbsolutePosition(0, 490);
                canvas.addImage(imageCover);
            }
        }
    } catch (BadElementException ex) {
        Logger.getLogger(ApostilaDECEAPageFooter.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(ApostilaDECEAPageFooter.class.getName()).log(Level.SEVERE, null, ex);
    } catch (DocumentException ex) {
        Logger.getLogger(ApostilaDECEAPageFooter.class.getName()).log(Level.SEVERE, null, ex);
    }
    PdfPTable table = new PdfPTable(1);
    table.setTotalWidth(527);
    table.setLockedWidth(true);
    table.getDefaultCell().setFixedHeight(20);
    table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
    if (writer.getPageNumber() > 2) {
        PdfPCell numberCell = new PdfPCell(new Phrase(String.format("%d", writer.getPageNumber()), new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD, new BaseColor(0, 129, 201))));
        numberCell.setHorizontalAlignment(Element.ALIGN_CENTER);
        numberCell.setBorder(Rectangle.NO_BORDER);
        table.addCell(numberCell);
    }
    table.writeSelectedRows(0, -1, 0, 34, writer.getDirectContent());
}
Also used : PdfPCell(com.itextpdf.text.pdf.PdfPCell) BaseColor(com.itextpdf.text.BaseColor) PdfPTable(com.itextpdf.text.pdf.PdfPTable) BadElementException(com.itextpdf.text.BadElementException) DocumentException(com.itextpdf.text.DocumentException) PdfContentByte(com.itextpdf.text.pdf.PdfContentByte) IOException(java.io.IOException) Phrase(com.itextpdf.text.Phrase) Image(com.itextpdf.text.Image) Font(com.itextpdf.text.Font)

Example 13 with PdfContentByte

use of com.itextpdf.text.pdf.PdfContentByte in project trainning by fernandotomasio.

the class DOC002PDF method makeReport.

@Override
public void makeReport() {
    teachingDocumentsService = (TeachingDocumentsService) services.get("teachingDocumentsService");
    document = documentManager.getDocumentPortrait();
    os = documentManager.prepareDocument(document);
    writer = documentManager.getWritter();
    ChapterSectionTOC eventHandler = new ChapterSectionTOC();
    writer.setPageEvent(eventHandler);
    document.open();
    try {
        String curriculoMinimoIdParam = (String) params.get("curriculoMinimoId");
        Long curriculoMinimoId = Long.parseLong(curriculoMinimoIdParam);
        CurriculoMinimoDTO curriculoMinimo = teachingDocumentsService.findCurriculoMinimo(curriculoMinimoId);
        buildCover(document, curriculoMinimo);
        buildFrontispicio(document, curriculoMinimo);
        document.add(buildPrefacio(curriculoMinimo));
        document.add(Chunk.NEXTPAGE);
        document.add(buildAbreviaturas(curriculoMinimo));
        document.add(Chunk.NEXTPAGE);
        document.add(buildDisposicoesPreliminares(curriculoMinimo));
        document.add(Chunk.NEXTPAGE);
        document.add(buildAtividadesComplementares(curriculoMinimo));
        document.add(Chunk.NEXTPAGE);
        document.add(buildComplementacaoInstrucao(curriculoMinimo));
        document.add(Chunk.NEXTPAGE);
        // PdfPTable tableAtividadesAvaliativas = buildAtividadesAvaliativasTable(curriculoMinimo);
        // document.add(tableAtividadesAvaliativas);
        Paragraph p12 = new Paragraph();
        p12.add(new Phrase("4 DETALHAMENTO DAS UNIDADES DIDÁTICAS", fontManager.getBoldFont()));
        document.add(p12);
        document.add(Chunk.NEWLINE);
        DisciplinaDTO[] disciplinas = teachingDocumentsService.findAllDisciplinasByCurriculoMinimo(curriculoMinimo.getId());
        for (DisciplinaDTO disciplina : disciplinas) {
            PdfPTable tableDisciplina = buildTableDisciplina(disciplina);
            document.add(tableDisciplina);
            document.add(Chunk.NEWLINE);
            UnidadeDidaticaDTO[] unidades = teachingDocumentsService.findAllUnidadesDidaticas(disciplina.getId());
            PdfPTable unidadesHeaderTable = new PdfPTable(1);
            unidadesHeaderTable.setWidthPercentage(100);
            PdfPCell p20 = new PdfPCell(new Phrase("UNIDADES DIDÁTICAS", fontManager.getBoldFont()));
            unidadesHeaderTable.addCell(p20);
            unidadesHeaderTable.setHorizontalAlignment(Element.ALIGN_CENTER);
            document.add(unidadesHeaderTable);
            document.add(Chunk.NEWLINE);
            for (UnidadeDidaticaDTO unidade : unidades) {
                PdfPTable tableUnidade = buildTableUnidade(unidade);
                document.add(tableUnidade);
                document.add(Chunk.NEWLINE);
            }
            PdfPTable tableRecomendacoes = buildTableRecomendacoesMetodologicas(disciplina);
            document.add(tableRecomendacoes);
            document.add(Chunk.NEWLINE);
            PdfPTable perfisTable = buildTablePerfisRelacionamento(disciplina);
            document.add(perfisTable);
            document.add(Chunk.NEWLINE);
            PdfPTable referenciasTable = buildTableReferencias(disciplina);
            document.add(referenciasTable);
            document.add(Chunk.NEXTPAGE);
        }
        document.add(Chunk.NEXTPAGE);
        document.add(buildDisposicoesFinais(curriculoMinimo));
        document.add(Chunk.NEWLINE);
        document.add(Chunk.NEWLINE);
        document.add(Chunk.NEWLINE);
        document.add(buildAssinatura());
        document.add(Chunk.NEXTPAGE);
        document.add(buildSumario(eventHandler.titles));
    } catch (DocumentException e) {
        Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, e);
        throw new CoreException(e.getMessage());
    } finally {
        document.close();
    }
    try {
        PdfReader reader = new PdfReader(os.toByteArray());
        int n = reader.getNumberOfPages();
        String pagesInterval = String.format("1-4,%d, 5-%d", n, n - 1);
        reader.selectPages(pagesInterval);
        os = new ByteArrayOutputStream();
        PdfStamper stamper = new PdfStamper(reader, os);
        stamper.close();
        reader.close();
    } catch (IOException ex) {
        Logger.getLogger(DOC002PDF.class.getName()).log(Level.SEVERE, null, ex);
    } catch (DocumentException ex) {
        Logger.getLogger(DOC002PDF.class.getName()).log(Level.SEVERE, null, ex);
    }
    try {
        PdfReader reader = new PdfReader(os.toByteArray());
        os = new ByteArrayOutputStream();
        PdfStamper stamper = new PdfStamper(reader, os);
        int total = reader.getNumberOfPages();
        PdfContentByte over = null;
        int total_pages = total - 1;
        for (int i = 1; i < total; i++) {
            int actual_page = i - 1;
            if (i > 5) {
                over = stamper.getOverContent(i);
                System.out.println(over);
                over.beginText();
                BaseFont baseFont = BaseFont.createFont(BaseFont.TIMES_ROMAN, "UTF-8", false);
                over.setFontAndSize(baseFont, 12);
                if ((i % 2) == 0) {
                    over.showTextAligned(PdfContentByte.ALIGN_RIGHT, actual_page + "/" + total_pages, 538, 803, 0);
                } else {
                    over.showTextAligned(PdfContentByte.ALIGN_LEFT, actual_page + "/" + total_pages, 58, 803, 0);
                }
                over.endText();
            }
        }
        // PdfContentByte content = stamper.getOverContent(1);
        // ColumnText ct = new ColumnText( content );
        // ct.setSimpleColumn(50,50,50,50);
        // ct.setText(new Phrase("TESTANDO"));
        // ct.go();
        stamper.close();
        reader.close();
    } catch (IOException ex) {
    // Logger.getLogger(DOC001PDF.class.getName()).log(Level.SEVERE, null, ex);
    } catch (DocumentException ex) {
        Logger.getLogger(DOC001PDF.class.getName()).log(Level.SEVERE, null, ex);
    }
}
Also used : PdfPCell(com.itextpdf.text.pdf.PdfPCell) Phrase(com.itextpdf.text.Phrase) PdfReader(com.itextpdf.text.pdf.PdfReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) UnidadeDidaticaDTO(com.tomasio.projects.trainning.dto.UnidadeDidaticaDTO) ObjetivoUnidadeDidaticaDTO(com.tomasio.projects.trainning.dto.ObjetivoUnidadeDidaticaDTO) Paragraph(com.itextpdf.text.Paragraph) PdfPTable(com.itextpdf.text.pdf.PdfPTable) CoreException(com.tomasio.projects.trainning.exeption.CoreException) PdfStamper(com.itextpdf.text.pdf.PdfStamper) DocumentException(com.itextpdf.text.DocumentException) DisciplinaDTO(com.tomasio.projects.trainning.dto.DisciplinaDTO) ObjetivoDisciplinaDTO(com.tomasio.projects.trainning.dto.ObjetivoDisciplinaDTO) BaseFont(com.itextpdf.text.pdf.BaseFont) CurriculoMinimoDTO(com.tomasio.projects.trainning.dto.CurriculoMinimoDTO) PdfContentByte(com.itextpdf.text.pdf.PdfContentByte)

Example 14 with PdfContentByte

use of com.itextpdf.text.pdf.PdfContentByte in project trainning by fernandotomasio.

the class TCATrainningDocument method createAnexo3.

private void createAnexo3(String file) {
    try {
        document.newPage();
        PdfReader reader = new PdfReader(file);
        int n = reader.getNumberOfPages();
        PdfImportedPage page;
        for (int i = 1; i <= n; i++) {
            page = writer.getImportedPage(reader, i);
            // Image imagePage = Image.getInstance(page);
            // imagePage.scaleToFit(PageSize.A4.getWidth(), PageSize.A4.getHeight());
            PdfContentByte cb = writer.getDirectContent();
            cb.addTemplate(page, 0, 0);
            document.newPage();
        // document.add(imagePage);
        }
    } catch (IOException ex) {
        Logger.getLogger(TCATrainningDocument.class.getName()).log(Level.SEVERE, null, ex);
    }
}
Also used : PdfImportedPage(com.itextpdf.text.pdf.PdfImportedPage) PdfContentByte(com.itextpdf.text.pdf.PdfContentByte) PdfReader(com.itextpdf.text.pdf.PdfReader) IOException(java.io.IOException)

Example 15 with PdfContentByte

use of com.itextpdf.text.pdf.PdfContentByte in project trainning by fernandotomasio.

the class TCATrainningDocument method createPrincipal.

private void createPrincipal(String file) {
    try {
        PdfReader reader = new PdfReader(file);
        PdfPTable t1 = new PdfPTable(2);
        int n = reader.getNumberOfPages();
        PdfImportedPage page;
        for (int i = 1; i <= n; i++) {
            page = writer.getImportedPage(reader, i);
            // Image imagePage = Image.getInstance(page);
            // imagePage.scaleToFit(PageSize.A4.getWidth(), PageSize.A4.getHeight());
            PdfContentByte cb = writer.getDirectContent();
            cb.addTemplate(page, 0, 0);
            document.newPage();
        // document.add(imagePage);
        }
    } catch (IOException ex) {
        Logger.getLogger(TCATrainningDocument.class.getName()).log(Level.SEVERE, null, ex);
    }
}
Also used : PdfImportedPage(com.itextpdf.text.pdf.PdfImportedPage) PdfPTable(com.itextpdf.text.pdf.PdfPTable) PdfContentByte(com.itextpdf.text.pdf.PdfContentByte) PdfReader(com.itextpdf.text.pdf.PdfReader) IOException(java.io.IOException)

Aggregations

PdfContentByte (com.itextpdf.text.pdf.PdfContentByte)29 IOException (java.io.IOException)13 DocumentException (com.itextpdf.text.DocumentException)10 Document (com.itextpdf.text.Document)9 PdfWriter (com.itextpdf.text.pdf.PdfWriter)9 BaseFont (com.itextpdf.text.pdf.BaseFont)8 PdfReader (com.itextpdf.text.pdf.PdfReader)7 FileOutputStream (java.io.FileOutputStream)7 Font (com.itextpdf.text.Font)6 Rectangle (com.itextpdf.text.Rectangle)6 PdfImportedPage (com.itextpdf.text.pdf.PdfImportedPage)5 PdfTemplate (com.itextpdf.text.pdf.PdfTemplate)5 Graphics2D (java.awt.Graphics2D)5 Paragraph (com.itextpdf.text.Paragraph)4 PdfGState (com.itextpdf.text.pdf.PdfGState)4 PdfPTable (com.itextpdf.text.pdf.PdfPTable)4 PdfGraphics2D (com.itextpdf.awt.PdfGraphics2D)3 Image (com.itextpdf.text.Image)3 Phrase (com.itextpdf.text.Phrase)3 Color (java.awt.Color)3