From eb892ab5245da1444d936319bffada8e749a3634 Mon Sep 17 00:00:00 2001 From: sameerkhan001 Date: Thu, 7 Aug 2025 11:31:05 +0530 Subject: [PATCH] 968706: Move and reorder bookmark in the PDF document. --- .../Move-and-reorder-bookmark-in-the-PDF.sln | 25 ++++ .../Data/Input.pdf | Bin 0 -> 3173 bytes ...ove-and-reorder-bookmark-in-the-PDF.csproj | 15 +++ .../Output/gitkeep.txt | 0 .../Program.cs | 107 ++++++++++++++++++ 5 files changed, 147 insertions(+) create mode 100644 Bookmarks/Move-and-reorder-bookmark-in-the-PDF/.NET/Move-and-reorder-bookmark-in-the-PDF.sln create mode 100644 Bookmarks/Move-and-reorder-bookmark-in-the-PDF/.NET/Move-and-reorder-bookmark-in-the-PDF/Data/Input.pdf create mode 100644 Bookmarks/Move-and-reorder-bookmark-in-the-PDF/.NET/Move-and-reorder-bookmark-in-the-PDF/Move-and-reorder-bookmark-in-the-PDF.csproj create mode 100644 Bookmarks/Move-and-reorder-bookmark-in-the-PDF/.NET/Move-and-reorder-bookmark-in-the-PDF/Output/gitkeep.txt create mode 100644 Bookmarks/Move-and-reorder-bookmark-in-the-PDF/.NET/Move-and-reorder-bookmark-in-the-PDF/Program.cs diff --git a/Bookmarks/Move-and-reorder-bookmark-in-the-PDF/.NET/Move-and-reorder-bookmark-in-the-PDF.sln b/Bookmarks/Move-and-reorder-bookmark-in-the-PDF/.NET/Move-and-reorder-bookmark-in-the-PDF.sln new file mode 100644 index 00000000..1b74582b --- /dev/null +++ b/Bookmarks/Move-and-reorder-bookmark-in-the-PDF/.NET/Move-and-reorder-bookmark-in-the-PDF.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.14.36221.1 d17.14 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Move-and-reorder-bookmark-in-the-PDF", "Move-and-reorder-bookmark-in-the-PDF\Move-and-reorder-bookmark-in-the-PDF.csproj", "{2D864F6D-C55F-4995-9871-B322FAFA3B30}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {2D864F6D-C55F-4995-9871-B322FAFA3B30}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2D864F6D-C55F-4995-9871-B322FAFA3B30}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2D864F6D-C55F-4995-9871-B322FAFA3B30}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2D864F6D-C55F-4995-9871-B322FAFA3B30}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {77EDC987-03C1-47D2-AD47-61625B9D80FC} + EndGlobalSection +EndGlobal diff --git a/Bookmarks/Move-and-reorder-bookmark-in-the-PDF/.NET/Move-and-reorder-bookmark-in-the-PDF/Data/Input.pdf b/Bookmarks/Move-and-reorder-bookmark-in-the-PDF/.NET/Move-and-reorder-bookmark-in-the-PDF/Data/Input.pdf new file mode 100644 index 0000000000000000000000000000000000000000..31a7a499e635310414afd31f87bfd59e235c522e GIT binary patch literal 3173 zcmbuCdpwl+9>tS8BuPHlo*%B6cc02kYpG`OcHW8tV^k^T1Lq& zmt4vv_q$pNQSQtlw|0$kXJ@w4*;BjPPUo-Z`Tm~Y^Lahb_x1jMzTY1NZGbueQ$7d+ zLaxRwuY-UvunL%L?+gNJX@P)HOCL8P7^;t_;$6s&>==!ABzl11>^^!SB2zAa)jlj7J`4hafJ9e^KNMl=pJ=1bxloA|Fcjs2rxFc_1hNB>t!Yej zb)-6hVQ>(T^R@?-Ld0JH0lm*!C|C}0b8+wGD*r?A)s9vf8pr1KakJE~K11FD1G*pVln-BTgqH zh)F3yR(d=pOX8~1Ml!E~OVc}Yi$+j2zak}H1-?m+Jyf8A+e0uQ=z#|Y^$VWH<@-DB z&dRvtRHA(TAl3-gaGEKY`qWdS#KI}uEVx}T-3;rL6GEE5GlrPD9OyMW#hqIJz&AJ8 z*AH7uy7VHV+T6b<^#px-VjwW-aSz_XHY#?iuEMWpqwdP0rbJbwY-xlA5Z%LN9EY8dw>(S20^sQ%9@<WM(L8g5k`+-{{Sv#8S=9i~T3ZM9<(Y?dQ2?6rL72h0^|HN1DwJ zE%z?%13N8jc3!6n(x&|jOt_PqTVZXm-r;aO+&8w5|QO-l%HzfOiIUH9cr%24A^OfIsnr&ZY8&7b?A-<%% zjR5(1CGB-x{`V(bCiCBHe`n@o=G<;Ym^7NLj5{r}wM!g!_@1j*1B)_eUzfhLa?PV(CN82d!nO)s7 z-=?)tty+TXAIYuU0AwXoY%1t{R@{qhjud7YDy+L$Rc05@_>Cm>5klvvtjFi> zRECIdMN-Ozy_Diunl4<|%^msED~c>l0P(yoZCpPE=#?N^GSkIRh5t(?nUU!X#9H2S zPsmKo9UX`YXP9}9Gl^l7;RoXjg6j&7%7?bP2_>No!bc!Jux1#c@YtlnqtFYP1l-Vz&F6haGokxshn0 zZ{NI{kEn7ouv?1HmQyg^J-H)&>(xZ7lo3>-qFtQ-_)>hKjWqF;$ooP@W*xl21Q3}? zDpfUF{MB&8rN+!S(8VZ=d->*>lg9e?Dk@z`#TWG+O4F&f^7eu7ylli8 zX_enh9L$)ksv0 z^TT8$m)Y^k9gD^LhwVj4${GfQzf0(9sd%^1IlNzfI}|y?oDMJ@B;8D{<35p{*RVyb zxCj3OPc$N>VW)&mJ3^hZ-noY!E!43|5;(&;o>oQExW literal 0 HcmV?d00001 diff --git a/Bookmarks/Move-and-reorder-bookmark-in-the-PDF/.NET/Move-and-reorder-bookmark-in-the-PDF/Move-and-reorder-bookmark-in-the-PDF.csproj b/Bookmarks/Move-and-reorder-bookmark-in-the-PDF/.NET/Move-and-reorder-bookmark-in-the-PDF/Move-and-reorder-bookmark-in-the-PDF.csproj new file mode 100644 index 00000000..e3245733 --- /dev/null +++ b/Bookmarks/Move-and-reorder-bookmark-in-the-PDF/.NET/Move-and-reorder-bookmark-in-the-PDF/Move-and-reorder-bookmark-in-the-PDF.csproj @@ -0,0 +1,15 @@ + + + + Exe + net8.0 + Move_and_reorder_bookmark_in_the_PDF + enable + enable + + + + + + + diff --git a/Bookmarks/Move-and-reorder-bookmark-in-the-PDF/.NET/Move-and-reorder-bookmark-in-the-PDF/Output/gitkeep.txt b/Bookmarks/Move-and-reorder-bookmark-in-the-PDF/.NET/Move-and-reorder-bookmark-in-the-PDF/Output/gitkeep.txt new file mode 100644 index 00000000..e69de29b diff --git a/Bookmarks/Move-and-reorder-bookmark-in-the-PDF/.NET/Move-and-reorder-bookmark-in-the-PDF/Program.cs b/Bookmarks/Move-and-reorder-bookmark-in-the-PDF/.NET/Move-and-reorder-bookmark-in-the-PDF/Program.cs new file mode 100644 index 00000000..8e88a80d --- /dev/null +++ b/Bookmarks/Move-and-reorder-bookmark-in-the-PDF/.NET/Move-and-reorder-bookmark-in-the-PDF/Program.cs @@ -0,0 +1,107 @@ +using Syncfusion.Drawing; +using Syncfusion.Pdf; +using Syncfusion.Pdf.Interactive; +using Syncfusion.Pdf.Parsing; +class Program +{ + static void Main(string[] args) + { + // Load the PDF document from disk. + using (FileStream docStream = new FileStream(Path.GetFullPath(@"Data/Input.pdf"), FileMode.Open, FileAccess.Read)) + { + // Load the PDF document into memory. + PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream); + + // Access the root bookmark collection of the document. + PdfBookmarkBase bookmarks = loadedDocument.Bookmarks; + + // Move a bookmark from index 2 to index 0 (reorder). + MoveBookmark(bookmarks, 2, 0, loadedDocument); + + // Remove a bookmark by its title (removes the first match found in hierarchy). + RemoveBookmarkByTitle(bookmarks, "Bookmark To Remove"); + + //Create file stream. + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite)) + { + loadedDocument.Save(outputFileStream); + } + + loadedDocument.Close(true); + } + } + + // Moves a bookmark from one index to another within its parent collection. + static void MoveBookmark(PdfBookmarkBase parentCollection, int fromIndex, int toIndex, PdfLoadedDocument document) + { + if (fromIndex == toIndex) return; // No move required if indices are the same. + + // Safely cast the bookmark to be moved. + PdfLoadedBookmark bookmarkToMove = parentCollection[fromIndex] as PdfLoadedBookmark; + if (bookmarkToMove == null) return; + + // Remove the bookmark from its original location. + parentCollection.RemoveAt(fromIndex); + + // Remove any existing bookmark with the same title at the new location to avoid duplicates. + RemoveBookmarkByTitle(parentCollection, bookmarkToMove.Title); + + // Insert the bookmark at the new index. + PdfBookmark newBookmark = parentCollection.Insert(toIndex, bookmarkToMove.Title); + newBookmark.TextStyle = bookmarkToMove.TextStyle; + newBookmark.Color = bookmarkToMove.Color; + + // Set the destination (page location/zoom) for the moved bookmark. + newBookmark.Destination = bookmarkToMove.Destination ?? new PdfDestination(document.Pages[0]) + { + Location = bookmarkToMove.Destination?.Location ?? new PointF(0, 0), + Zoom = bookmarkToMove.Destination?.Zoom ?? 1F + }; + + // Move all child bookmarks recursively. + foreach (PdfBookmark child in bookmarkToMove) + { + AddBookmark(newBookmark, child, document); + } + } + + // Clones an existing bookmark (including all descendants) and adds it to a parent. + static void AddBookmark(PdfBookmark parent, PdfBookmark bookmark, PdfLoadedDocument document) + { + // Remove any even-closer duplicate title children before insertion. + RemoveBookmarkByTitle(parent, bookmark.Title); + + PdfBookmark newChild = parent.Insert(parent.Count, bookmark.Title); + newChild.Destination = bookmark.Destination ?? new PdfDestination(document.Pages[0]) + { + Location = new PointF(0, 0), + Zoom = 1F + }; + newChild.TextStyle = bookmark.TextStyle; + newChild.Color = bookmark.Color; + + // Recursively clone and add children. + foreach (PdfBookmark child in bookmark) + { + AddBookmark(newChild, child, document); + } + } + + // Removes the first occurrence of a bookmark with the specified title from a parent (searches recursively). + static void RemoveBookmarkByTitle(PdfBookmarkBase parent, string title) + { + for (int i = 0; i < parent.Count;) + { + if (parent[i].Title == title) + { + parent.RemoveAt(i); + continue; + } + if (parent[i] is PdfBookmark child) + { + RemoveBookmarkByTitle(child, title); // Recurse for children + } + i++; + } + } +} \ No newline at end of file