如何将书签添加到PDF文件? [英] How to Add bookmarks to PDF file?

查看:148
本文介绍了如何将书签添加到PDF文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有4个pdf模板文件,使用itextsharp我添加了值,并且我将4个pdf文件合并/添加到单个文档中,因此所有4个页面都在一个pdf文件名下。现在我想将书签添加到我的pdf文件中。有没有办法在C#?为了更好地理解,请参考下面的图像

i have 4 pdf templates files by using itextsharp i added values and i merged/added 4 pdf files into single document, so all 4 pages are under one single pdf file name.Now i want to add bookmark to my pdf file. is there any way to do in C# ?for better understanding ,please refer below images

这就是我想要做的,我没有得到任何错误,但我的pdf中仍然没有书签,我想添加带有4个部分的书签,如图中所示。想要添加书签到最终的PDF格式。

Hi ,this is what i am trying to do, i am not getting any error but still there is no bookmark in my pdf, i want to add bookmark with 4 sections as showed in the image.after merging i want add bookmark to final pdf.

enter code herepublic string MergePDFs()
    {
        string outPutFilePath = @"D:\jeldsbre.pdf";
        string genereatedpdfs = @"D:\genereatedpdfs";

        using (FileStream stream = new FileStream(outPutFilePath, FileMode.Create))
        {
            Document pdfDoc = new Document(PageSize.A4);
            PdfCopy pdf = new PdfCopy(pdfDoc, stream);
            pdf.SetMergeFields();
            pdfDoc.Open();
            var files = Directory.GetFiles(genereatedpdfs);
            Console.WriteLine("Merging files count: " + files.Length);
            int i = 1;
            foreach (string file in files)
            {
                Console.WriteLine(i + ". Adding: " + file);
                pdf.AddDocument(new PdfReader(file));

                i++;
            }
          List<Dictionary<string, object>> bookmarks = new List<Dictionary<string, object>>();
            IList<Dictionary<string, object>> tempBookmarks =  new List<Dictionary<string, object>>();
            SimpleBookmark.ShiftPageNumbers(tempBookmarks, 1, null);
            bookmarks.AddRange(tempBookmarks);
            SimpleBookmark.ShiftPageNumbers(tempBookmarks, 3, null);
           bookmarks.AddRange(tempBookmarks);
           pdf.Outlines = bookmarks;
           if (pdfDoc != null)
                pdfDoc.Close();
            string base64 = GetBase64(outPutFilePath);
            return base64;
        }               

    }


推荐答案

假设您的原始PDF已经有书签,那么您不仅应该连接文档(使用 PdfCopy 类),还应该连接不同的书签结构。不同的文件(使用 SimpleBookMark 类),不要忘记考虑到你需要正确移动页码。

Assuming that your original PDFs already have bookmarks, then you should concatenate not only the documents (using the PdfCopy class), you should also concatenate the different bookmarks structures of the different files (using the SimpleBookMark class), not forgetting to take into account that you need to shift the page numbers correctly.

这是在我书中第7章的 ConcatenateBookmarks 示例中完成的:

This is done in the ConcatenateBookmarks example in chapter 7 of my book:

// Create a list for the bookmarks
ArrayList<HashMap<String, Object>> bookmarks = new ArrayList<HashMap<String, Object>>();
List<HashMap<String, Object>> tmp;
for (int i  = 0; i < src.length; i++) {
    reader = new PdfReader(src[i]);
    // merge the bookmarks
    tmp = SimpleBookmark.getBookmark(reader);
    SimpleBookmark.shiftPageNumbers(tmp, page_offset, null);
    bookmarks.addAll(tmp);
    // add the pages
    n = reader.getNumberOfPages();
    page_offset += n;
    for (int page = 0; page < n; ) {
        copy.addPage(copy.getImportedPage(reader, ++page));
    }
    copy.freeReader(reader);
    reader.close();
}
// Add the merged bookmarks
copy.setOutlines(bookmarks);

对于此示例的C#版本,请查看 http://tinyurl.com/itextsharpIIA2C07 对应的iTextSharp示例:

For a C# version of this example, please take a look at http://tinyurl.com/itextsharpIIA2C07 for the corresponding iTextSharp example:

// Create a list for the bookmarks
List<Dictionary<String, Object>> bookmarks = 
    new List<Dictionary<String, Object>>();            
for (int i  = 0; i < src.Count; i++) {
    PdfReader reader = new PdfReader(src[i]);
    // merge the bookmarks
    IList<Dictionary<String, Object>> tmp = 
    SimpleBookmark.GetBookmark(reader);
    SimpleBookmark.ShiftPageNumbers(tmp, page_offset, null);
    foreach (var d in tmp) bookmarks.Add(d);
    // add the pages
    int n = reader.NumberOfPages;
    page_offset += n;
    for (int page = 0; page < n; ) {
        copy.AddPage(copy.GetImportedPage(reader, ++page));
    }
}
// Add the merged bookmarks
copy.Outlines = bookmarks;

如果现有文件没有任何书签(或者如果您不想复制任何书签)现有文件),那么你的问题是我半年前回答的一个问题的副本:合并pdf并在java中添加带有iText的书签

If the existing documents don't have any bookmarks (or if you don't want to copy any existing documents), then your question is a duplicate of a question I answered half a year ago: Merge pdfs and add bookmark with iText in java

这篇关于如何将书签添加到PDF文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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