Example usage for org.apache.pdfbox.pdmodel PDPage setRotation

List of usage examples for org.apache.pdfbox.pdmodel PDPage setRotation

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel PDPage setRotation.

Prototype

public void setRotation(int rotation) 

Source Link

Document

This will set the rotation for this page.

Usage

From source file:Reports.RecycleDataReader.java

public void PdfGenerator(String pdfFileNameBase, List<String> cycles) throws IOException, COSVisitorException {
    GlobalVar.dirMake(new File(pdfFileNameBase)); //create a folder with the same name                          
    int rowCount = 0;
    int pageCount = 1;
    PDPage page; //default size PAGE_SIZE_A4
    PDPageContentStream stream;/*from  w  ww. j av a  2  s.co  m*/
    //page.setRotation(90); //counterclock wise rotate 90 degree  ////left hand rule

    //        stream.setFont(PDType1Font.COURIER, FONT_SIZE);
    String lastUpdate = null;
    String lastCycle = null;
    System.out.println("RECYCLE_DATA keyset:" + RECYCLE_DATA.keySet());
    System.out.println("Cycles empty? " + cycles.isEmpty() + cycles.size());
    System.out.println(cycles);
    for (String updateDate : RECYCLE_DATA.keySet()) {
        Map<String, List<String>> thisDTData = RECYCLE_DATA.get(updateDate);
        lastUpdate = updateDate;
        System.out.println("thisDT_DATA keyset:" + thisDTData.keySet());
        for (String cycle : thisDTData.keySet()) {
            if (cycles.isEmpty() || cycles.contains(cycle)) {
                PDDocument pdf = new PDDocument();
                lastCycle = cycle;
                List<String> thisCycle = thisDTData.get(cycle);
                pageCount = 1; // new cycle, restart page num
                page = new PDPage(); //page break
                stream = new PDPageContentStream(pdf, page, true, false);
                stream.beginText();
                page.setRotation(PAGE_ROT_DEGREE);
                //stream.setFont(PDType1Font.COURIER, FONT_SIZE);
                addBigFontUpdateNumberAndCycle(updateDate, cycle, stream);
                stream.setFont(PDType1Font.COURIER, FONT_SIZE);
                // stream.setFont(PDType1Font.
                int thisCycleRowCount = 0;
                for (String row : thisCycle) {
                    if (thisCycleRowCount > MAX_NUM_TRANS) {
                        //close the current page
                        setupFootNote(stream, pageCount, cycle, updateDate);
                        pageCount++;
                        stream.endText();
                        stream.close();
                        pdf.addPage(page);

                        // start a new page
                        page = new PDPage();
                        stream = new PDPageContentStream(pdf, page, true, false);
                        page.setRotation(PAGE_ROT_DEGREE);
                        stream.beginText();
                        stream.setFont(PDType1Font.COURIER, FONT_SIZE);
                        thisCycleRowCount = 0;
                    }
                    stream.setTextRotation(TEXT_ROT_RAD, TRANS_X + thisCycleRowCount * INVERVAL_X, TRANS_Y);
                    stream.drawString(row);
                    thisCycleRowCount++;
                    //System.out.println("Update:" + updateDate + " Cycle: " + cycle + " " + row);
                }
                setupFootNote(stream, pageCount, lastCycle, lastUpdate);
                stream.endText();
                stream.close();
                pdf.addPage(page);
                String pdfFileName = pdfFileNameBase + "\\RECYCLE UPDT " + updateDate + " " + cycle + ".pdf";

                pdf.save(pdfFileName);
                pdf.close();
            }
        }
    }

    // pdf.save(pdfFileName);        
    // pdf.close();      
}

From source file:Reports.RejectDataReader.java

