Skip to main content
Skip table of contents

Page Labels


pagelabels

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.io.font.constants.StandardFonts;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PageLabelNumberingStyle;
import com.itextpdf.kernel.pdf.PdfName;
import com.itextpdf.kernel.pdf.PdfNumber;
import com.itextpdf.kernel.pdf.PdfString;
import com.itextpdf.kernel.pdf.PdfViewerPreferences;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.action.PdfAction;
import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
import com.itextpdf.kernel.pdf.annot.PdfTextAnnotation;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.AreaBreak;
import com.itextpdf.layout.element.Link;
import com.itextpdf.layout.element.Paragraph;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

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

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

    protected void manipulatePdf(String dest) throws IOException {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);

        PdfViewerPreferences viewerPreferences = new PdfViewerPreferences();
        viewerPreferences.setPrintScaling(PdfViewerPreferences.PdfViewerPreferencesConstants.NONE);
        pdfDoc.getCatalog().setPageMode(PdfName.UseThumbs);
        pdfDoc.getCatalog().setPageLayout(PdfName.TwoPageLeft);
        pdfDoc.getCatalog().setViewerPreferences(viewerPreferences);

        doc.add(new Paragraph("Hello World"));
        doc.add(new Paragraph("Hello People"));

        doc.add(new AreaBreak());
        PdfFont font = PdfFontFactory.createFont(StandardFonts.HELVETICA);

        // Add the text to the direct content, but not in the right order
        PdfCanvas canvas = new PdfCanvas(pdfDoc.getPage(2));
        canvas.beginText();
        canvas.setFontAndSize(font, 12);
        canvas.moveText(88.66f, 788);
        canvas.showText("ld");
        canvas.moveText(-22f, 0);
        canvas.showText("Wor");
        canvas.moveText(-15.33f, 0);
        canvas.showText("llo");
        canvas.moveText(-15.33f, 0);
        canvas.showText("He");
        canvas.endText();
        PdfFormXObject formXObject = new PdfFormXObject(new Rectangle(250, 25));
        new PdfCanvas(formXObject, pdfDoc).beginText()
                .setFontAndSize(font, 12)
                .moveText(0, 7)
                .showText("Hello People")
                .endText();
        canvas.addXObjectAt(formXObject, 36, 763);

        pdfDoc.setDefaultPageSize(new PageSize(PageSize.A4).rotate());
        doc.add(new AreaBreak());
        doc.add(new Paragraph("Hello World"));

        pdfDoc.setDefaultPageSize(new PageSize(842, 595));
        doc.add(new AreaBreak());
        doc.add(new Paragraph("Hello World"));

        pdfDoc.setDefaultPageSize(PageSize.A4);
        doc.add(new AreaBreak());
        pdfDoc.getLastPage().setCropBox(new Rectangle(10, 70, 525, 755));
        doc.add(new Paragraph("Hello World"));

        doc.add(new AreaBreak());
        pdfDoc.getLastPage().getPdfObject().put(PdfName.UserUnit, new PdfNumber(5));
        doc.add(new Paragraph("Hello World"));

        doc.add(new AreaBreak());
        pdfDoc.getLastPage().setArtBox(new Rectangle(36, 36, 523, 770));
        Paragraph p = new Paragraph("Hello ")
                .add(new Link("World", PdfAction.createURI("http://maps.google.com")));
        doc.add(p);
        PdfAnnotation a = new PdfTextAnnotation(
                new Rectangle(36, 755, 30, 30))
                .setTitle(new PdfString("Example"))
                .setContents("This is a post-it annotation");
        pdfDoc.getLastPage().addAnnotation(a);


        pdfDoc.getPage(1).setPageLabel(PageLabelNumberingStyle.UPPERCASE_LETTERS, null);
        pdfDoc.getPage(3).setPageLabel(PageLabelNumberingStyle.DECIMAL_ARABIC_NUMERALS, null);
        pdfDoc.getPage(4).setPageLabel(PageLabelNumberingStyle.DECIMAL_ARABIC_NUMERALS, "Custom-", 2);

        doc.close();
    }
}

C#

