Exemplo n.º 1
0
        protected override void AddPageImpl(float width, float height)
        {
            if (_closed)
            {
                throw new Exception("Unable to add a page because the PDFContext is already closed.");
            }

            if (_data == null)
            {
                _data = new NSMutableData();
                var consumer = new CGDataConsumer(_data);
                _context = new CGContextPDF(consumer, CGRect.Empty, null);
                _context.SetFillColorSpace(CGColorSpace.CreateDeviceRGB());
                _context.SetStrokeColorSpace(CGColorSpace.CreateDeviceRGB());
            }

            if (_pageOpen)
            {
                _context.EndPage();
            }

            _context.BeginPage(new CGRect(0, 0, width, height));
            _context.TranslateCTM(0, height);
            _context.ScaleCTM(1, -1);
            _context.SetLineWidth(1);
            _context.SetFillColor(new CGColor(1, 1));
            _context.SetStrokeColor(new CGColor(0, 1));

            _pageOpen = true;

            _canvas.Context = _context;
        }
Exemplo n.º 2
0
 public void Create_DataConsumer_BadUTI()
 {
     using (NSMutableData destData = new NSMutableData())
         using (var consumer = new CGDataConsumer(destData)) {
             Assert.Null(CGImageDestination.Create(consumer, BadUti, 1), "Create-1");
             Assert.Null(CGImageDestination.Create(consumer, BadUti, 1, new CGImageDestinationOptions()), "Create-2");
         }
 }
Exemplo n.º 3
0
        private static void Save(CGImage screenshot, string path)
        {
            var fileURL = new NSUrl(path, false);

            using (var dataConsumer = new CGDataConsumer(fileURL))
            {
                var imageDestination = CGImageDestination.Create(dataConsumer, imageFormat, 1);
                imageDestination.AddImage(screenshot);
                imageDestination.Close();
            }
        }
Exemplo n.º 4
0
 public void Create_DataConsumer_GoodUTI()
 {
     using (NSMutableData destData = new NSMutableData())
         using (var consumer = new CGDataConsumer(destData)) {
             using (var id = CGImageDestination.Create(consumer, GoodUti, 1)) {
                 Assert.That(id.Handle, Is.Not.EqualTo(IntPtr.Zero), "handle-1");
             }
             using (var id = CGImageDestination.Create(consumer, GoodUti, 1, new CGImageDestinationOptions())) {
                 Assert.That(id.Handle, Is.Not.EqualTo(IntPtr.Zero), "handle-2");
             }
         }
 }
Exemplo n.º 5
0
        public void ToDictionaryWithPermissions()
        {
            TestRuntime.AssertXcodeVersion(9, 0);

            var filename = Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments) + "/t.pdf";

            var info = GetInfo();

            info.AccessPermissions = CGPDFAccessPermissions.AllowsContentCopying;

            using (var url = new NSUrl(filename)) {
                using (var ctx = new CGContextPDF(url, new CGRect(0, 0, 1000, 1000), info)) {
                    Assert.IsNotNull(ctx, "1");
                }
                using (var consumer = new CGDataConsumer(url)) {
                    using (var ctx = new CGContextPDF(consumer, new CGRect(0, 0, 1000, 1000), info)) {
                        Assert.IsNotNull(ctx, "2");
                    }
                }
            }
        }
Exemplo n.º 6
0
        public static CGImageDestination?Create(CGDataConsumer consumer, string typeIdentifier, int imageCount, CGImageDestinationOptions?options = null)
        {
            if (consumer is null)
            {
                throw new ArgumentNullException(nameof(consumer));
            }
            if (typeIdentifier is null)
            {
                throw new ArgumentNullException(nameof(typeIdentifier));
            }

            using var dict = options?.ToDictionary();
            var typeId = CFString.CreateNative(typeIdentifier);

            try {
                IntPtr p = CGImageDestinationCreateWithDataConsumer(consumer.Handle, typeId, imageCount, dict.GetHandle());
                return(p == IntPtr.Zero ? null : new CGImageDestination(p, true));
            } finally {
                CFString.ReleaseNative(typeId);
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Exports the image as a PDF with the given file name.
        /// </summary>
        /// <param name="filename">Filename.</param>
        public static void ExportAsPDF(this UIImage image, string filename, CGPDFInfo info, double horizRes = 96, double vertRes = 96)
        {
            var width  = (double)image.Size.Width * (double)image.CurrentScale * 72 * horizRes;
            var height = (double)image.Size.Height * (double)image.CurrentScale * 72 * vertRes;

            var pdfFile = new NSMutableData();

            pdfFile.Init();

            var pdfConsumer = new CGDataConsumer(pdfFile);
            var mediaBox    = new CGRect(0, 0, width, height);

            using (var context = new CGContextPDF(pdfConsumer, mediaBox, info)) {
                context.BeginPage(new CGPDFPageInfo());
                context.DrawImage(mediaBox, image.CGImage);
                context.EndPage();
            }

            var attrs = new NSFileAttributes();

            NSFileManager.DefaultManager.CreateFile(filename, pdfFile, attrs);
        }
Exemplo n.º 8
0
        public static CGImageDestination Create(CGDataConsumer consumer, string typeIdentifier, int imageCount, CGImageDestinationOptions options = null)
        {
            if (consumer == null)
            {
                throw new ArgumentNullException("consumer");
            }
            if (typeIdentifier == null)
            {
                throw new ArgumentNullException("typeIdentifier");
            }

            var    dict   = options == null ? null : options.ToDictionary();
            var    typeId = NSString.CreateNative(typeIdentifier);
            IntPtr p      = CGImageDestinationCreateWithDataConsumer(consumer.Handle, typeId, imageCount, dict == null ? IntPtr.Zero : dict.Handle);

            NSString.ReleaseNative(typeId);
            var ret = p == IntPtr.Zero ? null : new CGImageDestination(p, true);

            if (dict != null)
            {
                dict.Dispose();
            }
            return(ret);
        }