Ejemplo n.º 1
0
        //產生 PDF 元產所需的參數 (改用Thread的方式ren)
        private PXCV_Lib36.PXV_CommonRenderParameters prepareCommonRenderParameter(PDFDoc pdfDoc, int dpi, int pageNumber, float zoom, int offsetX, int offsetY, Border border, bool isSinglePage)
        {
            IntPtr p1 = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(PXCV_Helper.RECT)));
            IntPtr p2 = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(PXCV_Helper.RECT)));

            System.Drawing.Point m_Offset   = new System.Drawing.Point(offsetX, offsetY);
            System.Drawing.Size  aPageSize  = System.Drawing.Size.Empty;
            PXCV_Helper.RECT     aWholePage = new PXCV_Helper.RECT();
            PXCV_Helper.RECT     aDrawRect  = new PXCV_Helper.RECT();
            PXCV_Lib36.PXV_CommonRenderParameters commonRenderParam = new PXCV_Lib36.PXV_CommonRenderParameters();
            PageDimension aPageDim;

            pdfDoc.GetPageDimensions(pageNumber, out aPageDim.w, out aPageDim.h);

            //Border bd = border; //可視範圍
            double borderHeight = (border.ActualHeight / (double)96) * dpi;
            double borderWidth  = (border.ActualWidth / (double)96) * dpi;

            if (zoomStep == 0)
            {
                //PDF原尺吋
                aPageSize.Width  = (int)((aPageDim.w / 72.0 * dpi) * zoom);
                aPageSize.Height = (int)((aPageDim.h / 72.0 * dpi) * zoom);

                double borderRatio      = borderWidth / borderHeight;
                double renderImageRatio = 0;

                if (isSinglePage)
                {
                    renderImageRatio = (double)aPageSize.Width / (double)aPageSize.Height;
                }
                else
                {
                    renderImageRatio = (double)(aPageSize.Width * 2) / (double)aPageSize.Height;
                }

                if (aPageSize.Width < borderWidth && aPageSize.Height < borderHeight)
                {   //PDF原尺吋就比canvas還小 --> 貼齊canvas
                    double newPageW, newPageH;
                    if (renderImageRatio > borderRatio)
                    {   //寬先頂到
                        newPageW  = borderWidth / 2;
                        baseScale = newPageW / (double)aPageSize.Width;
                        newPageH  = Math.Round(baseScale * (double)aPageSize.Height, 2);
                    }
                    else
                    {   //高先頂到
                        newPageH  = borderHeight;
                        baseScale = newPageH / (double)aPageSize.Height;
                        newPageW  = Math.Round(baseScale * (double)aPageSize.Width, 2);
                    }

                    aPageSize.Width  = (int)newPageW;
                    aPageSize.Height = (int)newPageH;
                }
                else
                {   //PDF有一邊比canvas還大
                    double newPageW, newPageH;
                    if (renderImageRatio > borderRatio)
                    {   //寬先頂到
                        newPageW  = borderWidth / 2;
                        baseScale = newPageW / (double)aPageSize.Width;
                        newPageH  = Math.Round(baseScale * (double)aPageSize.Height, 2);
                    }
                    else
                    {   //高先頂到
                        newPageH  = borderHeight;
                        baseScale = newPageH / (double)aPageSize.Height;
                        newPageW  = Math.Round(baseScale * (double)aPageSize.Width, 2);
                    }

                    aPageSize.Width  = (int)newPageW;
                    aPageSize.Height = (int)newPageH;
                }
            }
            else
            {
                //PDF原尺吋
                aPageSize.Width  = (int)((aPageDim.w / 72.0 * dpi) * zoom * baseScale);
                aPageSize.Height = (int)((aPageDim.h / 72.0 * dpi) * zoom * baseScale);
            }

            //Region rgn1 = new Region(new System.Drawing.Rectangle(-m_Offset.X, -m_Offset.Y, aPageSize.Width, aPageSize.Height));
            //rgn1.Complement(new System.Drawing.Rectangle(0, 0, (int)borderWidth, (int)borderHeight));
            //rgn1.Complement(new System.Drawing.Rectangle(0, 0, aPageSize.Width, aPageSize.Height));
            aWholePage.left   = -m_Offset.X;
            aWholePage.top    = -m_Offset.Y;
            aWholePage.right  = aWholePage.left + aPageSize.Width;
            aWholePage.bottom = aWholePage.top + aPageSize.Height;

            //計算要ren的範圍
            //TODO: 改成部分ren,目前是全ren
            aDrawRect.left = 0;
            aDrawRect.top  = 0;
            if (zoomStep == 0)
            {
                if (aPageSize.Width < borderWidth)
                {
                    aDrawRect.right = aPageSize.Width;
                }
                else
                {
                    aDrawRect.right = (int)borderWidth;
                }
                if (aPageSize.Height < borderHeight)
                {
                    aDrawRect.bottom = aPageSize.Height;
                }
                else
                {
                    aDrawRect.bottom = (int)borderHeight;
                }
            }
            else
            {
                aDrawRect.right  = aPageSize.Width;
                aDrawRect.bottom = aPageSize.Height;
            }

            //aDrawRect.right = aPageSize.Width;
            //aDrawRect.bottom = aPageSize.Height;
            Marshal.StructureToPtr(aWholePage, p1, false);
            Marshal.StructureToPtr(aDrawRect, p2, false);
            commonRenderParam.WholePageRect = p1;
            commonRenderParam.DrawRect      = p2;
            commonRenderParam.RenderTarget  = PXCV_Lib36.PXCV_RenderMode.pxvrm_Viewing;
            commonRenderParam.Flags         = 0;
            //System.Drawing.Rectangle rc = new System.Drawing.Rectangle(0, 0, aControlSize.Width, aControlSize.Height);
            //System.Drawing.Rectangle rc = new System.Drawing.Rectangle(0, 0, aPageSize.Width, aPageSize.Height);
            //rc.Intersect(new System.Drawing.Rectangle(-m_Offset.X, -m_Offset.Y, aPageSize.Width, aPageSize.Height));
            //e.DrawRectangle(System.Windows.Media.Brushes.White, null, new Rect(new System.Windows.Size(rc.Width, rc.Height)));
            //aGraphics.FillRectangle(System.Drawing.Brushes.White, rc);
            //aGraphics.FillRegion(System.Drawing.Brushes.Gray, rgn1);
            //rgn1.Dispose();



            return(commonRenderParam);
        }
        private PXCV_Lib36.PXV_CommonRenderParameters prepareCommonRenderParameter(PDFDoc pdfDoc, int dpi, int pageNumber, float zoom, int offsetX, int offsetY, Border border, bool isSinglePage)
        {
            IntPtr intPtr  = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(PXCV_Helper.RECT)));
            IntPtr intPtr2 = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(PXCV_Helper.RECT)));
            Point  point   = new Point(offsetX, offsetY);
            Size   empty   = Size.Empty;

            PXCV_Helper.RECT rECT  = default(PXCV_Helper.RECT);
            PXCV_Helper.RECT rECT2 = default(PXCV_Helper.RECT);
            PXCV_Lib36.PXV_CommonRenderParameters result = default(PXCV_Lib36.PXV_CommonRenderParameters);
            PageDimension pageDimension = default(PageDimension);

            pdfDoc.GetPageDimensions(pageNumber, out pageDimension.w, out pageDimension.h);
            double num  = border.ActualHeight / 96.0 * (double)dpi;
            double num2 = border.ActualWidth / 96.0 * (double)dpi;

            if (zoomStep == 0)
            {
                empty.Width  = (int)(pageDimension.w / 72.0 * (double)dpi * (double)zoom);
                empty.Height = (int)(pageDimension.h / 72.0 * (double)dpi * (double)zoom);
                double num3 = num2 / num;
                double num4 = 0.0;
                num4 = ((!isSinglePage) ? ((double)(empty.Width * 2) / (double)empty.Height) : ((double)empty.Width / (double)empty.Height));
                if ((double)empty.Width < num2 && (double)empty.Height < num)
                {
                    double num5;
                    double num6;
                    if (num4 > num3)
                    {
                        num5      = num2 / 2.0;
                        baseScale = num5 / (double)empty.Width;
                        num6      = Math.Round(baseScale * (double)empty.Height, 2);
                    }
                    else
                    {
                        num6      = num;
                        baseScale = num6 / (double)empty.Height;
                        num5      = Math.Round(baseScale * (double)empty.Width, 2);
                    }
                    empty.Width  = (int)num5;
                    empty.Height = (int)num6;
                }
                else
                {
                    double num7;
                    double num8;
                    if (num4 > num3)
                    {
                        num7      = num2 / 2.0;
                        baseScale = num7 / (double)empty.Width;
                        num8      = Math.Round(baseScale * (double)empty.Height, 2);
                    }
                    else
                    {
                        num8      = num;
                        baseScale = num8 / (double)empty.Height;
                        num7      = Math.Round(baseScale * (double)empty.Width, 2);
                    }
                    empty.Width  = (int)num7;
                    empty.Height = (int)num8;
                }
            }
            else
            {
                empty.Width  = (int)(pageDimension.w / 72.0 * (double)dpi * (double)zoom * baseScale);
                empty.Height = (int)(pageDimension.h / 72.0 * (double)dpi * (double)zoom * baseScale);
            }
            rECT.left   = -point.X;
            rECT.top    = -point.Y;
            rECT.right  = rECT.left + empty.Width;
            rECT.bottom = rECT.top + empty.Height;
            rECT2.left  = 0;
            rECT2.top   = 0;
            if (zoomStep == 0)
            {
                if ((double)empty.Width < num2)
                {
                    rECT2.right = empty.Width;
                }
                else
                {
                    rECT2.right = (int)num2;
                }
                if ((double)empty.Height < num)
                {
                    rECT2.bottom = empty.Height;
                }
                else
                {
                    rECT2.bottom = (int)num;
                }
            }
            else
            {
                rECT2.right  = empty.Width;
                rECT2.bottom = empty.Height;
            }
            Marshal.StructureToPtr((object)rECT, intPtr, false);
            Marshal.StructureToPtr((object)rECT2, intPtr2, false);
            result.WholePageRect = intPtr;
            result.DrawRect      = intPtr2;
            result.RenderTarget  = PXCV_Lib36.PXCV_RenderMode.pxvrm_Viewing;
            result.Flags         = 0;
            return(result);
        }