Example #1
0
        public static NSData ToPdf(this UIView view, RectangleF rect)
        {
            var data = new NSMutableData();

            UIGraphics.BeginPDFContext(data, rect, null);
            UIGraphics.BeginPDFPage();
            view.Draw(rect);
            UIGraphics.EndPDFContent();

            return(data);
        }
Example #2
0
        /// <summary>
        /// Generate the image with coloured bounding boxes
        /// </summary>
        /// <param name="image">Image.</param>
        private void DrawImageWithBoundingBox(CGImage image)
        {
            // get page size in pdf document
            var pageRect = Constants.PDFPageSize;

            var imageDimension = CalculateImageDimensionInPage(pageRect, _resultImage.Width, _resultImage.Height);

            var imageWithOffset = new CGRect(imageDimension.X, imageDimension.Y - Constants.PDFPagePadding, imageDimension.Width, imageDimension.Height);

            UIGraphics.BeginPDFPage();
            using (var context = UIGraphics.GetCurrentContext())
            {
                context.SaveState();

                context.TranslateCTM(0, imageDimension.Height + Constants.PDFPagePadding);
                context.ScaleCTM(1, -1);
                context.DrawImage(imageWithOffset, image);

                context.RestoreState();

                _yOffset += imageWithOffset.Height;

                // draws number labels on image
                var observations = _imageEntry.Observations.ToList();

                foreach (var pair in observations.Select((value, index) => new { index, value }))
                {
                    // calculate new x and y position for number labels
                    var labelXPosition = (pair.value.BoundingBox.Right) / (_resultImage.Width / imageDimension.Width) +
                                         Constants.PDFPagePadding;
                    var labelYPosition = pair.value.BoundingBox.Bottom /
                                         (_resultImage.Height / imageDimension.Height) + Constants.PDFPagePadding;

                    // get string size
                    var textToDraw = string.Format(CultureInfo.CurrentCulture, Constants.NumberLabelFormat, pair.index + 1);
                    var sizeOfText =
                        textToDraw.StringSize(UIFont.BoldSystemFontOfSize(Constants.PDFImageTextLabelFontSize));
                    var rect = new CGRect(new CGPoint(labelXPosition - sizeOfText.Width, labelYPosition - sizeOfText.Height), sizeOfText);

                    // draws text label background
                    context.SetFillColor(Constants.TextLabelBackgroundColour);
                    context.FillRect(rect);

                    // draws text
                    context.SetFillColor(Constants.TextLabelTextColour);
                    textToDraw.DrawString(new CGPoint(labelXPosition - sizeOfText.Width, labelYPosition - sizeOfText.Height),
                                          UIFont.BoldSystemFontOfSize(Constants.PDFImageTextLabelFontSize));
                }

                context.Dispose();
            }
        }
Example #3
0
        public NSMutableData PrintToPdf()
        {
            var pdfData = new NSMutableData();

            UIGraphics.BeginPDFContext(pdfData, PaperRect, null);
            PrepareForDrawingPages(new NSRange(0, NumberOfPages));
            var rect = UIGraphics.PDFContextBounds;

            for (int i = 0; i < NumberOfPages; i++)
            {
                UIGraphics.BeginPDFPage();
                DrawPage(i, rect);
            }
            UIGraphics.EndPDFContent();
            return(pdfData);
        }
        NSData CreatePDF(string text, float w, float h)
        {
            NSMutableData data = new NSMutableData();

            UIGraphics.BeginPDFContext(data, new RectangleF(0, 0, w, h), null);

            UIGraphics.BeginPDFPage();
            CGContext gctx = UIGraphics.GetCurrentContext();

            gctx.ScaleCTM(1, -1);
            gctx.TranslateCTM(0, -25f);
            gctx.SelectFont("Helvetica", 25f, CGTextEncoding.MacRoman);
            gctx.ShowText(text);

            UIGraphics.EndPDFContent();
            return(data);
        }
        public static NSData PrintToPDF(this UIPrintPageRenderer renderer, CGRect paperRect)
        {
            var pdfData = new NSMutableData();

            UIGraphics.BeginPDFContext(pdfData, paperRect, null);
            var range = new NSRange(0, renderer.NumberOfPages);

            renderer.PrepareForDrawingPages(range);
            var bounds = UIGraphics.PDFContextBounds;

            for (int x = 0; x < renderer.NumberOfPages; x++)
            {
                UIGraphics.BeginPDFPage();
                renderer.DrawPage(x, bounds);
            }
            UIGraphics.EndPDFContent();
            return(pdfData);
        }
