We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How do I have PDF bookmarks be initially expanded when the PDF file is opened?

I have a question about PDF bookmarks in Aspose.PDF. I put together some code that uses Aspose.Cells and Aspose.PDF to generate a PDF file with bookmarks. See my question in the Aspose.Cells forum here, http://www.aspose.com/community/forums/addpost.aspx?PostID=572750


I put in bookmarks using PDFBookmarkEntry objects. I wanted the bookmarks to take the user to the very top of the the page, not the corresponding cell. An Aspose staff member replied showing me how to do this using Aspose.Pdf.OutlineItemCollection objects, but my problem with that solution is that the bookmarks are not initially expanded the way they were when I used Aspose.Cells bookmarks. The Aspose staff member sent me here because he was unable or unwilling to help me out. My code for generating my PDF bookmarks is in the linked forum post.

How do I have the bookmarks be initially expanded? There must be a way to do this since I can do it with the Aspose.Cells bookmarks (see the PDF file called file.pdf I attached to the forum question I linked above)

I have a question about PDF Bookmarks in Aspose.Cells. I have a simple Aspose.Cells workbook with one worksheet. I am exporting this workbook to a PDF file. The workbook has 100 rows of data in it and I inserted a page break every 25 rows. Immediately following each page break, I put in a PDFBookmarkEntry. My problem involves the behavior of the bookmarks when I open the file in a PDF reader.


When I open the PDF file and zoom in so that only a portion of the page is visible, and then click on a bookmark entry, I’m taken directly to the destination cell. The thing though is that I want the bookmark entry to take me to the top of the page, not the cell at the top of the page. This difference is evident when the worksheet has a header. Clicking the bookmark will take you to the cell, but not the very top of the page, cutting off the “File Header”. This is hard to describe, so I attached some screenshots.

The attached Current.PNG shows what happens when you click on “Row number 51”. See how the page is navigated to the cell “Row number 51” but it’s not navigated to the very top of that page? The user has to scroll up to see the “File Header”. Note that the file is zoomed in to page width (150% in this case).

The attached Ideal.PNG shows what I would like to happen when you click on “Row number 51”. The page is navigated to the top of the page. Again, the file is zoomed in to page width.

Note that the navigation works as I’d like it to when I select “fit one page to window”. I’ve attached the actual PDf file so you can see for yourself how the bookmarks work.

In summary: how do I insert a PDF Bookmark Entry that will navigate to the very top of the page when clicked? Not the cell at the top of the page.

Here is my C# code that generates the attached PDF file. I tried to keep it short.

static void Main(string[] args)
{
Program.Licenses();

var wb = new Workbook();
var ws = wb.Worksheets[0];

var bmRoot = new PdfBookmarkEntry()
{
IsOpen = true,
Destination = ws.Cells[0, 0],
Text = “Root”,
SubEntry = new System.Collections.ArrayList()
};

for (int i = 0; i < 100; i++)
{
var cell = ws.Cells[i, 0];

//text
cell.Value = string.Format(“Row number {0}”, i + 1);
//bg color
{
var style = cell.GetStyle();
style.ForegroundColor = System.Drawing.Color.LightGray;
style.Pattern = BackgroundType.Solid;
cell.SetStyle(style);
}

if (i % 25 == 0)
{
ws.HorizontalPageBreaks.Add(i);

var bmCell = new PdfBookmarkEntry()
{
IsOpen = true,
Destination = cell,
Text = string.Format(“Row number {0}”, i + 1)
};
bmRoot.SubEntry.Add(bmCell);
}
}

{ //header
const int LEFT = 0;
var ps = ws.PageSetup;
ps.SetHeader(LEFT, “&16&BFile Header”);
}


var so = new PdfSaveOptions(SaveFormat.Pdf);
so.Bookmark = bmRoot;

string path = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), “file.pdf”);
wb.Save(path, so);
}

Hi Jordan,

Thanks for your posting and using Aspose.Cells.

It is a correct behavior. Bookmarks generated by Aspose.Cells can only navigate to cells, they cannot navigate to any location inside the PDF page. So you cannot move to the very top of the page if there are headers on every page.

As a workaround, you can use Aspose.Pdf to add bookmarks to pages of your PDF.

( http://www.aspose.com/docs/display/pdfnet/Add+a+Bookmark+in+the+PDF+Document )

Is there a way to adjust the bookmarks using Aspose.PDF code, or something else that is not Aspose.Cells?

Hi,

Yes, I think, you can use Aspose.Pdf for this purpose. See my above post: 572748.

However, I cannot help you with Aspose.Pdf technically. You will have to post in Aspose.Pdf forum to get technical help in this regard.

That mostly works. I put together some code that converts my Aspose.Cells-derived bookmarks to Aspose.Pdf-derived bookmarks. I pasted it below.


My new problem is that, while this mostly works, I want the bookmarks to be expanded when a user opens the file. I can do this easily with the Aspose.Cells bookmarks, but I can’t find a way to do it with the Aspose.PDF bookmarks.

I’m also asking this question in the Aspose.Pdf forum for completeness since this is both an Aspose.Cells and an Aspose.PDF question. I don’t want them to tell me to ask this in the Aspose.Cells forum “to get technical help in this regard” Here’s the link: http://www.aspose.com/community/forums/572774/how-do-i-have-pdf-bookmarks-be-initially-expanded-when-the-pdf-file-is-opened/showthread.aspx#572774


private static void PDFBookmarks()
{
string path = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), “file.pdf”);
Aspose.Pdf.Document doc = new Aspose.Pdf.Document(path);

