We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

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 https://github.com/aspose-pdf/Aspose.PDF-for-Java
// 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§;

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

Source of the example code:https://docs.aspose.com/pdf/java/set-tagged-pdfs-element-properties/

@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.