Hyperlinks to other pages in asPose PDF Java (not legacy)

I need to create text "Chapter1" and on click go to page 2 in AsPose PDF for Java

How can I do that in com.aspose.pdf (not legacy)?

Is it supported? Examples?

Hi Alex,


Thanks for contacting support.

Please try using the following code snippet to accomplish your requirement.

[Java]

//open
document
<o:p></o:p>

Document document = new Document("c:/pdftest/facture.pdf");

//create link

Page page = document.getPages().get_Item(1);

LinkAnnotation link = new LinkAnnotation(page, new com.aspose.pdf.Rectangle(100, 100, 150, 150));

// link for second page of PDF

link.setDestination(new com.aspose.pdf.XYZExplicitDestination(2, 100, 100, 1));

page.getAnnotations().add(link);

//save updated document

document.save("c:/pdftest/PDFWithHyperlink+output.pdf");

Is it possible to do it without rectangles and XYZ coordinates. I only need a simple text(!!!) element on the page to be clickable. Legacy classes provide very good examples. I do not need to create any annotations or rectangles, just table on contents. When a person clicks on table of contents element “Chapter 2 …10” I would like to go to page 10.

Hi Alex,


Well in order to place the hyperlink inside PDF file, you need to specify the location (region) where link should be placed. Furthermore, in order to place the link inside PDF, you need to place LinkAnnotation inside PDF file. You may consider using following code snippet.

[Java]

com.aspose.pdf.facades.PdfContentEditor
editor = new com.aspose.pdf.facades.PdfContentEditor();<o:p></o:p>

// open source PDF file

editor.bindPdf("c:/pdftest/Doc+3.pdf");

java.awt.Rectangle rect=new java.awt.Rectangle(50,50, 200, 200);

java.awt.Color clr=new java.awt.Color(255,255,255);

// create link towards second page of PDF file

editor.createLocalLink(rect, 2, 1, clr);

editor.save("c:/pdftest/LocalinkTest.pdf");

Hi Alex,


Adding more to my previous comments, in case you need to create TOC in existing PDF file, please try using the code snippet shared below. When using this approach, the LinkAnnotations are not placed and this approach is much closer to aspose.pdf package approach.

However when using below code snippet, I have observed that formatting of TOC is not correct in resultant PDF file. For
the sake of correction, I have logged it in our issue tracking system as PDFNEWJAVA-34197. We will
investigate this issue in details and will keep you updated on the status of a
correction. We are sorry for this inconvenience.

<o:p></o:p>


[Java]

//
Load an existing PDF files
<o:p></o:p>

com.aspose.pdf.Document doc = new com.aspose.pdf.Document("c:/pdftest/emd-2012-014876.pdf");

// Get access to first page of PDF file

com.aspose.pdf.Page tocPage = doc.getPages().insert(1);

// Create object to represent TOC information

com.aspose.pdf.TocInfo tocInfo = new com.aspose.pdf.TocInfo();

com.aspose.pdf.TextFragment title = new com.aspose.pdf.TextFragment("Table Of Contents");

title.getTextState().setFontSize(20);

title.getTextState().setFontSize(com.aspose.pdf.FontStyles.Bold);

// Set the title for TOC

tocInfo.setTitle(title);

tocPage.setTocInfo(tocInfo);

// Create string objects which will be used as TOC elements

String[] titles = new String[4];

titles[0] = "First page";

titles[1] = "Second page";

titles[2] = "Third page";

titles[3] = "Fourth page";

for (int i = 0; i < 4; i++)

{

// Create Heading object

com.aspose.pdf.Heading heading2 = new com.aspose.pdf.Heading(1);

com.aspose.pdf.TextSegment segment2 = new com.aspose.pdf.TextSegment();

heading2.setTocPage(tocPage);

heading2.getSegments().add(segment2);

// Specify the destination page for heading object

heading2.setDestinationPage(doc.getPages().get_Item(i + 2));

// Destination page

heading2.setTop(doc.getPages().get_Item(i + 2).getRect().getHeight());

// Destination coordinate

segment2.setText(titles[i]);

// Add heading to page containing TOC

tocPage.getParagraphs().add(heading2);

}

// Save the updated document

doc.save("c:/pdftest/TOC_Output_Java.pdf");

The issues you have found earlier (filed as PDFNEWJAVA-34197) have been fixed in Aspose.Pdf for Java 10.0.0.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.
(2)