无法使用PDFBOX获取书签在页面中的特定位置

问题描述 投票:1回答:1

我正在使用PDFBOX 2.0.2 jar在现有标题加书签的PDF文件中添加多个PDF。同样,我将其拆分并合并其他PDF。

Splitter splitter = new Splitter();
                    splitter.setStartPage(1);
                    splitter.setEndPage(noOfPagesInHeadingBkmrkedPDF);

在拆分和合并之前,我将所有书签保留在HashMap中,并将键作为pageNumber并将值作为书签名称。合并后,我将书签放回原位我的查询是-如何获取页面上书签的特定坐标(位置),以便合并后我应该能够将其设置回页面的特定位置。

在拆分之前创建HashMap的代码段:

public void getAllBookmarks(PDOutlineNode bookmarksInOriginalFile, String emptyString, Map<Integer, String> bookmarkMap) throws IOException {
PDOutlineItem current = null;
if (null != bookmarksInOriginalFile)
    current = bookmarksInOriginalFile.getFirstChild();
while (current != null) {
    Integer pageNumber = 0;
    PDPageDestination pd = null;
    if (current.getDestination() instanceof PDPageDestination) {
        pd = (PDPageDestination) current.getDestination();
        pageNumber = (pd.retrievePageNumber() + 1);  // Do we have any method available to get the location on the specific page ??      
    }
    if (current.getAction() instanceof PDActionGoTo) {
        PDActionGoTo gta = (PDActionGoTo) current.getAction();
        if (gta.getDestination() instanceof PDPageDestination) {
            pd = (PDPageDestination) gta.getDestination();               
            pageNumber = (pd.retrievePageNumber() + 1);
        }
    }
    String bookmarkName = emptyString + current.getTitle();
    if(null!=bookmarkName && !EMPTY_STRING.equalsIgnoreCase(bookmarkName)){               
          bookmarkMap.put(pageNumber-1,bookmarkName);
    }

    getAllBookmarks(current, emptyString,bookmarkMap);
    current = current.getNextSibling();
    }
}

任何帮助将不胜感激。

谢谢...

java pdfbox
1个回答
1
投票

由于我能够使用@TilmanHausherr建议解决我的解决方案。我在回答我的问题。我更改了以下代码:

public void getAllBookmarks(PDOutlineNode bookmarksInOriginalFile, String emptyString, Map<Integer,BookmarkMetaDataBO> bookmarkMap) throws IOException {
            PDOutlineItem current = null;
            if (null != bookmarksInOriginalFile)
                current = bookmarksInOriginalFile.getFirstChild();
            while (current != null) {
                Integer pageNumber = 0;
                PDPageDestination pd = null;
                PDPageXYZDestination pdx =  null;
              // These value will give the specific location
                 **int left = 0;
                 int top = 0;**
                if (current.getDestination() instanceof PDPageXYZDestination) {
                    pdx = (PDPageXYZDestination) current.getDestination();
                    pageNumber = (pdx.retrievePageNumber() + 1); 
                    **left = pdx.getLeft();
                    top = pdx.getTop();**
                }
                if (current.getAction() instanceof PDActionGoTo) {
                    PDActionGoTo gta = (PDActionGoTo) current.getAction();
                    if (gta.getDestination() instanceof PDPageDestination) {
                        pd = (PDPageDestination) gta.getDestination();               
                        pageNumber = (pd.retrievePageNumber() + 1);
                    }
                }
                String bookmarkName = emptyString + current.getTitle();
                if(null!=bookmarkName && !EMPTY_STRING.equalsIgnoreCase(bookmarkName)){
                      BookmarkMetaDataBO bkmrkBo =  new BookmarkMetaDataBO();
                      **bkmrkBo.setTop(left);
                      bkmrkBo.setLeft(top);**
                      bookmarkMap.put(pageNumber-1,bkmrkBo);
                }

                getAllBookmarks(current, emptyString,bookmarkMap);
                current = current.getNextSibling();
                }
            }

谢谢...

© www.soinside.com 2019 - 2024. All rights reserved.