Example #6
0
        void DrawPDFInMemory()
        {
            //data buffer to hold the PDF
            NSMutableData data = new NSMutableData();

            //create a PDF with empty rectangle, which will configure it for 8.5x11 inches
            UIGraphics.BeginPDFContext(data, CGRect.Empty, null);
            //start a PDF page
            UIGraphics.BeginPDFPage();
            using (CGContext g = UIGraphics.GetCurrentContext()) {
                g.ScaleCTM(1, -1);
                g.TranslateCTM(0, -25);
                g.SelectFont("Helvetica", 25, CGTextEncoding.MacRoman);
                g.ShowText("Hello Evolve");
            }
            //complete a PDF page
            UIGraphics.EndPDFContent();
        }
Example #7
0
            private NSData PrintToPDFWithRenderer(UIPrintPageRenderer renderer, CGRect paperRect)
            {
                NSMutableData pdfData = new NSMutableData();

                UIGraphics.BeginPDFContext(pdfData, paperRect, null);

                renderer.PrepareForDrawingPages(new NSRange(0, renderer.NumberOfPages));
                _ = UIGraphics.PDFContextBounds;

                for (int i = 0; i < renderer.NumberOfPages; i++)
                {
                    UIGraphics.BeginPDFPage();
                    renderer.DrawPage(i, paperRect);
                }
                UIGraphics.EndPDFContent();

                return(pdfData);
            }
Example #8
0
        private string GeneratePdf()
        {
            NSMutableData printData = new NSMutableData();

            //create a PDF with empty rectangle, which will configure it for 8.5x11 inches

            var frame = new CGRect(0, 0, 0, 0);

            if (Orientation == PrintHelper.ORIENTATION.LANDSCAPE)
            {
                frame.Width  = 792f;
                frame.Height = 612f;
            }
            else
            {
                frame.Width  = 612f;
                frame.Height = 792f;
            }

            UIGraphics.BeginPDFContext(printData, frame, null);

            if (renderer != null)
            {
                for (int i = 0; i < renderer.NumberOfPages; i++)
                {
                    UIGraphics.BeginPDFPage();

                    renderer.DrawHeaderForPage(i, new CGRect());
                    renderer.DrawFooterForPage(i, new CGRect());
                    renderer.DrawContentForPage(i, new CGRect());
                    renderer.DrawPage(i, new CGRect());
                }
            }

            // complete a PDF page
            UIGraphics.EndPDFContent();

            var path    = NSFileManager.DefaultManager.GetUrls(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomain.User) [0].Path;
            var pdfFile = System.IO.Path.Combine(path, "test.pdf");

            printData.Save(pdfFile, true);

            return(pdfFile);
        }
Example #9
0
        /// <summary>
        /// Draws the category summary including image informations and categories count for the image.
        /// </summary>
        private void DrawCategorySummary()
        {
            // start the page with padding
            _yOffset = Constants.PDFPagePadding;

            // start a PDF page
            UIGraphics.BeginPDFPage();

            // draws file name
            var filename = _imageEntry.Name;

            filename.DrawString(new CGPoint(Constants.PDFPagePadding, _yOffset),
                                UIFont.BoldSystemFontOfSize(Constants.PDFPageImageTitleFontSize));
            _yOffset += Constants.PDFPageImageTitleFontSize + Constants.PDFPageTextPadding;

            // draws file creation date
            var fileDate = _imageEntry.CreationTime.ToString();

            fileDate.DrawString(new CGPoint(Constants.PDFPagePadding, _yOffset),
                                UIFont.SystemFontOfSize(Constants.PDFPageTextFontSize));
            _yOffset += Constants.PDFPageTextFontSize + Constants.PDFPageTextPadding;

            // draws categories text
            var selectedFilter = Constants.CategoriesText;

            selectedFilter.DrawString(new CGPoint(Constants.PDFPagePadding, _yOffset),
                                      UIFont.BoldSystemFontOfSize(Constants.PDFPageTextFontSize));
            _yOffset += Constants.PDFPageTextFontSize + Constants.PDFPageTextPadding;

            var categories     = _imageEntry.Observations.Select(o => o.Category.Label).Distinct().ToList();
            var categoriesText = string.Join(Constants.CategoriesJoinText, categories);

            categoriesText.DrawString(new CGPoint(Constants.PDFPagePadding, _yOffset),
                                      UIFont.SystemFontOfSize(Constants.PDFPageTextFontSize));
            _yOffset += Constants.PDFPageTextFontSize + Constants.PDFPageTextPadding;

            // draws table grids for category summary
            var tableOrigin = new CGPoint(Constants.PDFPagePadding, _yOffset);

            DrawTableLineAt(tableOrigin, Constants.PDFPageTableRowHeight, Constants.PDFPageTableColumnWidth,
                            categories.Count + 1, Constants.PDFCategoryTableNumberOfColumn);
            GenerateCategoryTableContent(tableOrigin, Constants.PDFPageTableRowHeight,
                                         Constants.PDFPageTableColumnWidth);
        }
        public void RedrawDailyStockPDF(bool DocumentSigned)
        {
            // render created preview in PDF context
            NSMutableData pdfData = new NSMutableData();

            UIGraphics.BeginPDFContext(pdfData, GeneratedPDFView.Bounds, null);
            UIGraphics.BeginPDFPage();
            GeneratedPDFView.Layer.RenderInContext(UIGraphics.GetCurrentContext());
            UIGraphics.EndPDFContent();

            // save the rendered context to disk
            NSError err;
            string  pdfID = String.Format("{0}_{1}_{2}_UsedStock", MyConstants.EmployeeID, MyConstants.EmployeeName, DateTime.Now.Date.ToString("yyyy-MM-dd"));
            string  pdfFileName;

            if (DocumentSigned)
            {
                pdfFileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), pdfID + "_Signed.pdf");
            }
            else
            {
                pdfFileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), pdfID + "_Not_Signed.pdf");
            }
            pdfData.Save(pdfFileName, true, out err);

            if (err != null)
            {
                err.Dispose(); err = null;
            }
            if (pdfData != null)
            {
                pdfData.Dispose(); pdfData = null;
            }

            // set up UIWebView in signature view controller
            this.PDFView.MultipleTouchEnabled = true;
            this.PDFView.ScalesPageToFit      = true;
            this.PdfFileName = pdfFileName;

            if (DocumentSigned)
            {
                PDFView.LoadRequest(new NSUrlRequest(NSUrl.FromFilename(PdfFileName)));
            }
        }
        public void PrintToPDF()
        {
            IDirectory dirInstance = IoCContainer.Instance.ResolveInterface <IDirectory> ();
            string     fileName    = dirInstance.GetAppRootPath() + ViewConstant.SHARE_TMP_PDF_NAME;

            UIGraphics.BeginPDFContext(fileName, PaperRect, new CGPDFInfo {
                Author = "LexisNexis Red", Title = "LexisNexis Red"
            });

            PrepareForDrawingPages(new NSRange(0, NumberOfPages));
            CGRect bounds = UIGraphics.PDFContextBounds;

            for (int i = 0; i < NumberOfPages; i++)
            {
                UIGraphics.BeginPDFPage();
                DrawPage(i, bounds);
            }

            UIGraphics.EndPDFContent();
        }
