Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Mon Aug 21, 2023 3:51 pm

I need to merge multiple PDFs from various sources into a single PDF, and bookmark each source.
The Bookmarks are created, but do not navigate to anything when clicked.

Spire.pdf 9.8.5, .Net 6.0

Code: Select all
        private void test()
        {
            PdfDocument doc = new PdfDocument();

            var fileNames = new List<string>()
            {
                @"c:\pdf\file1.pdf",
                @"c:\pdf\file2.pdf"
            };

            foreach (var fileName in fileNames)
            {
                var pdf = new PdfDocument(File.ReadAllBytes(fileName));
                var dest = new PdfDestination(pdf.Pages[0], PointF.Empty);
                var bookmark = doc.Bookmarks.Add(fileName);
                bookmark.Action = new PdfGoToAction(dest);
                doc.AppendPage(pdf);
            }

            doc.SaveToFile(@"C:\pdf\demo.pdf", FileFormat.PDF);
        }

SeanAtPrime
 
Posts: 1
Joined: Thu Nov 12, 2020 11:43 pm

Tue Aug 22, 2023 2:14 am

Hi,

Thanks for your feedback.
For your requirement, please see the following code for reference.
Code: Select all
PdfDocument doc = new PdfDocument();

var fileNames = new List<string>()
{
    @"C:\pdf\file1.pdf",
    @"C:\pdf\file2.pdf"
};

// record the page index
int pageIndex = 0;

foreach (var fileName in fileNames)
{
    var pdf = new PdfDocument(File.ReadAllBytes(fileName));
    doc.AppendPage(pdf);
    var dest = new PdfDestination(doc.Pages[pageIndex], PointF.Empty);
    var bookmark = doc.Bookmarks.Add(fileName);
    bookmark.Destination = dest;
    pageIndex += doc.Pages.Count;
}

doc.SaveToFile(@"demo.pdf", FileFormat.PDF);



If the code does not meet your requirement or you have further questions, just feel free to contact us. We are here to assist you.

Best regards,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Return to Spire.PDF

cron