PDFsharp & MigraDoc Forum

PDFsharp - A .NET library for processing PDF & MigraDoc - Creating documents on the fly
It is currently Fri May 16, 2025 6:24 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules

Also see our new Tailored Support & Services site.



Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Mon Nov 22, 2021 3:18 pm 
Offline

Joined: Mon Feb 24, 2020 9:35 am
Posts: 4
When I use PdfSharp to combine two PDF files and then save it to a different file, the internal hyperlinks become inactive.
Clicking on a hyperlink, no longer it works.

Any suggestions?

Thank you.
Regards
JM


Top
 Profile  
Reply with quote  
PostPosted: Mon Nov 22, 2021 8:12 pm 
Offline
PDFsharp Guru
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 1044
Location: CCAA
jmarati wrote:
Any suggestions?
The destination page of the hyperlinks must be updated when combining the documents.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Wed Mar 19, 2025 4:59 pm 
Offline

Joined: Wed Mar 19, 2025 4:55 pm
Posts: 1
Can anyone please show how "The destination page of the hyperlinks can be updated when combining the documents" as the previous post says ?


Top
 Profile  
Reply with quote  
PostPosted: Thu May 15, 2025 6:17 am 
Offline

Joined: Thu May 15, 2025 6:14 am
Posts: 2
I would very much like to know this, too


Top
 Profile  
Reply with quote  
PostPosted: Fri May 16, 2025 6:12 am 
Offline

Joined: Thu May 15, 2025 6:14 am
Posts: 2
I got it to work. Links don't jump to the exact location but to the top of the page, though.

This is the method that I call after I imported all pages from PDF2 to PDF1 (I simplified it a little).
I am quite new to C# and PDFsharp. I am sure this can be done more elegantly.

doc2_ is the second PDF. The one that is appended to PDF1.
pp_ is an object of a class that I wrote. It's basically just two PdfPage lists. When importing the pages from PDF2 into PDF1 I save here the original page in PDF2 and the newly imported page in PDF1. That way, when looking at pages in PDF2 I always know which is the corresponding page in PDF1. If someone needs the code just ask.
iPageOffset_ is the number of pages that were in PDF1 before anything was imported from PDF2.
Code:
private void FixLinks(PdfDocument doc2_, PagePairs pp_, int iPageOffset_)
{
  int iPageCount = pp_.GetSize();
  for (int j = 0; j < iPageCount; j++)
  {
    PdfPage? page1 = null;
    PdfPage? page2 = null;
    pp_.GetPagePair(j, out page1, out page2);
    if ((page1 == null) || (page2 == null))
      return;

    List<PdfAnnotation> la2Remove = new();
    List<PdfAnnotation> la2Add = new();
    // Go through the annotations of the original page in PDF2
    for (int i = 0; i < page2.Annotations.Count; i++)
    {
      PdfAnnotation annot = page2.Annotations[i];
      if (annot.Elements.GetString(PdfAnnotation.Keys.Subtype) != "/Link")
        continue; // Not a link

      PdfArray? dest = annot.Elements.GetArray("/Dest");
      if (dest == null)
        continue; // Link without destination?

      PdfRectangle rect = annot.Elements.GetRectangle(PdfAnnotation.Keys.Rect);
      int i0basePageIndex2 = -1;
      PdfReference? targetPage = dest.Elements.GetReference(0);
      if (targetPage == null)
      { // This is a document link that points to a specific page with a 0-based index
        try
        {
          i0basePageIndex2 = dest.Elements.GetInteger(0);
        }
        catch (InvalidCastException)
        {
          continue;
        }
      }
      else
      { // This is a link that points to a specific location on a page.
        // Try to find the referenced page in PDF2
        for (int p = 0; p < doc2_.PageCount; p++)
        {
          if (doc2_.Pages[p].Reference == targetPage)
          { // This is the page that was referenced in PDF2. Remember the 0-based index
            i0basePageIndex2 = p;
            break;
          }
        }
      }
      if (i0basePageIndex2 > -1)
      { // Remember which annotation that was imported into PDF1 to remove
        la2Remove.Add(page1.Annotations[i]);
        // Then create a new annotation to add later
        la2Add.Add(PdfLinkAnnotation.CreateDocumentLink(rect, i0basePageIndex2 + 1 + iPageOffset_));
      }
    }
    // Remove the annotation to replace
    foreach (PdfAnnotation a in la2Remove)
      page1.Annotations.Remove(a);

    // Then add the new one
    foreach (PdfAnnotation a in la2Add)
      page1.Annotations.Add(a);
  }
}


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 66 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group