unsafe List <OutlineIndex> readOutline(fz_outline *ptr) { var col = new List <OutlineIndex>(); while (ptr != null) { var obj = new OutlineIndex() { page = ptr->page, x = ptr->x, y = ptr->y, refs = ptr->refs, title = ReadCStr(ptr->title), uri = ReadCStr(ptr->uri) }; if (ptr->down != null) { obj.down = readOutline(ptr->down); } col.Add(obj); ptr = ptr->next; } return(col); }
public MuPdf(string path) { pages = new List <PageRef>(); const uint FZ_STORE_DEFAULT = 256 << 20; ctx = NativeMethods.NewContext(IntPtr.Zero, IntPtr.Zero, FZ_STORE_DEFAULT, "1.17.0"); // 创建上下文 NativeMethods.fz_register_document_handlers(ctx); stm = NativeMethods.OpenFile(ctx, path); // 打开文件流 doc = NativeMethods.OpenDocumentStream(ctx, ".pdf", stm); // 从文件流创建文档对象 int pn = NativeMethods.CountPages(ctx, doc); // 获取文档的页数 for (int i = 0; i < pn; i++) { // 遍历各页 IntPtr p = NativeMethods.LoadPage(ctx, doc, i); // 加载页面(首页为 0) Rectangle b = NativeMethods.BoundPage(ctx, p); // 获取页面尺寸 int width = (int)(b.Right - b.Left); // 获取页面的宽度和高度 int height = (int)(b.Bottom - b.Top); pages.Add(new PageRef() { ID = i, pagePtr = p, width = width, height = height }); } unsafe { // 在此读取目录 fz_outline *ptr = NativeMethods.fz_load_outline(ctx, doc);; outlines = readOutline(ptr); } BindMetadata(); }