Example #12
0
        public NSMutableData PrintToPdf()
        {
            var pdfData = new NSMutableData();

            // Second parameter (CGRect bounds) of BeginPDFContext controls the size of the page onto which the content is rendered ... but not the content size.
            // So the content (if bigger) will be clipped (both vertically and horizontally).
            // Also, pagenation is determinted by the content - independent of the below CGRect bounds.
            UIGraphics.BeginPDFContext(pdfData, PaperRect, null);
            //UIGraphics.BeginPDFContext(pdfData, new CGRect(0, 0, 200, 300), null);
            PrepareForDrawingPages(new NSRange(0, NumberOfPages));
            var rect = UIGraphics.PDFContextBounds;

            for (int i = 0; i < NumberOfPages; i++)
            {
                UIGraphics.BeginPDFPage();
                DrawPage(i, rect);
            }
            UIGraphics.EndPDFContent();
            return(pdfData);
        }
        public void RedrawPrePlumbingPDF(bool ThereAndBack, bool DocumentSigned)
        {
            if (ThereAndBack)
            {
                _navWorkflow.PopToRootViewController(false);
            }
            // render created preview in PDF context
            NSMutableData pdfData = new NSMutableData();

            UIGraphics.BeginPDFContext(pdfData, _generatedPdfView.Bounds, null);
            UIGraphics.BeginPDFPage();
            _generatedPdfView.Layer.RenderInContext(UIGraphics.GetCurrentContext());
            UIGraphics.EndPDFContent();
            // save the rendered context to disk
            NSError err;
            string  pdfFileName;

            string pdfID = _navWorkflow._tabs._jobRunTable.CurrentCustomer.CustomerNumber.ToString() + "_PrePlumbingPDF";

            if (DocumentSigned)
            {
                pdfFileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), pdfID + "_Signed.pdf");
            }
            else
            {
                pdfFileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), pdfID + "_Not_Signed.pdf");
            }

            pdfData.Save(pdfFileName, true, out err);
            err = null; pdfData = null;

            // load the content into Signing view controller

            _navWorkflow._tabs.SignPre.PDFView.MultipleTouchEnabled = true;
            _navWorkflow._tabs.SignPre.PDFView.ScalesPageToFit      = true;
            _navWorkflow._tabs.SignPre.PDFView.LoadRequest(new NSUrlRequest(NSUrl.FromFilename(pdfFileName)));
            pdfPrePlumbingFileName = pdfFileName;
            // if (ThereAndBack) _navWorkflow.PushViewController (_navWorkflow._tabs._signView, true);
        }
