使用 iText 和 C#,在与每个 PDF 书签完全相同的页面坐标处创建命名目标
我可以迭代我的PDF文件中的所有书签并创建名称的目的地,但位于每个书签的顶部,而不是在同一坐标上。
下面的代码有效,但是我如何将命名目的地设置为与书签完全相同的页面坐标(每个页面可以有几本书签):
string file = @"C:\MyDocs\MyDoc1.pdf";
string newFile = @"C:\MyDocs\MyDoc2.pdf";
PdfDocument pdf = new PdfDocument(new PdfReader(file), new PdfWriter(newFile));
// Get the bookmarks
PdfOutline outlines = pdf.GetOutlines(false);
List<PdfOutline> bookmarks = outlines.GetAllChildren().ToList<PdfOutline>();
PdfNameTree destsTree = pdf.GetCatalog().GetNameTree(PdfName.Dests);
IDictionary<String, PdfObject> Tnames = destsTree.GetNames();
foreach (var item in bookmarks)
{
string title = item.GetTitle();
int pgn = pdf.GetPageNumber((PdfDictionary)item.GetDestination().GetDestinationPage(Tnames));
PdfPage pdfPage = pdf.GetPage(pgn);
iText.Kernel.Geom.Rectangle pageRect = pdfPage.GetPageSize();
float getLeft = pageRect.GetLeft();
float getTop = pageRect.GetTop();
PdfExplicitDestination destObj = PdfExplicitDestination.CreateXYZ(pdfPage, getLeft, getTop, 1);
pdf.AddNamedDestination(title, destObj.GetPdfObject());
}
pdf.Close();
I can iterate through all the bookmarks in my PDF file and create named destinations but positioned at the top of the same page as each bookmark and not at the same coordinates.
The code below works but how do I set the named destination to also be at the exact same page coordinates as the bookmark (there can be a few bookmarks per page):
string file = @"C:\MyDocs\MyDoc1.pdf";
string newFile = @"C:\MyDocs\MyDoc2.pdf";
PdfDocument pdf = new PdfDocument(new PdfReader(file), new PdfWriter(newFile));
// Get the bookmarks
PdfOutline outlines = pdf.GetOutlines(false);
List<PdfOutline> bookmarks = outlines.GetAllChildren().ToList<PdfOutline>();
PdfNameTree destsTree = pdf.GetCatalog().GetNameTree(PdfName.Dests);
IDictionary<String, PdfObject> Tnames = destsTree.GetNames();
foreach (var item in bookmarks)
{
string title = item.GetTitle();
int pgn = pdf.GetPageNumber((PdfDictionary)item.GetDestination().GetDestinationPage(Tnames));
PdfPage pdfPage = pdf.GetPage(pgn);
iText.Kernel.Geom.Rectangle pageRect = pdfPage.GetPageSize();
float getLeft = pageRect.GetLeft();
float getTop = pageRect.GetTop();
PdfExplicitDestination destObj = PdfExplicitDestination.CreateXYZ(pdfPage, getLeft, getTop, 1);
pdf.AddNamedDestination(title, destObj.GetPdfObject());
}
pdf.Close();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论