Skip to main content
Skip table of contents

Centering text


centertext

JAVA

JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.objects;

import com.itextpdf.kernel.colors.Color;
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.canvas.draw.ILineDrawer;
import com.itextpdf.kernel.pdf.canvas.draw.SolidLine;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Tab;
import com.itextpdf.layout.element.TabStop;
import com.itextpdf.layout.properties.TabAlignment;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class CenterText {
    public static final String DEST = "./target/sandbox/objects/center_text.pdf";

    public static void main(String[] args) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new CenterText().manipulatePdf(DEST);
    }

    public void manipulatePdf(String dest) throws IOException {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document document = new Document(pdfDoc);
        Rectangle pageSize = pdfDoc.getDefaultPageSize();
        float width = pageSize.getWidth() - document.getLeftMargin() - document.getRightMargin();

        SolidLine line = new SolidLine();
        addParagraphWithTabs(document, line, width);

        // Draw a custom line to fill both sides, as it is described in iText5 example
        MyLine customLine = new MyLine();
        addParagraphWithTabs(document, customLine, width);

        document.close();
    }

    private static void addParagraphWithTabs(Document document, ILineDrawer line, float width) {
        List<TabStop> tabStops = new ArrayList<>();

        // Create a TabStop at the middle of the page
        tabStops.add(new TabStop(width / 2, TabAlignment.CENTER, line));

        // Create a TabStop at the end of the page
        tabStops.add(new TabStop(width, TabAlignment.LEFT, line));

        Paragraph p = new Paragraph().addTabStops(tabStops);
        p
                .add(new Tab())
                .add("Text in the middle")
                .add(new Tab());
        document.add(p);
    }

    private static class MyLine implements ILineDrawer {
        private float lineWidth = 1;
        private float offset = 2.02f;
        private Color color = ColorConstants.BLACK;

        @Override
        public void draw(PdfCanvas canvas, Rectangle drawArea) {
            float coordY = drawArea.getY() + lineWidth / 2 + offset;
            canvas
                    .saveState()
                    .setStrokeColor(color)
                    .setLineWidth(lineWidth)
                    .moveTo(drawArea.getX(), coordY)
                    .lineTo(drawArea.getX() + drawArea.getWidth(), coordY)
                    .stroke()
                    .restoreState();
        }

        @Override
        public float getLineWidth() {
            return lineWidth;
        }

        @Override
        public void setLineWidth(float lineWidth) {
            this.lineWidth = lineWidth;
        }

        @Override
        public Color getColor() {
            return color;
        }

        @Override
        public void setColor(Color color) {
            this.color = color;
        }

        public float getOffset() {
            return offset;
        }

        public void setOffset(float offset) {
            this.offset = offset;
        }
    }
}

C#

C#
using System;
using System.Collections.Generic;
using System.IO;
using iText.Kernel.Colors;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Kernel.Pdf.Canvas.Draw;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;

namespace iText.Samples.Sandbox.Objects
{
    public class CenterText
    {
        public static readonly String DEST = "results/sandbox/objects/center_text.pdf";

        public static void Main(string[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();

            new CenterText().ManipulatePdf(DEST);
        }

        public void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document document = new Document(pdfDoc);
            Rectangle pageSize = pdfDoc.GetDefaultPageSize();
            float width = pageSize.GetWidth() - document.GetLeftMargin() - document.GetRightMargin();

            SolidLine line = new SolidLine();
            AddParagraphWithTabs(document, line, width);

            // Draw a custom line to fill both sides, as it is described in iText5 example
            MyLine customLine = new MyLine();
            AddParagraphWithTabs(document, customLine, width);

            document.Close();
        }

        private static void AddParagraphWithTabs(Document document, ILineDrawer line, float width)
        {
            List<TabStop> tabStops = new List<TabStop>();

            // Create a TabStop at the middle of the page
            tabStops.Add(new TabStop(width / 2, TabAlignment.CENTER, line));

            // Create a TabStop at the end of the page
            tabStops.Add(new TabStop(width, TabAlignment.LEFT, line));

            Paragraph p = new Paragraph().AddTabStops(tabStops);
            p
                .Add(new Tab())
                .Add("Text in the middle")
                .Add(new Tab());
            document.Add(p);
        }

        private class MyLine : ILineDrawer
        {
            private float lineWidth = 1;
            private float offset = 2.02f;
            private Color color = ColorConstants.BLACK;

            public void Draw(PdfCanvas canvas, Rectangle drawArea)
            {
                float coordY = drawArea.GetY() + lineWidth / 2 + offset;
                canvas
                    .SaveState()
                    .SetStrokeColor(color)
                    .SetLineWidth(lineWidth)
                    .MoveTo(drawArea.GetX(), coordY)
                    .LineTo(drawArea.GetX() + drawArea.GetWidth(), coordY)
                    .Stroke()
                    .RestoreState();
            }

            public float GetLineWidth()
            {
                return lineWidth;
            }

            public void SetLineWidth(float lineWidth)
            {
                this.lineWidth = lineWidth;
            }

            public Color GetColor()
            {
                return color;
            }

            public void SetColor(Color color)
            {
                this.color = color;
            }

            public float GetOffset()
            {
                return offset;
            }

            public void SetOffset(float offset)
            {
                this.offset = offset;
            }
        }
    }
}
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.