C#
using System.IO;
using iText.IO.Font.Constants;
using iText.Kernel.Font;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Action;
using iText.Kernel.Pdf.Annot;
using iText.Kernel.Pdf.Canvas;
using iText.Kernel.Pdf.Xobject;
using iText.Layout;
using iText.Layout.Element;

namespace iText.Samples.Sandbox.Objects
{
    public class PageLabels
    {
        public static readonly string DEST = "results/sandbox/objects/page_labels.pdf";

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

        protected void ManipulatePdf(string dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document doc = new Document(pdfDoc);

            PdfViewerPreferences viewerPreferences = new PdfViewerPreferences();
            viewerPreferences.SetPrintScaling(PdfViewerPreferences.PdfViewerPreferencesConstants.NONE);
            pdfDoc.GetCatalog().SetPageMode(PdfName.UseThumbs);
            pdfDoc.GetCatalog().SetPageLayout(PdfName.TwoPageLeft);
            pdfDoc.GetCatalog().SetViewerPreferences(viewerPreferences);

            doc.Add(new Paragraph("Hello World"));
            doc.Add(new Paragraph("Hello People"));

            doc.Add(new AreaBreak());
            PdfFont font = PdfFontFactory.CreateFont(StandardFonts.HELVETICA);
            
            // Add the text to the direct content, but not in the right order
            PdfCanvas canvas = new PdfCanvas(pdfDoc.GetPage(2));
            canvas.BeginText();
            canvas.SetFontAndSize(font, 12);
            canvas.MoveText(88.66f, 788);
            canvas.ShowText("ld");
            canvas.MoveText(-22f, 0);
            canvas.ShowText("Wor");
            canvas.MoveText(-15.33f, 0);
            canvas.ShowText("llo");
            canvas.MoveText(-15.33f, 0);
            canvas.ShowText("He");
            canvas.EndText();
            PdfFormXObject formXObject = new PdfFormXObject(new Rectangle(250, 25));
            new PdfCanvas(formXObject, pdfDoc)
                .BeginText()
                .SetFontAndSize(font, 12)
                .MoveText(0, 7)
                .ShowText("Hello People")
                .EndText();
            canvas.AddXObjectAt(formXObject, 36, 763);

            pdfDoc.SetDefaultPageSize(new PageSize(PageSize.A4).Rotate());
            doc.Add(new AreaBreak());
            doc.Add(new Paragraph("Hello World"));

            pdfDoc.SetDefaultPageSize(new PageSize(842, 595));
            doc.Add(new AreaBreak());
            doc.Add(new Paragraph("Hello World"));

            pdfDoc.SetDefaultPageSize(PageSize.A4);
            doc.Add(new AreaBreak());
            pdfDoc.GetLastPage().SetCropBox(new Rectangle(10, 70, 525, 755));
            doc.Add(new Paragraph("Hello World"));

            doc.Add(new AreaBreak());
            pdfDoc.GetLastPage().GetPdfObject().Put(PdfName.UserUnit, new PdfNumber(5));
            doc.Add(new Paragraph("Hello World"));

            doc.Add(new AreaBreak());
            pdfDoc.GetLastPage().SetArtBox(new Rectangle(36, 36, 523, 770));
            Paragraph p = new Paragraph("Hello ")
                .Add(new Link("World", PdfAction.CreateURI("http://maps.google.com")));
            doc.Add(p);
            PdfAnnotation a = new PdfTextAnnotation(
                    new Rectangle(36, 755, 30, 30))
                .SetTitle(new PdfString("Example"))
                .SetContents("This is a post-it annotation");
            pdfDoc.GetLastPage().AddAnnotation(a);


            pdfDoc.GetPage(1).SetPageLabel(PageLabelNumberingStyle.UPPERCASE_LETTERS, null);
            pdfDoc.GetPage(3).SetPageLabel(PageLabelNumberingStyle.DECIMAL_ARABIC_NUMERALS, null);
            pdfDoc.GetPage(4).SetPageLabel(PageLabelNumberingStyle.DECIMAL_ARABIC_NUMERALS, "Custom-", 2);

            doc.Close();
        }
    }
}
JavaScript errors detected

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

If this problem persists, please contact our support.