public void PdfGenerator(String pdfFileNameBase, List<String> cycles) throws IOException, COSVisitorException {
    GlobalVar.dirMake(new File(pdfFileNameBase)); //create a folder with the same name                          
    int rowCount = 0;
    int pageCount = 1;
    PDPage page; //default size PAGE_SIZE_A4
    PDPageContentStream stream;/* w  w  w . ja  va 2  s .c  o m*/
    //page.setRotation(90); //counterclock wise rotate 90 degree  ////left hand rule

    //        stream.setFont(PDType1Font.COURIER, FONT_SIZE);
    String lastUpdate = null;
    String lastCycle = null;
    System.out.println("REJECT_DATA keyset:" + REJECT_DATA.keySet());
    System.out.println("Cycles empty? " + cycles.isEmpty() + cycles.size());
    System.out.println(cycles);
    for (String updateDate : REJECT_DATA.keySet()) {
        Map<String, List<String>> thisDTData = REJECT_DATA.get(updateDate);
        lastUpdate = updateDate;
        System.out.println("thisDT_DATA keyset:" + thisDTData.keySet());
        for (String cycle : thisDTData.keySet()) {
            if (cycles.isEmpty() || cycles.contains(cycle)) {
                PDDocument pdf = new PDDocument();
                lastCycle = cycle;
                List<String> thisCycle = thisDTData.get(cycle);
                pageCount = 1; // new cycle, restart page num
                page = new PDPage(); //page break
                stream = new PDPageContentStream(pdf, page, true, false);
                stream.beginText();
                page.setRotation(PAGE_ROT_DEGREE);
                //stream.setFont(PDType1Font.COURIER, FONT_SIZE);
                addBigFontUpdateNumberAndCycle(updateDate, cycle, stream);
                stream.setFont(PDType1Font.COURIER, FONT_SIZE);
                // stream.setFont(PDType1Font.
                int thisCycleRowCount = 0;
                for (String row : thisCycle) {
                    if (thisCycleRowCount > MAX_NUM_TRANS) {
                        //close the current page
                        setupFootNote(stream, pageCount, cycle, updateDate);
                        pageCount++;
                        stream.endText();
                        stream.close();
                        pdf.addPage(page);

                        // start a new page
                        page = new PDPage();
                        stream = new PDPageContentStream(pdf, page, true, false);
                        page.setRotation(PAGE_ROT_DEGREE);
                        stream.beginText();
                        stream.setFont(PDType1Font.COURIER, FONT_SIZE);
                        thisCycleRowCount = 0;
                    }
                    stream.setTextRotation(TEXT_ROT_RAD, TRANS_X + thisCycleRowCount * INVERVAL_X, TRANS_Y);
                    stream.drawString(row);
                    thisCycleRowCount++;
                    //System.out.println("Update:" + updateDate + " Cycle: " + cycle + " " + row);
                }
                setupFootNote(stream, pageCount, lastCycle, lastUpdate);
                stream.endText();
                stream.close();
                pdf.addPage(page);
                String pdfFileName = pdfFileNameBase + "\\REJECT UPDT " + updateDate + " " + cycle + ".pdf";

                pdf.save(pdfFileName);
                pdf.close();
            }
        }
    }

    // pdf.save(pdfFileName);        
    // pdf.close();      
}

From source file:src.controller.PageController.java

/**
 * Tourne la page spcifie//  w ww  .j  a v a2s .  c  om
 * @param document
 * @param id
 * @param degree 
 */
public void rotatePage(PDDocument document, int id, int degree) {
    PDPage page = document.getPage(id);
    page.setRotation(page.getRotation() + degree);
}

From source file:stepReport.reports.model.savePDFModel.java

