iText pdf library
Website search

Centering text

CenterText.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/*
 * Example written in answer to a question on StackOverflow.
 * http://stackoverflow.com/questions/39437838
 */
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.property.TabAlignment;
 
import java.io.File;
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;
        }
    }
}
CenterText.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
 
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;
            }
        }
    }
}


Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now