public static void Run() { // ExStart:GetBookmarkPageNumber // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_Bookmarks(); // Create PdfBookmarkEditor PdfBookmarkEditor bookmarkEditor = new PdfBookmarkEditor(); // Open PDF file bookmarkEditor.BindPdf(dataDir + "GetBookmarks.pdf"); // Extract bookmarks Aspose.Pdf.Facades.Bookmarks bookmarks = bookmarkEditor.ExtractBookmarks(); foreach (Aspose.Pdf.Facades.Bookmark bookmark in bookmarks) { string strLevelSeprator = string.Empty; for (int i = 1; i < bookmark.Level; i++) { strLevelSeprator += "----"; } Console.WriteLine("{0}Title: {1}", strLevelSeprator, bookmark.Title); Console.WriteLine("{0}Page Number: {1}", strLevelSeprator, bookmark.PageNumber); Console.WriteLine("{0}Page Action: {1}", strLevelSeprator, bookmark.Action); } // ExEnd:GetBookmarkPageNumber }
public void CreateMissingOutlineLevels() { //ExStart //ExFor:OutlineOptions.CreateMissingOutlineLevels //ExFor:ParagraphFormat.IsHeading //ExFor:PdfSaveOptions.OutlineOptions //ExFor:PdfSaveOptions.SaveFormat //ExSummary:Shows how to create missing outline levels saving the document in PDF. Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Creating TOC entries builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1; Assert.True(builder.ParagraphFormat.IsHeading); builder.Writeln("Heading 1"); builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading4; builder.Writeln("Heading 1.1.1.1"); builder.Writeln("Heading 1.1.1.2"); builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading9; builder.Writeln("Heading 1.1.1.1.1.1.1.1.1"); builder.Writeln("Heading 1.1.1.1.1.1.1.1.2"); // Create "PdfSaveOptions" with some mandatory parameters // "HeadingsOutlineLevels" specifies how many levels of headings to include in the document outline // "CreateMissingOutlineLevels" determining whether or not to create missing heading levels PdfSaveOptions pdfSaveOptions = new PdfSaveOptions(); pdfSaveOptions.OutlineOptions.HeadingsOutlineLevels = 9; pdfSaveOptions.OutlineOptions.CreateMissingOutlineLevels = true; pdfSaveOptions.SaveFormat = SaveFormat.Pdf; doc.Save(ArtifactsDir + "CreateMissingOutlineLevels.pdf", pdfSaveOptions); //ExEnd #if NETFRAMEWORK || NETSTANDARD2_0 // Bind PDF with Aspose.PDF PdfBookmarkEditor bookmarkEditor = new PdfBookmarkEditor(); bookmarkEditor.BindPdf(ArtifactsDir + "CreateMissingOutlineLevels.pdf"); // Get all bookmarks from the document Bookmarks bookmarks = bookmarkEditor.ExtractBookmarks(); Assert.AreEqual(11, bookmarks.Count); #endif }
public static void Main() { // The path to the documents directory. string dataDir = Path.GetFullPath("../../../Data/"); //create PdfBookmarkEditor PdfBookmarkEditor bookmarkEditor = new PdfBookmarkEditor(); //open PDF file bookmarkEditor.BindPdf(dataDir+ "input.pdf"); //extract bookmarks Bookmarks bookmarks = bookmarkEditor.ExtractBookmarks(); foreach (Bookmark bookmark in bookmarks) { Console.WriteLine("Title: {0}", bookmark.Title); Console.WriteLine("Page Number: {0}", bookmark.PageNumber); } }
public void AddBookmarkWithWhiteSpaces(SaveFormat saveFormat) { Document doc = new Document(); InsertBookmarks(doc); if (saveFormat == SaveFormat.Pdf) { //Save document with pdf save options doc.Save(MyDir + @"\Artifacts\Bookmark_WhiteSpaces.pdf", AddBookmarkSaveOptions(SaveFormat.Pdf)); //Bind pdf with Aspose PDF PdfBookmarkEditor bookmarkEditor = new PdfBookmarkEditor(); bookmarkEditor.BindPdf(MyDir + @"\Artifacts\Bookmark_WhiteSpaces.pdf"); //Get all bookmarks from the document Bookmarks bookmarks = bookmarkEditor.ExtractBookmarks(); Assert.AreEqual(3, bookmarks.Count); //Assert that all the bookmarks title are with witespaces Assert.AreEqual("My Bookmark", bookmarks[0].Title); Assert.AreEqual("Nested Bookmark", bookmarks[1].Title); //Assert that the bookmark title without witespaces Assert.AreEqual("Bookmark_WithoutWhiteSpaces", bookmarks[2].Title); } else { MemoryStream dstStream = new MemoryStream(); doc.Save(dstStream, AddBookmarkSaveOptions(saveFormat)); //Get bookmarks from the document BookmarkCollection bookmarks = doc.Range.Bookmarks; Assert.AreEqual(3, bookmarks.Count); //Assert that all the bookmarks title are with witespaces Assert.AreEqual("My Bookmark", bookmarks[0].Name); Assert.AreEqual("Nested Bookmark", bookmarks[1].Name); //Assert that the bookmark title without witespaces Assert.AreEqual("Bookmark_WithoutWhiteSpaces", bookmarks[2].Name); } }
public void CreateMissingOutlineLevels() { //ExStart //ExFor:Saving.PdfSaveOptions.OutlineOptions.CreateMissingOutlineLevels //ExSummary:Shows how to create missing outline levels saving the document in pdf Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Creating TOC entries builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1; builder.Writeln("Heading 1"); builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading4; builder.Writeln("Heading 1.1.1.1"); builder.Writeln("Heading 1.1.1.2"); builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading9; builder.Writeln("Heading 1.1.1.1.1.1.1.1.1"); builder.Writeln("Heading 1.1.1.1.1.1.1.1.2"); //Create "PdfSaveOptions" with some mandatory parameters //"HeadingsOutlineLevels" specifies how many levels of headings to include in the document outline //"CreateMissingOutlineLevels" determining whether or not to create missing heading levels PdfSaveOptions pdfSaveOptions = new PdfSaveOptions(); pdfSaveOptions.OutlineOptions.HeadingsOutlineLevels = 9; pdfSaveOptions.OutlineOptions.CreateMissingOutlineLevels = true; pdfSaveOptions.SaveFormat = SaveFormat.Pdf; doc.Save(MyDir + @"\Artifacts\CreateMissingOutlineLevels.pdf", pdfSaveOptions); //ExEnd //Bind pdf with Aspose PDF PdfBookmarkEditor bookmarkEditor = new PdfBookmarkEditor(); bookmarkEditor.BindPdf(MyDir + @"\Artifacts\CreateMissingOutlineLevels.pdf"); //Get all bookmarks from the document Bookmarks bookmarks = bookmarkEditor.ExtractBookmarks(); Assert.AreEqual(11, bookmarks.Count); }
public static void Main() { // The path to the documents directory. string dataDir = Path.GetFullPath("../../../Data/"); //create PdfBookmarkEditor PdfBookmarkEditor bookmarkEditor = new PdfBookmarkEditor(); //open PDF file bookmarkEditor.BindPdf(dataDir + "input.pdf"); //extract bookmarks Bookmarks bookmarks = bookmarkEditor.ExtractBookmarks(); foreach (Bookmark bookmark in bookmarks) { Console.WriteLine("Title: {0}", bookmark.Title); Console.WriteLine("Page Number: {0}", bookmark.PageNumber); } }
public static void Run() { // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdfFacades_Bookmarks(); //create PdfBookmarkEditor PdfBookmarkEditor bookmarkEditor = new PdfBookmarkEditor(); //open PDF file bookmarkEditor.BindPdf(dataDir + "ExtractBookmarks.pdf"); //extract bookmarks Aspose.Pdf.Facades.Bookmarks bookmarks = bookmarkEditor.ExtractBookmarks(); foreach (Bookmark bookmark in bookmarks) { Console.WriteLine("Title: {0}", bookmark.Title); Console.WriteLine("Page Number: {0}", bookmark.PageNumber); } }
public void AllowToAddBookmarksWithWhiteSpaces() { //ExStart //ExFor:OutlineOptions.BookmarksOutlineLevels //ExFor:BookmarksOutlineLevelCollection.Add(String, Int32) //ExSummary:Shows how adding bookmarks outlines with whitespaces(pdf, xps) Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); //Add bookmarks with whitespaces. MS Word formats (like doc, docx) does not support bookmarks with whitespaces by default //and all whitespaces in the bookmarks were replaced with underscores. If you need to use bookmarks in PDF or XPS outlines, you can use them with whitespaces. builder.StartBookmark("My Bookmark"); builder.Writeln("Text inside a bookmark."); builder.StartBookmark("Nested Bookmark"); builder.Writeln("Text inside a NestedBookmark."); builder.EndBookmark("Nested Bookmark"); builder.Writeln("Text after Nested Bookmark."); builder.EndBookmark("My Bookmark"); //Specify bookmarks outline level. If you are using xps format, just use XpsSaveOptions. PdfSaveOptions pdfSaveOptions = new PdfSaveOptions(); pdfSaveOptions.OutlineOptions.BookmarksOutlineLevels.Add("My Bookmark", 1); pdfSaveOptions.OutlineOptions.BookmarksOutlineLevels.Add("Nested Bookmark", 2); doc.Save(MyDir + @"\Artifacts\Bookmarks.WhiteSpaces Out.pdf", pdfSaveOptions); //ExEnd //Bind pdf with Aspose.Pdf PdfBookmarkEditor bookmarkEditor = new PdfBookmarkEditor(); bookmarkEditor.BindPdf(MyDir + @"\Artifacts\Bookmarks.WhiteSpaces Out.pdf"); //Get all bookmarks from the document Bookmarks bookmarks = bookmarkEditor.ExtractBookmarks(); Assert.AreEqual(2, bookmarks.Count); //Assert that all the bookmarks title are with whitespaces Assert.AreEqual("My Bookmark", bookmarks[0].Title); Assert.AreEqual("Nested Bookmark", bookmarks[1].Title); }
public static void Run() { // ExStart:ExtractBookmarks // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdfFacades_Bookmarks(); // Create PdfBookmarkEditor PdfBookmarkEditor bookmarkEditor = new PdfBookmarkEditor(); // Open PDF file bookmarkEditor.BindPdf(dataDir+ "ExtractBookmarks.pdf"); // Extract bookmarks Aspose.Pdf.Facades.Bookmarks bookmarks = bookmarkEditor.ExtractBookmarks(); foreach (Bookmark bookmark in bookmarks) { Console.WriteLine("Title: {0}", bookmark.Title); Console.WriteLine("Page Number: {0}", bookmark.PageNumber); } // ExEnd:ExtractBookmarks }
public static void Main() { // The path to the documents directory. string dataDir = Path.GetFullPath("../../../Data/"); //create PdfBookmarkEditor PdfBookmarkEditor bookmarkEditor = new PdfBookmarkEditor(); //open PDF file bookmarkEditor.BindPdf(dataDir+ "input.PDF"); //extract bookmarks Aspose.Pdf.Facades.Bookmarks bookmarks = bookmarkEditor.ExtractBookmarks(); foreach (Aspose.Pdf.Facades.Bookmark bookmark in bookmarks) { // get the title information of bookmark item Console.WriteLine("Title: {0}", bookmark.Title); // get the destination page for bookmark Console.WriteLine("Page Number: {0}", bookmark.PageNumber); // get the information related to associated action with bookmark Console.WriteLine("Bookmark Action: " + bookmark.Action); } }
public static void Main() { // The path to the documents directory. string dataDir = Path.GetFullPath("../../../Data/"); //create PdfBookmarkEditor PdfBookmarkEditor bookmarkEditor = new PdfBookmarkEditor(); //open PDF file bookmarkEditor.BindPdf(dataDir + "input.PDF"); //extract bookmarks Aspose.Pdf.Facades.Bookmarks bookmarks = bookmarkEditor.ExtractBookmarks(); foreach (Aspose.Pdf.Facades.Bookmark bookmark in bookmarks) { // get the title information of bookmark item Console.WriteLine("Title: {0}", bookmark.Title); // get the destination page for bookmark Console.WriteLine("Page Number: {0}", bookmark.PageNumber); // get the information related to associated action with bookmark Console.WriteLine("Bookmark Action: " + bookmark.Action); } }
public static void Run() { // ExStart:GetFromPDF // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdfFacades_Bookmarks(); // Create PdfBookmarkEditor PdfBookmarkEditor bookmarkEditor = new PdfBookmarkEditor(); // Open PDF file bookmarkEditor.BindPdf(dataDir + "GetFromPDF.PDF"); // Extract bookmarks Aspose.Pdf.Facades.Bookmarks bookmarks = bookmarkEditor.ExtractBookmarks(); foreach (Aspose.Pdf.Facades.Bookmark bookmark in bookmarks) { // Get the title information of bookmark item Console.WriteLine("Title: {0}", bookmark.Title); // Get the destination page for bookmark Console.WriteLine("Page Number: {0}", bookmark.PageNumber); // Get the information related to associated action with bookmark Console.WriteLine("Bookmark Action: " + bookmark.Action); } // ExEnd:GetFromPDF }
public void BookmarkLevels() { //ExStart //ExFor:BookmarksOutlineLevelCollection //ExFor:BookmarksOutlineLevelCollection.Add(String, Int32) //ExFor:BookmarksOutlineLevelCollection.Clear //ExFor:BookmarksOutlineLevelCollection.Contains(System.String) //ExFor:BookmarksOutlineLevelCollection.Count //ExFor:BookmarksOutlineLevelCollection.IndexOfKey(System.String) //ExFor:BookmarksOutlineLevelCollection.Item(System.Int32) //ExFor:BookmarksOutlineLevelCollection.Item(System.String) //ExFor:BookmarksOutlineLevelCollection.Remove(System.String) //ExFor:BookmarksOutlineLevelCollection.RemoveAt(System.Int32) //ExFor:OutlineOptions.BookmarksOutlineLevels //ExSummary:Shows how to set outline levels for bookmarks. Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Insert a bookmark with another bookmark nested inside it. builder.StartBookmark("Bookmark 1"); builder.Writeln("Text inside Bookmark 1."); builder.StartBookmark("Bookmark 2"); builder.Writeln("Text inside Bookmark 1 and 2."); builder.EndBookmark("Bookmark 2"); builder.Writeln("Text inside Bookmark 1."); builder.EndBookmark("Bookmark 1"); // Insert another bookmark. builder.StartBookmark("Bookmark 3"); builder.Writeln("Text inside Bookmark 3."); builder.EndBookmark("Bookmark 3"); // When saving to .pdf, bookmarks can be accessed via a drop-down menu and used as anchors by most readers. // Bookmarks can also have numeric values for outline levels, // enabling lower level outline entries to hide higher-level child entries when collapsed in the reader. PdfSaveOptions pdfSaveOptions = new PdfSaveOptions(); BookmarksOutlineLevelCollection outlineLevels = pdfSaveOptions.OutlineOptions.BookmarksOutlineLevels; outlineLevels.Add("Bookmark 1", 1); outlineLevels.Add("Bookmark 2", 2); outlineLevels.Add("Bookmark 3", 3); Assert.AreEqual(3, outlineLevels.Count); Assert.True(outlineLevels.Contains("Bookmark 1")); Assert.AreEqual(1, outlineLevels[0]); Assert.AreEqual(2, outlineLevels["Bookmark 2"]); Assert.AreEqual(2, outlineLevels.IndexOfKey("Bookmark 3")); // We can remove two elements so that only the outline level designation for "Bookmark 1" is left. outlineLevels.RemoveAt(2); outlineLevels.Remove("Bookmark 2"); // There are nine outline levels. Their numbering will be optimized during the save operation. // In this case, levels "5" and "9" will become "2" and "3". outlineLevels.Add("Bookmark 2", 5); outlineLevels.Add("Bookmark 3", 9); doc.Save(ArtifactsDir + "BookmarksOutlineLevelCollection.BookmarkLevels.pdf", pdfSaveOptions); // Emptying this collection will preserve the bookmarks and put them all on the same outline level. outlineLevels.Clear(); //ExEnd #if NET462 || NETCOREAPP2_1 || JAVA PdfBookmarkEditor bookmarkEditor = new PdfBookmarkEditor(); bookmarkEditor.BindPdf(ArtifactsDir + "BookmarksOutlineLevelCollection.BookmarkLevels.pdf"); Bookmarks bookmarks = bookmarkEditor.ExtractBookmarks(); Assert.AreEqual(3, bookmarks.Count); Assert.AreEqual("Bookmark 1", bookmarks[0].Title); Assert.AreEqual("Bookmark 2", bookmarks[1].Title); Assert.AreEqual("Bookmark 3", bookmarks[2].Title); #endif }
public void BookmarkLevels() { //ExStart //ExFor:BookmarksOutlineLevelCollection //ExFor:BookmarksOutlineLevelCollection.Add(String, Int32) //ExFor:BookmarksOutlineLevelCollection.Clear //ExFor:BookmarksOutlineLevelCollection.Contains(System.String) //ExFor:BookmarksOutlineLevelCollection.Count //ExFor:BookmarksOutlineLevelCollection.IndexOfKey(System.String) //ExFor:BookmarksOutlineLevelCollection.Item(System.Int32) //ExFor:BookmarksOutlineLevelCollection.Item(System.String) //ExFor:BookmarksOutlineLevelCollection.Remove(System.String) //ExFor:BookmarksOutlineLevelCollection.RemoveAt(System.Int32) //ExFor:OutlineOptions.BookmarksOutlineLevels //ExSummary:Shows how to set outline levels for bookmarks. // Open a blank document, create a DocumentBuilder, and use the builder to add some text wrapped inside bookmarks Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Note that whitespaces in bookmark names will be converted into underscores when saved to Microsoft Word formats // such as .doc and .docx, but will be preserved in other formats like .pdf or .xps builder.StartBookmark("Bookmark 1"); builder.Writeln("Text inside Bookmark 1."); builder.StartBookmark("Bookmark 2"); builder.Writeln("Text inside Bookmark 1 and 2."); builder.EndBookmark("Bookmark 2"); builder.Writeln("Text inside Bookmark 1."); builder.EndBookmark("Bookmark 1"); builder.StartBookmark("Bookmark 3"); builder.Writeln("Text inside Bookmark 3."); builder.EndBookmark("Bookmark 3"); // We can specify outline levels for our bookmarks so that they show up in the table of contents and are indented by an amount // of space proportional to the indent level in a SaveOptions object // Some pdf/xps readers such as Google Chrome also allow the collapsing of all higher level bookmarks by adjacent lower level bookmarks // This feature applies to .pdf or .xps file formats, so only their respective SaveOptions subclasses will support it PdfSaveOptions pdfSaveOptions = new PdfSaveOptions(); BookmarksOutlineLevelCollection outlineLevels = pdfSaveOptions.OutlineOptions.BookmarksOutlineLevels; outlineLevels.Add("Bookmark 1", 1); outlineLevels.Add("Bookmark 2", 2); outlineLevels.Add("Bookmark 3", 3); Assert.AreEqual(3, outlineLevels.Count); Assert.True(outlineLevels.Contains("Bookmark 1")); Assert.AreEqual(1, outlineLevels[0]); Assert.AreEqual(2, outlineLevels["Bookmark 2"]); Assert.AreEqual(2, outlineLevels.IndexOfKey("Bookmark 3")); // We can remove two elements so that only the outline level designation for "Bookmark 1" is left outlineLevels.RemoveAt(2); outlineLevels.Remove("Bookmark 2"); // We have 9 bookmark levels to work with, and bookmark levels are also sorted in ascending order, // and get numbered in succession along that order // Practically this means that our three levels "1, 5, 9", will be seen as "1, 2, 3" in the output outlineLevels.Add("Bookmark 2", 5); outlineLevels.Add("Bookmark 3", 9); // Save the document as a .pdf and find links to the bookmarks and their outline levels doc.Save(ArtifactsDir + "BookmarksOutlineLevelCollection.BookmarkLevels.pdf", pdfSaveOptions); // We can empty this dictionary to remove the contents table outlineLevels.Clear(); //ExEnd #if NETFRAMEWORK || NETSTANDARD2_0 // Bind pdf with Aspose.Pdf PdfBookmarkEditor bookmarkEditor = new PdfBookmarkEditor(); bookmarkEditor.BindPdf(ArtifactsDir + "BookmarksOutlineLevelCollection.BookmarkLevels.pdf"); // Get all bookmarks from the document Bookmarks bookmarks = bookmarkEditor.ExtractBookmarks(); Assert.AreEqual(3, bookmarks.Count); // Assert that all the bookmarks title are with whitespaces Assert.AreEqual("Bookmark 1", bookmarks[0].Title); Assert.AreEqual("Bookmark 2", bookmarks[1].Title); Assert.AreEqual("Bookmark 3", bookmarks[2].Title); #endif }