public void savePDFSemanal(File file, String[][] matrizDados) {

    if (matrizDados == null)
        return;//from ww  w . ja va2 s  . c  o  m

    //Cria o documento
    PDDocument document = new PDDocument();

    //Vou criando as paginas dinamicamente de acordo com o numero de registros a serem impressos.
    //Para cada 25 registros, crio uma nova pagina
    //O valor de k vai ser atualizado durante o loop de impressao de registros,
    //assim como o loop de impressao de registro comeca a partir do valor de k
    int k = 1;
    int pagina = 0;
    while (k < matrizDados.length) {
        //Variavel com o numero da pagina
        pagina++;
        //Adiciona uma pagina
        PDPage page = new PDPage();
        //Configura o padrao de tamanho da pagina
        page.setMediaBox(PDRectangle.A4);
        //Configura a orientacao
        page.setRotation(90);
        //Adiciona a pagina ao documento
        document.addPage(page);
        PDFont font;
        //Obtem a largura da pagina
        float pageWidth = page.getMediaBox().getWidth();

        try {
            //abre o buffer pra edicao da pagina
            PDPageContentStream contentStream = new PDPageContentStream(document, page);
            //Gira a pagina em 90 graus
            contentStream.transform(new Matrix(0, 1, -1, 0, pageWidth, 0));

            PDImageXObject pdImage = PDImageXObject.createFromFile("./step2.png", document);

            contentStream.drawImage(pdImage, 30, 520);

            //Define a cor da letra
            contentStream.setNonStrokingColor(Color.BLACK);
            //Abre pra edicao escrita
            contentStream.beginText();

            //Configura a fonte de titulo e o tamanho no buffer
            font = PDType1Font.COURIER_BOLD;
            contentStream.setFont(font, 18);
            contentStream.setLeading(14.5f);

            contentStream.newLineAtOffset(250, 530);
            contentStream.showText("Resumo de Horas semanais");

            //Imprime o numero da pagina
            font = PDType1Font.COURIER;
            contentStream.setFont(font, 12);

            contentStream.newLineAtOffset(490, 0);
            contentStream.showText("Pag " + Integer.toString(pagina));

            //Define o ponto de partida em X e Y
            contentStream.newLineAtOffset(-700, -50);
            //Define a fonte do cabecalho
            font = PDType1Font.COURIER_BOLD;
            contentStream.setFont(font, 12);
            //carrega o cabecalho com nome, profissao, itera pra cada data da semana e depois o total
            String titulo = StringUtils.rightPad("Nome", 20) + StringUtils.rightPad("Profissao", 16);
            for (int i = 2; i < matrizDados[0].length; i++)
                titulo += matrizDados[0][i] + "  ";

            //Escreve o cabecalho
            contentStream.showText(titulo);
            //Troca a fonte pra normal
            font = PDType1Font.COURIER;
            contentStream.setFont(font, 12);
            //TODO criar loop duplo para criar pagina e depois imprimir o dado enquanto houver dados a serem impressos
            contentStream.newLine();

            //Para cada linha da matriz recebida, vou formatar os campos nome, profissao, cada data da semana e o total pra imprimir na linha
            //Tenho que comecar a partir de k porque pode nao ser a primeira pagina. 

            //Configuro o limite baseado se eu estou ou nao na ultima pagina
            int limite = (k + savePDFModel.REGISTROS_PAGINA < matrizDados.length - 1)
                    ? savePDFModel.REGISTROS_PAGINA
                    : matrizDados.length - k;

            for (int i = 0; i < limite; i++) {
                String nome = this.formatName(matrizDados[i + k][0]);
                String profissao = this.formatProfissao(matrizDados[i + k][1]);
                String linha = nome + profissao;
                for (int j = 2; j < matrizDados[i].length; j++)
                    linha += StringUtils.rightPad(matrizDados[i + k][j], 10);

                contentStream.showText(linha);
                contentStream.newLine();
            }
            k += limite;

            //Imprime o total em negrito quando chega no final
            System.out.println(k);
            if (k >= matrizDados.length) {
                font = PDType1Font.COURIER_BOLD;
                contentStream.setFont(font, 12);
                Double[] totais = new Double[matrizDados[0].length - 2];
                for (int i = 0; i < totais.length; i++)
                    totais[i] = 0.0;

                for (int i = 1; i < matrizDados.length; i++) {
                    for (int j = 2; j < matrizDados[i].length; j++) {
                        if (!matrizDados[i][j].equals(""))
                            totais[j - 2] += Double.parseDouble(matrizDados[i][j]);
                    }
                }
                String linhaTot = StringUtils.rightPad("Totais", 36);
                for (int i = 0; i < totais.length; i++) {
                    linhaTot += StringUtils.rightPad(totais[i].toString(), 10);
                }
                contentStream.showText(linhaTot);
                //Imprime a linha de assinatura
                this.signatureLine(contentStream);
            }
            contentStream.endText();
            contentStream.close();

        } catch (javax.imageio.IIOException ex) {
            JOptionPane.showMessageDialog(new JFrame(), "Imagem step2.png no encontrada");
            return;
        } catch (IOException ex) {
            Logger.getLogger(savePDFModel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    try {
        //Esse save vai dentro do loop?
        document.save(file);
        document.close();
    } catch (IOException ex) {
        Logger.getLogger(savePDFModel.class.getName()).log(Level.SEVERE, null, ex);
    }
}