Tagged PDF : Introduction « PDF « Java Tutorial






import java.io.FileOutputStream;

import com.lowagie.text.Document;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfName;
import com.lowagie.text.pdf.PdfStructureElement;
import com.lowagie.text.pdf.PdfStructureTreeRoot;
import com.lowagie.text.pdf.PdfWriter;

public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    writer.setTagged();
    document.open();
    PdfStructureTreeRoot root = writer.getStructureTreeRoot();
    PdfStructureElement eTop = new PdfStructureElement(root, new PdfName("Everything"));
    root.mapRole(new PdfName("Everything"), new PdfName("Sect"));
    PdfStructureElement e1 = new PdfStructureElement(eTop, PdfName.P);
    PdfStructureElement e2 = new PdfStructureElement(eTop, PdfName.P);
    PdfStructureElement e3 = new PdfStructureElement(eTop, PdfName.P);
    PdfContentByte cb = writer.getDirectContent();
    BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, false);
    cb.setLeading(16);
    cb.setFontAndSize(bf, 12);

    cb.beginText();
    cb.setTextMatrix(50, 700);
    for (int k = 0; k < 2; ++k) {
      cb.beginMarkedContentSequence(e2);
      cb.newlineShowText("text2");
      cb.endMarkedContentSequence();
    }
    cb.endText();
    document.newPage();
    cb.setLeading(16);
    cb.setFontAndSize(bf, 12);
    cb.beginText();
    cb.setTextMatrix(50, 804);
    cb.beginMarkedContentSequence(e2);
    cb.newlineShowText("text2");
    cb.endMarkedContentSequence();

    cb.endText();

    cb.beginMarkedContentSequence(e3);
    cb.endMarkedContentSequence();
    document.close();
  }

}








29.1.Introduction
29.1.1.Create your first Pdf document with Java
29.1.2.Create Empty page
29.1.3.Update a Pdf document
29.1.4.Read Pdf document to string
29.1.5.PRTokeniser
29.1.6.PdfDictionary
29.1.7.inspects a PDF file
29.1.8.Tagged PDF