使用C#代码使用iTextSharp将页面书签添加到现有PDF [英] Add Page bookmarks to an existing PDF using iTextSharp using C# code
问题描述
我的要求与此问题中描述的相同:书签使用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)时,您需要在 BookmarkedTimeTable.cs .
更新问题后更新:
您的错误表明testdata
的类型实际上不是IList<Dictionar(string,object)>
;请注意,我已经在 strin 中添加了 g ,假设这是您的问题而不是您的实际代码中的错字.
请从我的书中的示例开始,然后逐渐更改该示例,直到破坏该代码为止.
根据其他评论进行更新:
对PDF进行数字签名时,可能会(或可能不会)进行有限数量的操作.请参见对经过数字签名的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
toIList<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屋!