iTextSharp在写入新文档后保留命名目的地和书签

时间:2015-01-30 15:02:22

标签: pdf itextsharp zoom bookmarks

我有一个修复我的文档的方法,因此它在页面级别缩放时打开,书签被保留但我似乎无法将我的命名目的地(锚点)保留在我的新文档中。我在我的方法中评论了我认为可以解决问题的方法,我之前在另一种方法中使用了代码。

private static void FixZoom(string source)
    {
        var reader = new PdfReader(new FileStream(source, FileMode.Open, FileAccess.Read));                        
        var size = reader.GetPageSizeWithRotation(1);

        using (var document = new iTextSharp.text.Document(size))
        {                
            var path = String.Empty;
            var dirName = Path.GetDirectoryName(source);
            if (dirName != null)
            {
                path = Path.Combine(dirName, "Zoom" + Path.GetFileName(source));
            }

            using (var stream = new FileStream(path, FileMode.Create, FileAccess.ReadWrite))
            {
                using (var writer = PdfWriter.GetInstance(document, stream))                
                {                                                                    
                    document.Open();

                    var bookmarks = SimpleBookmark.GetBookmark(reader);

                    var cb = writer.DirectContent;

                    var pdfDest = new PdfDestination(PdfDestination.FIT);

                    var action = PdfAction.GotoLocalPage(1, pdfDest, writer);

                    for (var pageNumber = 1; pageNumber <= reader.NumberOfPages; pageNumber++)
                    {                        
                        document.SetPageSize(reader.GetPageSizeWithRotation(pageNumber));
                        document.NewPage();
                        var page = writer.GetImportedPage(reader, pageNumber);
                        cb.AddTemplate(page, 0, 0);
                    }

                    //BEGIN: This is not working
                    var map = SimpleNamedDestination.GetNamedDestination(reader, false);
                    if (map.Count > 0)
                    {
                        writer.AddNamedDestinations(map, reader.NumberOfPages);
                    }
                    //END: This is not working

                    var pageMode = 0;
                    pageMode += PdfWriter.PageLayoutOneColumn;

                    writer.SetOpenAction(action);
                    writer.ViewerPreferences = pageMode;

                    writer.Outlines = bookmarks;
                    document.Close();
                }
            }

            File.Delete(source);
            File.Move(path, source);                
        }
    }

在评论之后我用这段代码修复了它。

private static void FixZoom(string source)
    {
        var reader = new PdfReader(new FileStream(source, FileMode.Open, FileAccess.Read));
        var size = reader.GetPageSizeWithRotation(1);

        using (var document = new iTextSharp.text.Document(size))
        {
            var path = String.Empty;
            var dirName = Path.GetDirectoryName(source);
            if (dirName != null)
            {
                path = Path.Combine(dirName, "Zoom" + Path.GetFileName(source));
            }

            using (var stream = new FileStream(path, FileMode.Create, FileAccess.ReadWrite))
            {
                using (var stamper = new PdfStamper(reader, stream))
                {
                    document.Open();                        
                    var pdfDestination = new PdfDestination(PdfDestination.FIT);
                    var pdfAction = PdfAction.GotoLocalPage(1, pdfDestination, stamper.Writer);

                    stamper.Writer.SetOpenAction(pdfAction);

                    document.Close();
                }                                                                
            }

            File.Delete(source);
            File.Move(path, source);
        }
    }

0 个答案:

没有答案