Reputation: 39
I haven't been able to find answers to my problem. I would like to know if it is possible to update the page link on each item of the TOC.
Please note the question is to update links on TOC, and not bookmarks.
From what I read so far, it is a difficult process that requires at least a week of programming work. Since this is not easily achieved through iTextSharp.
I have first pdf with 5 pages. First page is the TOC.
I have second pdf with 6 pages. First page is the TOC.
I want to merge the two documents.
TOC pages goes on the top of the merged document.
Update each item in the TOC with link reference to the new page. So, when you click on it, it takes you to where it should be in the merged document.
Upvotes: 1
Views: 1087
Reputation: 77528
This is explained in the book "iText in Action - Second Edition." If you don't own a copy of the book, you should take a look at the examples, more specifically at the ConcatenateBookmarks example.
I see that you tagged the question with the C# tag, so instead of copy/pasting the Java example, I'll copy the C# example. The src
is a a List
of byte arrays with PDF documents.
public byte[] ManipulatePdf(List<byte[]> src) {
using (MemoryStream ms = new MemoryStream()) {
// step 1
using (Document document = new Document()) {
// step 2
using (PdfCopy copy = new PdfCopy(document, ms)) {
// step 3
document.Open();
// step 4
int page_offset = 0;
// Create a list for the bookmarks
List<Dictionary<String, Object>> bookmarks =
new List<Dictionary<String, Object>>();
for (int i = 0; i < src.Count; i++) {
PdfReader reader = new PdfReader(src[i]);
// merge the bookmarks
IList<Dictionary<String, Object>> tmp =
SimpleBookmark.GetBookmark(reader);
SimpleBookmark.ShiftPageNumbers(tmp, page_offset, null);
foreach (var d in tmp) bookmarks.Add(d);
// add the pages
int n = reader.NumberOfPages;
page_offset += n;
for (int page = 0; page < n; ) {
copy.AddPage(copy.GetImportedPage(reader, ++page));
}
}
// Add the merged bookmarks
copy.Outlines = bookmarks;
}
}
return ms.ToArray();
}
}
Update:
If the question is about a different type of TOC, then you need to look at the ConcatenateNamedDestinations example.
In this case, we have links from one document to another document using named destinations. In this case, we need to ConsolidateNamedDestinations()
and MakeRemoteNamedDestinationsLocal()
. The C# version of this example can be found here: ConcatenateNamedDestinations.cs
I don't know if this solves your problem. You talk about "link reference update", but that could mean different things (named destinations, explicit destinations, actions).
Upvotes: 1