书签使用iTextSharp的4.1.6特定页面 [英] Bookmark to specific page using iTextSharp 4.1.6

查看:379
本文介绍了书签使用iTextSharp的4.1.6特定页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要一个文档中的书签指向添加到特定页面。从其他PDF文件添加书签,我看到类似的代码合并以下工作正常,但是当我复制它自定义书签添加到非书签项目失败:

I want to add a bookmark pointing to a specific page within a document. Adding bookmarks from other PDF files I'm merging with code similar to that below works fine, but when I copied it to add custom bookmarks to non-bookmarked items it fails:

var bookmarks = new ArrayList();
var writer = new PdfCopy(document, memorystream);
// ...
var uni = new Hashtable();
uni.Add("Action", "GoTo");
uni.Add("Title", "Awesome Unicorn pic");
uni.Add("Page", "8 XYZ 0 0 0");
bookmarks.Add(uni);
// ...
writer.Outlines = bookmarks;



但显然(页,8 XYZ 0 0 0) 不引用的的8而的8或类似的东西。是否有其他动作我可以用它来指向任意的页面?或一些其他的方法?

But apparently ("Page", "8 XYZ 0 0 0") does not reference Page 8 but rather Section 8 or something like that. Is there an alternative Action I could use to point to an arbitrary page? Or some other method?

推荐答案

看起来像我的软弱人类的大脑混乱的PDF坐标系统。原来,(页,8 XYZ 0 0 0); 实际上做参考第8页,但XYZ 0 0 0不引用左上点在网页上,而是左下点。所以,当点击时,这样的书签意外地把你带到第二页。真棒

Looks like the PDF coordinate system messed with my feeble human brain. Turns out that ("Page", "8 XYZ 0 0 0"); actually does reference page 8, but "XYZ 0 0 0" does not reference the top left point on a page, but rather the bottom left point. So when clicked, a bookmark like this unexpectedly takes you to page two. Awesome.

下面的代码按预期方式工作,因为它得到的第一页的高度,并使用该链接的页面的顶部。该代码是来自各地的我的源代码不同的地方聚集,所以它不是很合但尽管如此,它的工作原理。

The code below works as expected, because it gets the height of the first page and uses that to link to the top of the page. The code is gathered from different places around my source, so it's not very "together" but still, it works.

var bookmarks = new ArrayList();
var rdr = new PdfReader(first);
var doc = new Document(rdr.GetPageSizeWithRotation(1));
var wri = new PdfCopy(doc, memorystream);
var temp = wri.GetImportedPage(rdr, 1); // get 1st page
var h = temp.Height; // get height of 1st page

// Add first item to bookmarks.
var test = new Hashtable();
test.Add("Action", "GoTo");
test.Add("Title", "Page1 0 H 0");
test.Add("Page", "1 XYZ 0 "+h+" 0"); // use height of 1st page
bookmarks.Add(test);

// Do your worst and afterwards set the bookmarks to Outline. So yeah.
wri.Outlines = bookmarks;

这篇关于书签使用iTextSharp的4.1.6特定页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