Example #14
0
        private NSData PrintToPDFWithRenderer(UIPrintPageRenderer renderer, CGRect paperRect)
        {
            NSMutableData pdfData = new NSMutableData();

            try
            {
                UIGraphics.BeginPDFContext(pdfData, paperRect, null);
                renderer.PrepareForDrawingPages(new NSRange(0, renderer.NumberOfPages));
                for (int i = 0; i < renderer.NumberOfPages; i++)
                {
                    UIGraphics.BeginPDFPage();
                    renderer.DrawPage(i, paperRect);
                }
                UIGraphics.EndPDFContent();
            }
            catch
            {
                PDFToHtml.Status = PDFEnum.Failed;
            }

            return(pdfData);
        }
Example #15
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 (_tempFilePath == null)
            {
                _tempFilePath = Path.GetTempFileName();
                UIGraphics.BeginPDFContext(_tempFilePath, CGRect.Empty, _documentInfo);
            }

            var pageInfo = new NSMutableDictionary();

            UIGraphics.BeginPDFPage(new CGRect(0, 0, width, height), pageInfo);

            var context = UIGraphics.GetCurrentContext();

            context.SetFillColorSpace(CGColorSpace.CreateDeviceRGB());
            context.SetStrokeColorSpace(CGColorSpace.CreateDeviceRGB());
            _canvas.Context = context;
        }
Example #16
0
        /// <summary>
        /// Draws the observation table grids.
        /// </summary>
        private void DrawObservationTable()
        {
            // start the page with padding
            _yOffset = Constants.PDFPagePadding;

            var currentObservations = _imageEntry.Observations.ToList();

            // get page size in pdf document
            var pageHeight = Constants.PDFPageSize.Height;

            // calculates the maximum rows in a page by considering top and bottom padding of the page
            var rowsInPage =
                (int)Math.Floor((pageHeight - Constants.PDFPagePadding * 2) / Constants.PDFPageTableRowHeight);

            // if the table has more row than maximum rows in a page, start a new page
            for (var i = 0; i < currentObservations.Count; i += rowsInPage)
            {
                // start a PDF page
                UIGraphics.BeginPDFPage();

                // set the start coordinate of table and draw table grids
                var tableOrigin = new CGPoint(Constants.PDFPagePadding, _yOffset);
                DrawTableLineAt(tableOrigin, Constants.PDFPageTableRowHeight, Constants.PDFPageTableColumnWidth,
                                rowsInPage, Constants.PDFObservationTableNumberOfColumn);

                // if it is the first page of table, draws table header before generates table content
                if (i == 0)
                {
                    // generates table header
                    Constants.ObservationTableHeader[0].DrawString(
                        new CGPoint(tableOrigin.X + Constants.PDFPageTableTextPadding,
                                    tableOrigin.Y + Constants.PDFPageTableTextPadding),
                        UIFont.BoldSystemFontOfSize(Constants.PDFPageTextFontSize));

                    Constants.ObservationTableHeader[1].DrawString(
                        new CGPoint(
                            tableOrigin.X + Constants.PDFPageTableTextPadding + Constants.PDFPageTableColumnWidth,
                            tableOrigin.Y + Constants.PDFPageTableTextPadding),
                        UIFont.BoldSystemFontOfSize(Constants.PDFPageTextFontSize));

                    Constants.ObservationTableHeader[2].DrawString(
                        new CGPoint(
                            tableOrigin.X + Constants.PDFPageTableTextPadding +
                            2 * Constants.PDFPageTableColumnWidth,
                            tableOrigin.Y + Constants.PDFPageTableTextPadding),
                        UIFont.BoldSystemFontOfSize(Constants.PDFPageTextFontSize));

                    _yOffset += Constants.PDFPageTableRowHeight;

                    // re-calculates the y position of current content and generate table contents
                    tableOrigin = new CGPoint(Constants.PDFPagePadding, _yOffset);

                    GenerateObservationTableContent(tableOrigin, Constants.PDFPageTableRowHeight,
                                                    Constants.PDFPageTableColumnWidth,
                                                    rowsInPage > currentObservations.Count
                            ? currentObservations.GetRange(i, currentObservations.Count)
                            : currentObservations.GetRange(i, rowsInPage - 1), i);
                }
                // otherwise, generates table content directly
                else
                {
                    GenerateObservationTableContent(tableOrigin, Constants.PDFPageTableRowHeight,
                                                    Constants.PDFPageTableColumnWidth, currentObservations.Count - i < rowsInPage
                        ? currentObservations.GetRange(i - 1, currentObservations.Count - i + 1)
                        : currentObservations.GetRange(i - 1, rowsInPage), i - 1);
                }
            }
        }