TocInfo-Title cannot be aligned to the left

Hi,
our corporate design forces me to put the toc title to the left.
No matter how I format the Textfragment, it is alway in the center.
I found a code snippet from you and I added the corresponding
“HorizontalAlignment.Left” to it.
Is there any other way to get it to the left?

List tflist = new java.util.ArrayList();
Document doc = new Document();
TextFragment tf = new TextFragment(“Heading 1”);
TextFragment tf2 = new TextFragment(“Heading 2”);
TextFragment tf3 = new TextFragment(“Heading 3”);
Page page = doc.getPages().add();
page.getParagraphs().add(tf);
page.getParagraphs().add(tf2);
page.getParagraphs().add(tf3);
tflist.add(tf);
tflist.add(tf2);
tflist.add(tf3);
doc.processParagraphs();

Page tocPage = doc.getPages().insert(1);
// Create object to represent TOC information
TocInfo tocInfo = new TocInfo();
TextFragment title = new TextFragment(“Table Of Contents”);
title.getTextState().setFontSize(20);
title.getTextState().setFontStyle(FontStyles.Bold);
title.getTextState().setHorizontalAlignment(HorizontalAlignment.Left);

// Set the title for TOC
tocInfo.setTitle(title);
tocPage.setTocInfo(tocInfo);

String[] titles = new String[4];
titles[0] = “Heading 1”;
titles[1] = “Heading 2”;
titles[2] = “Heading 3”;
for (int i = 0; i < 3; i++) {
// Create Heading object
Heading heading2 = new Heading(1);

TextSegment segment2 = new TextSegment();
heading2.setTocPage(tocPage);
heading2.getSegments().add(segment2);
heading2.setDestinationPage(page);
heading2.setHyperlink(new LocalHyperlink(tflist.get(i)));

// Destination coordinate
segment2.setText(titles[i]);

// Add heading to page containing TOC
tocPage.getParagraphs().add(heading2);

}
// Save the updated document
doc.save(“TOC_Output_Java.pdf”);

@MrNW

  • Are you setting the HorizontalAlignment on the TextFragment used in TocInfo.setTitle() the only place where you’re trying to control alignment?
  • Does the title appear in the correct position (left-aligned) if you add the same TextFragment directly to a regular page paragraph instead of using TocInfo?
  • Have you tried setting the alignment on the TocInfo level or using a Paragraph with HorizontalAlignment instead of a TextFragment for the title?
  • Is the issue occurring with all PDF viewers, or only specific ones?

see my answers below in bold:

@MrNW

The user is reporting a specific issue with text alignment in a TOC title using Aspose.PDF for Java, has provided code, and confirmed the behavior through diagnostic questions. This is a code-level problem requiring troubleshooting.