将继承缩放(动作属性)设置为pdf文件中的书签 [英] Set inherit Zoom(action property) to bookmark in the pdf file

查看:510
本文介绍了将继承缩放(动作属性)设置为pdf文件中的书签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码找到了pdf文件中的书签数量。

  var reader = new PdfReader(System .Windows.Forms.Application.StartupPath +\\zoom.pdf,新的System.Text.ASCIIEncoding()。GetBytes()); 
IList< Dictionary< string,object>>书签= SimpleBookmark.GetBookmark(读者);
foreach(字典<字符串,对象>书签中的bk)
{
string bjj = bk.Values.ToArray()。GetValue(0).ToString();
}

但我需要在pdf中设置书签的继承缩放动作属性文件。



请告诉我从上面的代码或C#中的任何其他示例代码中做起来

解决方案

这个问题是同一个OP对以下问题的变体:






  • 您可以将Fit替换为XYZ 30 100 0,而不是将Fit替换为FitV 60。在这种情况下,30和100是X,Y坐标。第三个数字是缩放级别,但是如果选择0,则会得到继承缩放。


    I have found the number of bookmarks in the pdf file using the following code.

    var reader = new PdfReader(System.Windows.Forms.Application.StartupPath + "\\zoom.pdf", new System.Text.ASCIIEncoding().GetBytes(""));
    IList<Dictionary<string, object>> bookmarks = SimpleBookmark.GetBookmark(reader);
    foreach (Dictionary<string, object> bk in bookmarks)
    {
        string bjj = bk.Values.ToArray().GetValue(0).ToString();
    }
    

    But i need to set the inherit zoom action property for book bookmark in the pdf file.

    please advise me to do from above code or any other sample code in C#

    解决方案

    This question is a variation on the following questions by the same OP:

    The previous versions of this were very unclear. Based on the comments on the previous questions, the OP wants to take the Outline tree using the SimpleBookmark class, change the Destinations similar to the way he changed the destinations of annotations (as shown in C# - Set inherit zoom action for all the bookmarks in the PDF file) and then persist these changes.

    When using SimpleBookmark, you get the outlines in a form that is similar to this XML-file:

    <Bookmark>
        <Title Action="GoTo" Style="bold" Page="1 FitH 572">Akira</Title>
    <Bookmark>
    

    You can change this XML file (or in the case of the OP the contents of the Dictionary<string, object> obtained from SimpleBookmark), for instance, you could change 1 Fith 572 into 1 Fith 580. This is outside the scope of iText: it's a matter of changing XML or changing Strings in a Dictionary.

    Once you've made the change, you need to persist the change in the PDF. For instance, you have a bookmarks object:

    List<Dictionary<String, Object>> bookmarks;
    

    and all the changes are reflected in this bookmarks object.

    Now you can use PdfStamper's setOutline() method (Java) to change the outlines of the document. In C#, it would be something like this:

    stamper.Outlines = bookmarks;
    

    This is only one way to approach it. Obviously, it's also possible to walk through the outline objects and change the destinations at the lowest PDF level.

    If you want an example, see the ChangeBookmarks sample:

    public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
        PdfReader reader = new PdfReader(src);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
        List<HashMap<String, Object>> list = SimpleBookmark.getBookmark(reader);
        changeList(list);
        stamper.setOutlines(list);
        stamper.close();
        reader.close();
    }
    
    public void changeList(List<HashMap<String, Object>> list) {
        for (HashMap<String, Object> entry : list) {
            for (String key : entry.keySet()) {
                if ("Kids".equals(key)) {
                    Object o = entry.get(key);
                    changeList((List<HashMap<String, Object>>)o);
                }
                else if ("Page".equals(key)) {
                    String dest = (String)entry.get(key);
                    entry.put("Page", dest.replaceAll("Fit", "FitV 60"));
                }
            }
        }
    }
    

    As already explained, you get all the bookmarks as a List<HashMap<String, Object>>. You write a recursive method that goes through the outline tree ("Kids") and changes all the values of the Page entry. In my case, the bookmarks in bookmarks.pdf are all of type "Fit". I replace "Fit" with "FitV 60". Take a look at changed_bookmarks.pdf to see the difference:

    Suppose that you want an XYZ destination where you inherit the zoom factor, you can take do something that is similar to what you did in a previous question you posted: How to set zoom level to pdf using iTextSharp?

    Instead of replacing "Fit" with "FitV 60", you can replace "Fit" by "XYZ 30 100 0". In this case, 30 and 100 are X,Y coordinates. The third number is the zoom level, but if you choose 0, you get "Inherit zoom".

    这篇关于将继承缩放(动作属性)设置为pdf文件中的书签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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