Skip to content

PageReference is only rendered in LibreOffice #2904

Open
@rfsalman

Description

@rfsalman

Issue

  • PageReference is only rendered in LibreOffice

Environment

  • Docxjs version: 8.5.0

Description

I am trying to create a Table Of Contents manually because TableOfContent was not displaying anything, by keeping a reference to headers and render then with InternalHyperlink.

Below is the simplified version of the code:

const headers = [
  { id: 'mainheaders1', text: '1. General' },
]

const borders = {};

const rows = [];

const linkText = new TextRun({
  text: headers[0].text,
  style: 'headerLinkText',
});

const link = new InternalHyperlink({
  anchor: id,
  children: [linkText],
});

const pageNumber = new Paragraph({
  alignment: 'end',
  style: 'headerLinkText',
  children: [new TextRun(' '), new PageReference(id, { hyperlink: true })], // ! PageReference is not working on most docx editors, only in LibreOffice
});

 const row = new TableRow({
  children: [
    new TableCell({
      margins: {
        top: 120,
        bottom: 120,
      },
      borders,
      width: {
        size: 70,
        type: WidthType.PERCENTAGE,
      },
      children: [
        new Paragraph({
          children: [link],
        }),
      ],
    }),
    new TableCell({
      margins: {
        top: 120,
        left: 240,
      },
      borders,
      width: {
        size: 30,
        type: WidthType.PERCENTAGE,
      },
      children: [pageNumber],
    }),
  ],
});

rows.push(row);

const contentsTable = new Table({
  margins: {
    left: 0,
    top: 0,
    right: 0,
    bottom: 0,
  },
  borders,
  width: {
    size: 100,
    type: WidthType.PERCENTAGE,
  },
  rows,
});

const section: ISectionOptions = {
  properties: {
    type: SectionType.NEXT_PAGE,
  },
  children: [pageHeader, contentsTable],
};

Opened in Microsoft Word, this is how the document was rendered:
toc

And this is how it was rendered in LibreOffice:
toc-libre

The hyperlinks work in all editors, but i'd like the PageReferences to work on all editors too, are there any special handling required for specific editors ?

Big thanks to the maintainers of this project.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions