使用C#代码使用iTextSharp将页面书签添加到现有PDF [英] Add Page bookmarks to an existing PDF using iTextSharp using C# code

查看:960
本文介绍了使用C#代码使用iTextSharp将页面书签添加到现有PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的要求与此问题中描述的相同:书签使用iTextSharp 4.1.6到特定页面

我尝试了答案,但最后一行给出了以下错误:

无法将ArrayList隐式转换为IList<Dictionar(strin,object)>

我不确定如何纠正它.

出现错误的行是在上面链接的答案1中

wri.Outlines = bookmarks

我修改了这样的示例:

List<Dictionary<string, object>> testData = new List<Dictionary<string, object>>(100);
// Just Sample data for understanding.
//for (int i = 0; i < 100; i++)
//{
var test = new Dictionary<string, object>
{
    { "Action", "GoTo"},
    { "Title", "Page1 0 H 0" },
    {"Page", "1 XYZ 0 " + h + " 0" }
};
testData.Add(test);
//}
wri.Outlines = testData;

现在添加书签后,由于文件已损坏,我无法打开PDF.

解决方案

请查阅官方文档.

第7章中讨论了书签,您将在其中找到已添加书签的时间表示例.在使用iTextSharp(C#)而不是iText(Java)时,您需要在对经过数字签名的PDF可以进行哪些操作?不允许添加书签,因此您在应用更改之前,必须删除所有签名字段.根据要保留空白签名字段还是完全删除该字段,将使用方法clearSignatureField()removeField()传递签名字段的名称作为参数.您可以使用getSignatureNames()方法获得名称的ArrayList(请参见Bookmark to specific page using iTextSharp 4.1.6

I tried the answer, but the last line gives my the following error:

Can not implicitly convert ArrayList to IList<Dictionar(strin,object)>

I am not sure how to correct it.

The line that gave error is, in the Answer 1 of above link

wri.Outlines = bookmarks

I modified the example like this:

List<Dictionary<string, object>> testData = new List<Dictionary<string, object>>(100);
// Just Sample data for understanding.
//for (int i = 0; i < 100; i++)
//{
var test = new Dictionary<string, object>
{
    { "Action", "GoTo"},
    { "Title", "Page1 0 H 0" },
    {"Page", "1 XYZ 0 " + h + " 0" }
};
testData.Add(test);
//}
wri.Outlines = testData;

Now after adding the bookmarks, I'm unable to open the PDF because the file has been corrupted.

解决方案

Please consult the official documentation.

Bookmarks are discussed in Chapter 7 where you'll find the BookmarkedTimeTable example. As you're working with iTextSharp (C#), not iText (Java), you'll want to look up the corresponding example in the list of examples ported to C#, more specifically BookmarkedTimeTable.cs.

Update after you updated the question:

Your error indicates that testdata isn't really of type IList<Dictionar(string,object)>; note that I've added a g to strin assuming that this is a typo in your question, not in your actual code.

Please start with the example from my book, and then change that example gradually until you break that code.

Update based on extra comment:

When a PDF is digitally signed, there is a limited number of operations that may (or may not) be allowed. See Which operations are allowed on a digitally signed PDF? Adding bookmarks is not an allowed operation, so you'll have to remove all signature fields before applying the change. Depending on whether or not you want to keep the empty signature field or completely remove the field, you'll use either the method clearSignatureField() or removeField() passing the name of a signature field as parameter. You can get an ArrayList of names using the getSignatureNames() method (see ÀcroFields).

这篇关于使用C#代码使用iTextSharp将页面书签添加到现有PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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