How add Multiple links in same PDF using iText

与世无争的帅哥 提交于 2019-12-25 01:27:58

问题


I'm using the Link and Destination tags of iText 7.0.5 to create the links while generating the pdf doc. When there is only one Link and Destination, its working as expected. But when I'm adding multiple links and destination, its not working as expected. For example there are three links and 3 destinations and each link mapped to different destination. If I click on third link in PDF it will be redirected to third destination, but if I click on first and second links, those are also redirecting to third destination instead of first or second destination. Below is the sample code how I'm creating the link and destinations using iText. Please let me know what could be the problem here. Is there another way to achieve this. Thanks.

// Pdf doc will hold these tables. Destination

    Table destinationTable1 = new Table(1);
    Cell cell = new Cell().add(addHeaderCellWithBorder("First Destination"));
    cell.setProperty(Property.DESTINATION, "dest1");
    destinationTable1.addCell(cell);

    Table destinationTable2 = new Table(1);
    Cell cell = new Cell().add(addHeaderCellWithBorder("Second Destination"));
    cell.setProperty(Property.DESTINATION, "dest2");
    destinationTable2.addCell(cell);

// Link Creation in another table

    Table linkTable1 = new Table(100);
    Cell linkcell1 = new Cell();
    Paragraph p1 = new Paragraph();
    PdfAction firstLink = PdfAction.createGoTo("dest1");
    PdfLinkAnnotation firstLinkAnnot = new PdfLinkAnnotation(new Rectangle(0, 0, 0, 0))
            .setHighlightMode(PdfAnnotation.HIGHLIGHT_INVERT).setAction(firstLink)
            .setBorderStyle(PdfAnnotation.STYLE_UNDERLINE);
    Link link1 = new Link("First Link", firstLinkAnnot);
    p1.add(link1.setUnderline()).setFontColor(Color.BLUE).setFontSize(7).setPaddingTop(1).setMultipliedLeading(1);
    linkTable1.addCell(linkcell1.add(p1).setBorder(Border.NO_BORDER));

    Table linkTable2 = new Table(100);
    Cell linkcell2 = new Cell();
    Paragraph p2 = new Paragraph();
    PdfAction secondLink = PdfAction.createGoTo("dest2");
    PdfLinkAnnotation secondLinkAnnot = new PdfLinkAnnotation(new Rectangle(0, 0, 0, 0))
            .setHighlightMode(PdfAnnotation.HIGHLIGHT_INVERT).setAction(secondLink)
            .setBorderStyle(PdfAnnotation.STYLE_UNDERLINE);
    Link link2 = new Link("Second Link", secondLinkAnnot);
    p2.add(link2.setUnderline()).setFontColor(Color.BLUE).setFontSize(7).setPaddingTop(1).setMultipliedLeading(1);
    linkTable2.addCell(linkcell2.add(p2).setBorder(Border.NO_BORDER));

来源:https://stackoverflow.com/questions/55043420/how-add-multiple-links-in-same-pdf-using-itext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!