在Itext7中合并时如何为PDF添加书签
How to add Bookmarks to PDF when Merging in Itext7
我正在尝试创建一个合并多个 pdf 并为每个 pdf 添加书签的程序。
我的启动方法是:
private Byte[] MergePdfForms(Dictionary<string, Stream> files)
{
var dest = new MemoryStream();
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
PdfMerger merger = new PdfMerger(pdf);
foreach (var keyValuePair in files)
{
//TODO add bookmark to this page
PdfDocument firstSourcePdf = new PdfDocument(new PdfReader(keyValuePair.Value));
merger.Merge(firstSourcePdf, 1, firstSourcePdf.GetNumberOfPages());
firstSourcePdf.Close();
}
pdf.Close();
return dest.ToArray();
}
我试过
pdf.InitializeOutlines();
pdf.AddNamedDestination("Test Bookmark", pdf.GetLastPage().GetPdfObject());
这不会创建任何书签。
看看这里的例子:http://developers.itextpdf.com/examples/merging-pdf-documents/clone-merging-documents-bookmarks
private byte[] MergePdfForms(Dictionary<string, Stream> files)
{
var dest = new MemoryStream();
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
PdfMerger merger = new PdfMerger(pdf);
PdfOutline rootOutline = pdf.GetOutlines(false);
PdfOutline helloWorld = rootOutline.AddOutline("Root");
int pages = 1;
foreach (var keyValuePair in files)
{
var firstSourcePdf = new PdfDocument(new PdfReader(keyValuePair.Value));
var subPages = firstSourcePdf.GetNumberOfPages();
merger.Merge(firstSourcePdf, 1, subPages);
firstSourcePdf.Close();
var link1 = helloWorld.AddOutline(keyValuePair.Key);
link1.AddDestination(PdfExplicitDestination.CreateFit(pdf.GetPage(pages)));
pages += subPages;
}
pdf.Close();
return dest.ToArray();
}
我正在尝试创建一个合并多个 pdf 并为每个 pdf 添加书签的程序。
我的启动方法是:
private Byte[] MergePdfForms(Dictionary<string, Stream> files)
{
var dest = new MemoryStream();
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
PdfMerger merger = new PdfMerger(pdf);
foreach (var keyValuePair in files)
{
//TODO add bookmark to this page
PdfDocument firstSourcePdf = new PdfDocument(new PdfReader(keyValuePair.Value));
merger.Merge(firstSourcePdf, 1, firstSourcePdf.GetNumberOfPages());
firstSourcePdf.Close();
}
pdf.Close();
return dest.ToArray();
}
我试过
pdf.InitializeOutlines();
pdf.AddNamedDestination("Test Bookmark", pdf.GetLastPage().GetPdfObject());
这不会创建任何书签。
看看这里的例子:http://developers.itextpdf.com/examples/merging-pdf-documents/clone-merging-documents-bookmarks
private byte[] MergePdfForms(Dictionary<string, Stream> files)
{
var dest = new MemoryStream();
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
PdfMerger merger = new PdfMerger(pdf);
PdfOutline rootOutline = pdf.GetOutlines(false);
PdfOutline helloWorld = rootOutline.AddOutline("Root");
int pages = 1;
foreach (var keyValuePair in files)
{
var firstSourcePdf = new PdfDocument(new PdfReader(keyValuePair.Value));
var subPages = firstSourcePdf.GetNumberOfPages();
merger.Merge(firstSourcePdf, 1, subPages);
firstSourcePdf.Close();
var link1 = helloWorld.AddOutline(keyValuePair.Key);
link1.AddDestination(PdfExplicitDestination.CreateFit(pdf.GetPage(pages)));
pages += subPages;
}
pdf.Close();
return dest.ToArray();
}