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; }
public static void CopyPath(CGPath cgpath) { var pdfpath = Path.GetTempPath() + ".pdf"; var context = new CGContextPDF(NSUrl.FromFilename(pdfpath)); CGRect rc = new CGRect(0, 0, 500, 500); context.BeginPage(rc); using (var gc = NSGraphicsContext.FromGraphicsPort(context, false)) { var ctx = gc.CGContext; //ctx.SetStrokeColor(new CGColor(0, 0, 100)); //ctx.SetLineWidth(2); //ctx.SetFillColor(new CGColor(100, 0, 0)); ctx.AddPath(cgpath); ctx.DrawPath(CGPathDrawingMode.FillStroke); } context.EndPage(); context.Close(); var data = NSData.FromFile(pdfpath); NSPasteboard.GeneralPasteboard.ClearContents(); NSPasteboard.GeneralPasteboard.SetDataForType(data, NSPasteboard.NSPdfType); }
public void Context_Url() { using (var url = new NSUrl(filename)) using (var ctx = new CGContextPDF(url)) { ctx.BeginPage(PDFInfoTest.GetInfo()); ctx.SetUrl(url, CGRect.Empty); ctx.EndPage(); } }
public void Dispose() { if (context != null) { context.EndPage(); context.Dispose(); context = null; } }
static void Main() { var pdf = new CGContextPDF(NSUrl.FromFilename("demo.pdf"), new RectangleF(0, 0, 617, 792)); pdf.BeginPage(null); pdf.SetRGBFillColor(1, 0, 0, 1); pdf.AddArc(300, 300, 100, 0, (float)(2 * Math.PI), true); pdf.FillPath(); pdf.EndPage(); pdf.Flush(); }
public void Context_Url_Rect() { var rect = new CGRect(10, 10, 100, 100); using (var url = new NSUrl(filename)) using (var ctx = new CGContextPDF(url, rect)) { ctx.BeginPage((CGPDFPageInfo)null); ctx.SetDestination("xamarin", rect); ctx.EndPage(); } }
static void Main () { NSApplication.Init (); NSUrl path = NSUrl.FromFilename (Path.Combine ("../../..", "demo.pdf")); //Escape out of generate-pdf.app/Contents/Resources var pdf = new CGContextPDF (path, new RectangleF (0, 0, 617, 792)); pdf.BeginPage (null); pdf.SetFillColor (1, 0, 0, 1); pdf.AddArc (300, 300, 100, 0, (float) (2 * Math.PI), true); pdf.FillPath (); pdf.EndPage (); pdf.Flush (); }
static void Main() { NSApplication.Init(); NSUrl path = NSUrl.FromFilename(Path.Combine("../../..", "demo.pdf")); //Escape out of generate-pdf.app/Contents/Resources var pdf = new CGContextPDF(path, new RectangleF(0, 0, 617, 792)); pdf.BeginPage(null); pdf.SetFillColor(1, 0, 0, 1); pdf.AddArc(300, 300, 100, 0, (float)(2 * Math.PI), true); pdf.FillPath(); pdf.EndPage(); pdf.Flush(); }
public void Context_Tag() { TestRuntime.AssertXcodeVersion(11, 0); using (var d = new NSDictionary()) using (var url = new NSUrl(filename)) using (var ctx = new CGContextPDF(url)) { ctx.BeginPage(PDFInfoTest.GetInfo()); ctx.BeginTag(CGPdfTagType.Header, (NSDictionary)null); ctx.EndTag(); ctx.BeginTag(CGPdfTagType.Caption, d); ctx.SetUrl(url, CGRect.Empty); ctx.EndTag(); ctx.EndPage(); } }
public void Context_Tag_Strong() { TestRuntime.AssertXcodeVersion(11, 0); using (var url = new NSUrl(filename)) using (var ctx = new CGContextPDF(url)) { var tp = new CGPdfTagProperties() { ActualText = "ActualText", AlternativeText = "AlternativeText", TitleText = "TitleText", LanguageText = "LanguageText", }; ctx.BeginPage(PDFInfoTest.GetInfo()); ctx.BeginTag(CGPdfTagType.Header, tp); ctx.EndTag(); ctx.BeginTag(CGPdfTagType.Caption, (CGPdfTagProperties)null); ctx.SetUrl(url, CGRect.Empty); ctx.EndTag(); ctx.EndPage(); } }
/// <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); }