Text Alignment in accessible/tagged PDFs

Hi, I’m trying to create a document as an accessible PDF (PDF-UA) . I have a problem with text alignment. How do you get a piece of text center-aligned ?

Thanks!

@dushanrs

You can use TextFragment.HorizontalAlignment property as shown below to achieve your requirement. Hope this helps you.

Aspose.Pdf.Document document = new Aspose.Pdf.Document();
Aspose.Pdf.Page page = document.Pages.Add();

Aspose.Pdf.Text.TextFragment text = new Aspose.Pdf.Text.TextFragment("some text");
page.Paragraphs.Add(text);
text.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;

Thanks for the reply! What you suggest works with general PDFs. But we cannot use TextFragment in accessible or tagged PDF creation.

@dushanrs

Could you please ZIP and attach your sample input and expected output PDF files? We will then provide you more information on it.

@tahir.manzoor

I cannot copy my work here due to work related constraints. But this example copied from Aspose documentation represent the basic need. In this example, a ParagraphElement is used to add text to the PDF. But I cannot see how it can be aligned the way I want ( right/center/left).

// For complete examples and data files, please go to GitHub - aspose-pdf/Aspose.PDF-for-Java: Aspose.PDF for Java examples, plugins and showcases
// The path to the documents directory.
String path = “pathTodir”;

// Create Pdf Document
Document document = new Document();

// Get Content for work with TaggedPdf
ITaggedContent taggedContent = document.getTaggedContent();

// Set Title and Language for Documnet
taggedContent.setTitle(“Tagged Pdf Document”);
taggedContent.setLanguage(“en-US”);

// Get Root Structure Elements
StructureElement rootElement = taggedContent.getRootElement();

ParagraphElement p = taggedContent.createParagraphElement();
// Set Text to Text Structure Element
p.setText(“Paragraph.”);
rootElement.appendChild(p);

// Save Tagged Pdf Document
document.save(path + “TextStructureElement.pdf”);

Source of the example code:Setting Structure Elements Properties in Tagged PDF|Aspose.PDF for Java

@dushanrs

We have logged a ticket for your requirement as PDFJAVA-42378 in our issue tracking system. We will inform you once there is an update available on it. We apologize for your inconvenience.