Aspose.Pdf.OutlineItemCollection root = null;

foreach (Aspose.Pdf.OutlineItemCollection itemRoot in doc.Outlines)
{
var destRoot = itemRoot.Destination as Aspose.Pdf.InteractiveFeatures.XYZExplicitDestination;
if (destRoot != null)
{
root = new Aspose.Pdf.OutlineItemCollection(doc.Outlines)
{
Title = itemRoot.Title,
Action = new Aspose.Pdf.InteractiveFeatures.GoToAction(doc.Pages[destRoot.PageNumber])
};

foreach (Aspose.Pdf.OutlineItemCollection itemChild in itemRoot)
{
var destChild = itemChild.Destination as Aspose.Pdf.InteractiveFeatures.XYZExplicitDestination;
if (destChild != null)
{
Aspose.Pdf.OutlineItemCollection child = new Aspose.Pdf.OutlineItemCollection(doc.Outlines)
{
Title = itemChild.Title,
Action = new Aspose.Pdf.InteractiveFeatures.GoToAction(doc.Pages[destChild.PageNumber])
};
root.Add(child);
}
}
}
}

doc.Outlines.Delete();
doc.Outlines.Add(root);

var contentEditor = new Aspose.Pdf.Facades.PdfContentEditor(doc);
contentEditor.ChangeViewerPreference(Aspose.Pdf.Facades.ViewerPreference.PageModeUseOutlines);
doc.Save(path);
}

Hi Jordan,

Thanks for your posting and using Aspose APIs.

I have asked this question from Aspose.Pdf support team and they provided me the following code to make bookmarks expanded. You can try it at your end and see how the results turn out.

You can do it using ViewPreference

C#


Document pdfDoc = new Document(myDir + “safety_group.pdf”);

Page pdfPage = pdfDoc.Pages.Add();


//pass your pdfDoc to PdfContentEdiot object to set preview preference

PdfContentEditor contentEditor = new PdfContentEditor(pdfDoc);


//change Viewer Preferences

contentEditor.ChangeViewerPreference(ViewerPreference.FitWindow);

contentEditor.ChangeViewerPreference(ViewerPreference.PageModeUseOutlines);

contentEditor.Save(myDir + “safety_group_out1.pdf”);

Hi Jordan,


Thanks for contacting support.

Your query has been answered in another thread over 572840.

In the event of any further query, please feel free to contact.

Are you saying that this line will cause the bookmarks to be expanded?


contentEditor.ChangeViewerPreference(ViewerPreference.PageModeUseOutlines);

That line must do something else because my bookmarks are not initially expanded when I open the output document. If you looked at the code I posted, you’d see that I already had that exact line is right at the bottom of my “private static void PDFBookmarks()” function.

I’ve attached two images that show what I’m looking for. The Collapsed.PNG image is what I get when I use the code you posted, which I was already using. The Expanded.PNG shows what I want to see when I open the file. I circled the red part showing that the bookmarks are expanded. I can do this easily using the Aspose.Cells code, but, as I explained above, I’d like to do this using the Aspose.PDF library.

Hi, I got a response to my other thread, but not an answer. The moderator told me to make this call to expand the bookmarks initially:


contentEditor.ChangeViewerPreference(Aspose.Pdf.Facades.ViewerPreference.PageModeUseOutlines);

Not only does this not cause the bookmarks to be initially expanded, but I already had that in my code which I posted in the thread. I’ve attached two image files that show what I want. Collapsed.PNG shows what the PDF file looks like when a user opens it now. I generated the Collapsed.PNG file using the code I already pasted which includes the PageModeUseOutlines call.

Expanded.PNG shows the file as I would like it to appear when the user opens it. The bookmarks are already expanded. I can do this easily using Aspose.Cells, but as I explained above, I want to do this using Aspose.PDF.

Hi Jordan,


Thanks for sharing the details.

I have tested the scenario in details and have found that currently Aspose.Pdf for .NET does not support the feature to make Bookmarks expanded when viewing the document. Currently it only opens the Bookmarks/Outlines area when viewing the file. For
the sake of correction, I have logged this requirement in our issue tracking system as PDFNEWNET-37492. We will
investigate this issue in details and will keep you updated on the status of a
correction.

We apologize for your inconvenience.

The issues you have found earlier (filed as PDFNEWNET-37492) have been fixed in Aspose.Pdf for .NET 9.8.0.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.