Exemple #1
0
        private static UIImage RenderImage(CGPDFPage page)
        {
            var rect         = page.GetBoxRect(CGPDFBox.Crop);
            var pageRotation = page.RotationAngle;
            var size         = rect.Size;

            UIGraphics.BeginImageContextWithOptions(size, false, (nfloat)(1));

            var context = UIGraphics.GetCurrentContext();

            context.SaveState();
            context.TranslateCTM((nfloat)0.0, size.Height);
            context.ScaleCTM(One, (nfloat)(-1.0));

            context.SetFillColor(One, One);
            context.FillRect(rect);

            var transform = page.GetDrawingTransform(CGPDFBox.Crop, rect, 0, true);

            context.ConcatCTM(transform);
            context.DrawPDFPage(page);

            var image = UIGraphics.GetImageFromCurrentImageContext();

            context.RestoreState();
            UIGraphics.EndImageContext();

            return(image);
        }
        private void draw(CGContext context)
        {
            if (!PDFDocument.DocumentHasLoaded)
            {
                return;
            }

            context.SetFillColor(1.0f, 1.0f, 1.0f, 1.0f);
            using (CGPDFPage pdfPage = PDFDocument.GetPage(_pageNumber)) {
                context.TranslateCTM(0, Bounds.Height);
                context.ScaleCTM(1.0f, -1.0f);
                context.ConcatCTM(pdfPage.GetDrawingTransform(CGPDFBox.Crop, Bounds, 0, true));
                context.SetRenderingIntent(CGColorRenderingIntent.Default);
                context.InterpolationQuality = CGInterpolationQuality.Default;
                context.DrawPDFPage(pdfPage);
            }
        }
Exemple #3
0
        public static UIImage PDF2Image(CGPDFPage page, nfloat width, nfloat scale)
        {
            UIImage img = new UIImage();

            try
            {
                CGRect pageRect = page.GetBoxRect(CGPDFBox.Media);
                nfloat pdfScale = width / pageRect.Size.Width;
                pageRect.Size = new CGSize(pageRect.Size.Width * pdfScale, pageRect.Size.Height * pdfScale);

                UIGraphics.BeginImageContextWithOptions(pageRect.Size, true, scale);
                CGContext context = UIGraphics.GetCurrentContext();

                // White BG
                context.SetFillColor(1.0f, 1.0f, 1.0f, 1f);
                context.FillRect(pageRect);
                context.SaveState();

                //border
                context.SetStrokeColor(0f, 0f, 0f, 0.5f);
                context.StrokeRect(pageRect);

                // Next 3 lines makes the rotations so that the page look in the right direction
                context.TranslateCTM(0.0f, pageRect.Size.Height);
                context.ScaleCTM(1.0f, -1.0f);
                CGAffineTransform transform = page.GetDrawingTransform(CGPDFBox.Media, pageRect, 0, true);
                context.ConcatCTM(transform);

                context.DrawPDFPage(page);
                context.RestoreState();

                img = UIGraphics.GetImageFromCurrentImageContext();

                UIGraphics.EndImageContext();

                context.Dispose();
            }
            catch (Exception ex)
            {
            }
            return(img);
        }
Exemple #4
0
        public override void Draw(CGRect rect)
        {
            base.Draw(rect);

            //flip the CTM so the PDF will be drawn upright
            using (CGContext g = UIGraphics.GetCurrentContext()) {
                g.TranslateCTM(0, Bounds.Height);
                g.ScaleCTM(1, -1);

                // render the first page of the PDF
                using (CGPDFPage pdfPage = pdfDoc.GetPage(1)) {
                    //get the affine transform that defines where the PDF is drawn
                    CGAffineTransform t = pdfPage.GetDrawingTransform(CGPDFBox.Crop, rect, 0, true);
                    //concatenate the pdf transform with the CTM for display in the view
                    g.ConcatCTM(t);
                    //draw the pdf page
                    g.DrawPDFPage(pdfPage);
                }
            }
        }
Exemple #5
0
        internal void ConvertPagetoImage(int index)
        {
            if (this.PdfDocument == null || this.Element.ImageStreams.ContainsKey(index))
            {
                return;
            }

            float   scaleFactor = 1;
            UIImage image       = null;

            using (CGPDFPage pdfPage = this.PdfDocument.GetPage(index + 1))
            {
                if (pdfPage != null)
                {
                    CGRect rect   = pdfPage.GetBoxRect(CGPDFBox.Media);
                    nfloat factor = (nfloat)scaleFactor;
                    CGRect bounds = new CGRect(rect.X * factor, rect.Y * factor, rect.Width * factor, rect.Height * factor);
                    UIGraphics.BeginImageContext(bounds.Size);
                    CGContext context = UIGraphics.GetCurrentContext();
                    context.SetFillColor(1.0f, 1.0f, 1.0f, 1.0f);
                    context.FillRect(bounds);
                    context.TranslateCTM(0, bounds.Height);
                    context.ScaleCTM(factor, -factor);
                    context.ConcatCTM(pdfPage.GetDrawingTransform(CGPDFBox.Crop, rect, 0, true));
                    context.SetRenderingIntent(CGColorRenderingIntent.Default);
                    context.InterpolationQuality = CGInterpolationQuality.Default;
                    context.DrawPDFPage(pdfPage);
                    image = UIGraphics.GetImageFromCurrentImageContext();
                    UIGraphics.EndImageContext();
                }
            }

            if (image != null)
            {
                this.Element.ImageStreams.Add(index, image);
                this.Element.PDFViewModel.Items[index].PageData = image;
            }
        }
