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();
}
}