Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

setCharacterSpacing affecting centre alignment

Abierto
#1,280 1 comentario 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
1-2 días
Aptitud para principiantes
35/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Estancado
Stack tecnológico
java
Área
backend

Línea de trabajo

Start by tracing how ColumnText lays out centered paragraphs when PdfContentByte.setCharacterSpacing has been applied, then check existing alignment tests for coverage. Reproduce the supplied example with OpenPDF 2.0.3 and compare it with the same paragraph without character spacing; done means the centered text remains correctly aligned when spacing is set. The issue names no source file or test.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

Not sure if a bug or I'm doing something wrong but when I try and set the character spacing for some text added to a ColumnText, it's affecting the centre alignment I'm trying to achieve.

If I don't set setCharacterSpacing, the paragraph is correctly aligned in the centre.

How can I centre text on a page using ColumnText and setCharacterSpacing?

public class SimplePdf {
    public static void main(String[] args) throws IOException {
        String text = """
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Morbi ac nisl cursus, feugiat mauris nec, tempor ipsum.
Vivamus sodales bibendum urna in iaculis.
Vivamus porta at nisi a interdum.
Quisque vehicula ipsum sed leo tempus semper.
In ultrices mattis odio, non.""";

        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("simple-output.pdf"));

        Rectangle pageSize = document.getPageSize();

        document.open();

        PdfContentByte cb = writer.getDirectContent();

        cb.beginText();
        cb.setCharacterSpacing(3.04f);
        cb.endText();

        Paragraph paragraph = new Paragraph(text, new Font(BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.EMBEDDED), 10f));
        paragraph.setAlignment(Element.ALIGN_CENTER);

        ColumnText ct = new ColumnText(cb);
        ct.setSimpleColumn(paragraph, 0, pageSize.getHeight() - 300f, pageSize.getWidth(), pageSize.getHeight() - 170f, 16, Element.ALIGN_CENTER);
        ct.go();

        // Close everything
        document.close();
        writer.close();
    }
}

Version: openpdf 2.0.3

Lenguaje dominante
Java
Estrellas
4.4k
Forks
714
Métricas de merge de PR
Sin PR fusionados en 30 d

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de LibrePDF/OpenPDF

Todos los issues de LibrePDF/OpenPDF

Issues similares

Más issues de Java

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.