0

I have a method that fixes my document so it opens on Page Level Zoom, the bookmarks are retained but I can't seem to keep my named destinations (anchors) in my new document. I commented in my method what I thought would fix the problem, I used the code before in another method.

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);                
        }
    }

After the comment I fixed it with this code.

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);
        }
    }
  • You are using `PdfWriter` where you should use `PdfStamper`. Please read the documentation. That is: either [chapter 6 of my book](http://manning.com/lowagie2/samplechapter6.pdf). You'll learn that creating a `Document` from scratch using `PdfWriter` throws away all interactivity (this includes named destinations, bookmarks,...), whereas `PdfStamper` preserves them. – Bruno Lowagie Jan 30 '15 at 15:22
  • I'll give it a read and comment when I'm done – Maurice van Lieshout Jan 30 '15 at 16:31
  • Actually, I'm going to vote to close your question as a duplicate of [How to set zoom level to pdf using iTextSharp?](http://stackoverflow.com/questions/24087786/how-to-set-zoom-level-to-pdf-using-itextsharp-5-5-0-0) – Bruno Lowagie Jan 30 '15 at 16:34
  • 1
    A solution should be given as an answer, not as an extension of the question. – mkl Jan 30 '15 at 20:24
  • 1
    I"m sorry i'm not using stackoverflow that often. I wanted to add my new method so people who have the same problem can see my solution. I don't see code markup for comments. – Maurice van Lieshout Jan 30 '15 at 21:14

0 Answers0