예제 #1
0
        public PdfPageLinks GetPageLinks(int pageNumber, Size pageSize)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(GetType().Name);
            }

            var links = new List <PdfPageLink>();

            using (var pageData = new PageData(_document, _form, pageNumber))
            {
                int    link = 0;
                IntPtr annotation;

                while (NativePdfiumMethods.FPDFLink_Enumerate(pageData.Page, ref link, out annotation))
                {
                    var    destination = NativePdfiumMethods.FPDFLink_GetDest(_document, annotation);
                    int?   target      = null;
                    string uri         = null;

                    if (destination != IntPtr.Zero)
                    {
                        target = (int)NativePdfiumMethods.FPDFDest_GetPageIndex(_document, destination);
                    }

                    var action = NativePdfiumMethods.FPDFLink_GetAction(annotation);
                    if (action != IntPtr.Zero)
                    {
                        const uint length = 1024;
                        var        sb     = new StringBuilder(1024);
                        NativePdfiumMethods.FPDFAction_GetURIPath(_document, action, sb, length);

                        uri = sb.ToString();
                    }

                    var rect = new NativePdfiumMethods.FS_RECTF();

                    if (NativePdfiumMethods.FPDFLink_GetAnnotRect(annotation, rect) && (target.HasValue || uri != null))
                    {
                        int deviceX1;
                        int deviceY1;
                        int deviceX2;
                        int deviceY2;

                        NativePdfiumMethods.FPDF_PageToDevice(
                            pageData.Page,
                            0,
                            0,
                            pageSize.Width,
                            pageSize.Height,
                            0,
                            rect.left,
                            rect.top,
                            out deviceX1,
                            out deviceY1
                            );

                        NativePdfiumMethods.FPDF_PageToDevice(
                            pageData.Page,
                            0,
                            0,
                            pageSize.Width,
                            pageSize.Height,
                            0,
                            rect.right,
                            rect.bottom,
                            out deviceX2,
                            out deviceY2
                            );

                        links.Add(new PdfPageLink(
                                      new Rectangle(deviceX1, deviceY1, deviceX2 - deviceX1, deviceY2 - deviceY1),
                                      target,
                                      uri
                                      ));
                    }
                }
            }

            return(new PdfPageLinks(links));
        }