gpt4 book ai didi

c# - 合并带有书签的 pdf 文件

转载 作者:太空狗 更新时间:2023-10-29 22:14:28 26 4
gpt4 key购买 nike

我正在尝试合并很多 pdf,并且我想为每个 pdf 添加一个书签(pdf 的名称),我发现了合并 pdf 的不同技术,但它们都不能只添加书签,例如。 itextsharp 添加一章,然后是该章的书签,我不想更改 pdf。

最佳答案

使用 itextsharp 你可以做到。我通过以下方法来完成:

MergePdfFiles(string outputPdf, string[] sourcePdfs) {
PdfReader reader = null;
Document document = new Document();
PdfImportedPage page = null;
PdfCopy pdfCpy = null;
int n = 0;
int totalPages = 0;
int page_offset = 0;
List < Dictionary < string, object >> bookmarks = new List < Dictionary < string, object >> ();
IList < Dictionary < string, object >> tempBookmarks;
for (int i = 0; i <= sourcePdfs.GetUpperBound(0); i++) {
reader = new PdfReader(sourcePdfs[i]);
reader.ConsolidateNamedDestinations();
n = reader.NumberOfPages;
tempBookmarks = SimpleBookmark.GetBookmark(reader);
if (i == 0) {
document = new iTextSharp.text.Document(reader.GetPageSizeWithRotation(1));
pdfCpy = new PdfCopy(document, new FileStream(outputPdf, FileMode.Create));
document.Open();
SimpleBookmark.ShiftPageNumbers(tempBookmarks, page_offset, null);
page_offset += n;
if (tempBookmarks != null)
bookmarks.AddRange(tempBookmarks);
// MessageBox.Show(n.ToString());
totalPages = n;
} else {
SimpleBookmark.ShiftPageNumbers(tempBookmarks, page_offset, null);
if (tempBookmarks != null)
bookmarks.AddRange(tempBookmarks);
page_offset += n;
totalPages += n;
}
for (int j = 1; j <= n; j++) {
page = pdfCpy.GetImportedPage(reader, j);
pdfCpy.AddPage(page);
}
reader.Close();
}
pdfCpy.Outlines = bookmarks;
document.Close();
}

关于c# - 合并带有书签的 pdf 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10187483/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com