Criar um PDF complexo

Contents
[ ]

O Olá Mundo exemplo cobre o caminho de criação de PDF mais simples. Este exemplo se baseia nesse fluxo de trabalho e cria um documento mais rico que combina gráficos, texto e conteúdo tabular.

Para criar um documento PDF mais complexo em Java:

  1. Crie um Document e adicione um Page.
  2. Adicione uma imagem ao Page com page.addImage(...) e um alvo Rectangle.
  3. Crie um cabeçalho TextFragment e defina sua fonte, tamanho, alinhamento, e Position.
  4. Crie um segundo TextFragment para o parágrafo de descrição.
  5. Construa um Table com bordas, preenchimento e estilo de cabeçalho.
  6. Adicione linhas de agenda geradas ao Table.
  7. Anexe o Table ao Page parágrafos.
  8. Salve o PDF de saída Document.

O código Java a seguir é baseado em GetStartedExamples.java.

public static void complexExample(Path imageFile, Path outputFile) {
    try (Document document = new Document()) {
        Page page = document.getPages().add();

        page.addImage(imageFile.toString(), new Rectangle(20, 730, 120, 830, true));

        TextFragment header = new TextFragment("New ferry routes in Fall 2029");
        header.getTextState().setFont(FontRepository.findFont("Arial"));
        header.getTextState().setFontSize(24);
        header.setHorizontalAlignment(HorizontalAlignment.Center);
        header.setPosition(new Position(130, 720));
        page.getParagraphs().add(header);

        String descriptionText = "Visitors must buy tickets online and tickets are limited to 5,000 per day. "
                + "Ferry service is operating at half capacity and on a reduced schedule. "
                + "Expect lineups.";
        TextFragment description = new TextFragment(descriptionText);
        description.getTextState().setFont(FontRepository.findFont("Times New Roman"));
        description.getTextState().setFontSize(14);
        description.setHorizontalAlignment(HorizontalAlignment.Left);
        page.getParagraphs().add(description);

        page.getParagraphs().add(createScheduleTable());

        document.save(outputFile.toString());
    }
}

O mesmo exemplo usa um método auxiliar para preparar a tabela de horários com formatação de cabeçalho e horários de partida gerados:

private static Table createScheduleTable() {
    Table table = new Table();
    table.setColumnWidths("200 200");
    table.setBorder(new BorderInfo(BorderSide.Box, 1.0f, Color.getDarkSlateGray()));
    table.setDefaultCellBorder(new BorderInfo(BorderSide.Box, 0.5f, Color.getBlack()));
    table.setDefaultCellPadding(new MarginInfo(4.5, 4.5, 4.5, 4.5));
    table.getMargin().setBottom(10);
    table.getDefaultCellTextState().setFont(FontRepository.findFont("Helvetica"));

    Row headerRow = table.getRows().add();
    Cell departsCityCell = headerRow.getCells().add("Departs City");
    Cell departsIslandCell = headerRow.getCells().add("Departs Island");
    styleHeaderCell(departsCityCell);
    styleHeaderCell(departsIslandCell);

    Duration time = Duration.ofHours(6);
    Duration increment = Duration.ofMinutes(30);
    for (int index = 0; index < 10; index++) {
        Row dataRow = table.getRows().add();
        dataRow.getCells().add(formatTime(time));
        time = time.plus(increment);
        dataRow.getCells().add(formatTime(time));
    }

    return table;
}