我需要在不使用 Acrobat SDK(需要安装完整的 Acrobat 专业版)的情况下将书签添加到 pdf 文档。我正在使用 iTextSharp,但它是为 Java 制作的,并且移植到 .net 还不完整。您知道免费的替代方法或文档吗?
问问题
1478 次
1 回答
2
您声称“ iTextSharp 但它是为 Java 制作的,并且移植到 .net 并不完整”是完全错误的。此处列出了主要区别。
使用 iTextsharp 添加书签很简单。请参阅PdfOutline和PdfDestination的 API 。这是一个简单的示例,可以帮助您入门:
using (Document document = new Document()) {
PdfWriter writer = PdfWriter.GetInstance(
document, Response.OutputStream
);
document.Open();
PdfOutline root = writer.RootOutline;
string section = "Section {0}";
string paragraph = "Paragraph {0}";
for (int i = 0; i < 10;) {
PdfOutline sectionBookmark = new PdfOutline(
root,
new PdfDestination(
PdfDestination.FITH, writer.GetVerticalPosition(true)
),
string.Format(section, ++i)
);
document.Add(new Paragraph(string.Format(section, i)));
for (int j = 0; j < 4;) {
PdfOutline subSectionBookmark = new PdfOutline(
sectionBookmark,
new PdfDestination(
PdfDestination.FITH, writer.GetVerticalPosition(true)
),
string.Format(paragraph, ++j)
);
document.Add(new Paragraph(string.Format(paragraph, j)));
}
document.NewPage();
}
}
以上示例在 5.1.3 的 Web 环境中进行了测试。如果您的开发环境不同,请将Response.OutputStream
上面的替换为您选择的Stream。
于 2011-12-29T12:29:17.673 回答