Main Content

Create Links

You can add these kinds of links to a report:

Create Internal Links

To link from one location in a document to another location in the same document:

  1. Define the link target by appending an mlreportgen.dom.LinkTarget object to the document or document element. To make sure that the link target name that you use to create the LinkTarget object is valid for all report types, generate the link target name by using mlreportgen.utils.normalizeLinkID.

  2. Define the link by appending an mlreportgen.dom.InternalLink object to the document or document element. When you create the InternalLink object, for the targetName argument, use the link target name from the LinkTarget object.

For example, you can include an About the Author link to a heading with the title Author's Biography.

import mlreportgen.dom.*
import mlreportgen.utils.*
d = Document('mydoc','pdf');

% Append a link target to a heading
h = Heading(1,'Author''s Biography');
h.Style = {PageBreakBefore(true)};
linkID = normalizeLinkID('bio');
append(h,LinkTarget(linkID));

% Link to the target
append(d,InternalLink(linkID,'About the Author'));

% Append the heading 
append(d,h);

close(d);
rptview(d);

Create External Links

Use an mlreportgen.dom.ExternalLink object to create an external link, specifying the link target and the link text.

import mlreportgen.dom.*
d = Document('mydoc');

append(d,ExternalLink('https://www.mathworks.com/','MathWorks'));

close(d);
rptview('mydoc','html');

Add Text or Images to Links

To add text or an image to an ExternalLink or InternalLink object, use the append method with that object. Append a Text, Image, or CustomElement object.

Create Page References

You can create a numeric reference to the page where a link target resides. For example, you can create a page reference in the form “See page 15,” where the target you are referencing is on an object on page 15. For example:

import mlreportgen.dom.*;
d = Document('mydoc','pdf');
open(d);

% Add target to heading object and append heading and 
% para text to document
h = Heading1(LinkTarget('mytarget'));
append(h,'Referenced Head');
p = Paragraph('Here is some paragraph text.');
append(d,h);
append(d,p);

% Add another page and insert the page reference 
% to the target
p1 = Paragraph('The following paragraph contains the page reference.');
p1.Style = {PageBreakBefore(true)};
p2 = Paragraph('See Page ');
p2.WhiteSpace = 'preserve';
ref = PageRef('mytarget');
append(p2,ref);
append(p2,'.');
append(d,p1);
append(d,p2);

close(d);
rptview(d.OutputPath);

In your PDF template, you can use a <pageref> element to create this kind of reference. Your DOM API program must set the link target that the element uses. The <pageref> uses one argument: <pageref target="nameoftarget">.

For more information on this mechanism, see mlreportgen.dom.PageRef.

See Also

| | | |

Related Examples

More About