Exemple #6
0
        public override void Draw(RectangleF rect)
        {
            base.Draw(rect);

            CGContext gctx = UIGraphics.GetCurrentContext();

            gctx.TranslateCTM(0, Bounds.Height);
            gctx.ScaleCTM(1, -1);

            using (CGPDFPage pdfPg = _pdf.GetPage(PageNumber)) {
                gctx.SaveState();

                RectangleF        r  = new RectangleF(Bounds.Left, Bounds.Top, Bounds.Width, Bounds.Height + 44f);
                CGAffineTransform tf = pdfPg.GetDrawingTransform(CGPDFBox.Crop, r, 0, true);
                gctx.ConcatCTM(tf);
                gctx.DrawPDFPage(pdfPg);

                gctx.RestoreState();
                gctx.TranslateCTM(0, Bounds.Height - 25);
                gctx.SelectFont("Helvetica", 25f, CGTextEncoding.MacRoman);
                gctx.ShowText(AnnotatedText);
            }
        }
Exemple #7
0
    public override void DrawInContext(CGContext context)
    {
        // PDF page drawing expects a Lower-Left coordinate system, so we flip the coordinate system
        // before we start drawing.
        context.TranslateCTM(0, Bounds.Height);
        context.ScaleCTM(1, -1);

        // Grab the first PDF page
        using (CGPDFPage page = doc.GetPage(1)) {
            // We're about to modify the context CTM to draw the PDF page where we want it, so save the graphics state in case we want to do more drawing
            context.SaveState();

            // CGPDFPageGetDrawingTransform provides an easy way to get the transform for a PDF page. It will scale down to fit, including any
            // base rotations necessary to display the PDF page correctly.

            CGAffineTransform pdfTransform = page.GetDrawingTransform(CGPDFBox.Crop, Bounds, 0, true);

            // And apply the transform.
            context.ConcatCTM(pdfTransform);
            // Finally, we draw the page and restore the graphics state for further manipulations!
            context.DrawPDFPage(page);
            context.RestoreState();
        }
    }
Exemple #8
0
        private List <UIImage> ConvertToImages( )
        {
            Images = new List <UIImage>();

            if (Doc == null)
            {
                throw new Exception("Could not load document");
            }

            for (Int32 i = 1; i <= Count; i++)
            {
                using (CGPDFPage Page = Doc.GetPage(i))
                {
                    CGRect PageRect = Page.GetBoxRect(CGPDFBox.Media);
                    //nfloat Scale = 1;//View.Frame.Height / PageRect.Height;
                    //PageRect.Size = new CGSize(PageRect.Height * Scale, PageRect.Width * Scale);

                    if (PageRect.Height > PageRect.Width)
                    {
                        PageRect.Size = new CGSize(PageRect.Height, PageRect.Width);
                    }

                    CGRect MediaBox = Page.GetBoxRect(CGPDFBox.Media);
                    CGRect CropBox  = Page.GetBoxRect(CGPDFBox.Crop);

                    nfloat TopMargin    = CropBox.GetMinY() - MediaBox.GetMinY();
                    nfloat BottomMargin = MediaBox.GetMaxY() - CropBox.GetMaxY();
                    nfloat LeftMargin   = CropBox.GetMinX() - MediaBox.GetMinX();
                    nfloat RightMargin  = MediaBox.GetMaxX() - CropBox.GetMaxX();

                    if (TopMargin + BottomMargin + LeftMargin + RightMargin > 0)
                    {
                        PageRect = new CGRect(
                            PageRect.Location,
                            new CGSize(
                                PageRect.Size.Width - (LeftMargin + RightMargin),
                                PageRect.Size.Height - (TopMargin + BottomMargin)
                                )
                            );
                    }

                    UIGraphics.BeginImageContext(PageRect.Size);

                    using (CGContext context = UIGraphics.GetCurrentContext())
                    {
                        context.SaveState();

                        context.TranslateCTM(0, PageRect.Size.Height);
                        context.ScaleCTM(1, -1);

                        context.ConcatCTM(
                            Page.GetDrawingTransform(CGPDFBox.Crop, PageRect, 0, true)
                            );

                        context.DrawPDFPage(Page);
                        context.RestoreState();

                        Images.Add(UIGraphics.GetImageFromCurrentImageContext());
                    }

                    UIGraphics.EndImageContext();
                }
            }
            return(Images);
        }
		private static UIImage RenderImage (CGPDFPage page)
		{

			var rect = page.GetBoxRect (CGPDFBox.Crop);
			var pageRotation = page.RotationAngle;
			var size = rect.Size;

			UIGraphics.BeginImageContextWithOptions (size, false, (nfloat)(1));

			var context = UIGraphics.GetCurrentContext ();
			context.SaveState ();
			context.TranslateCTM ((nfloat)0.0, size.Height);
			context.ScaleCTM (One, (nfloat)(-1.0));

			context.SetFillColor (One, One);
			context.FillRect (rect);

			var transform = page.GetDrawingTransform (CGPDFBox.Crop, rect, 0, true);
			context.ConcatCTM (transform);
			context.DrawPDFPage (page);

			var image = UIGraphics.GetImageFromCurrentImageContext ();
			context.RestoreState ();
			UIGraphics.EndImageContext ();

			return image;
		}