예제 #1
0
        void WritePrintedPdfDoc(ParcelFileDescriptor destination)
        {
            var javaStream = new Java.IO.FileOutputStream(destination.FileDescriptor);
            var osi        = new OutputStreamInvoker(javaStream);

            using (var mem = new MemoryStream()) {
                document.WriteTo(mem);
                var bytes = mem.ToArray();
                osi.Write(bytes, 0, bytes.Length);
            }
        }
예제 #2
0
            public override void OnWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback)
            {
                int totalPages = 1;

                for (int i = 0; i < totalPages; i++)
                {
                    if (pageInRange(pages, i))
                    {
                        PdfDocument.Page[] writtenPagesArray;
                        PdfDocument.Page   page = mPdfDocument.StartPage(i);
                        if (cancellationSignal.IsCanceled)
                        {
                            callback.OnWriteCancelled();
                            mPdfDocument.Close();
                            mPdfDocument = null;
                            return;
                        }
                        drawPage(page);
                        mPdfDocument.FinishPage(page);
                    }
                }
                try
                {
                    FileOutputStream fileOutputStream = new FileOutputStream(destination.FileDescriptor);
                    MemoryStream     stream           = new MemoryStream();
                    mPdfDocument.WriteTo(stream);
                    byte[] bytes = stream.ToArray();
                    stream.Close();
                    fileOutputStream.Write(bytes);
                }
                catch (Java.IO.IOException e)
                {
                    callback.OnWriteFailed(e.ToString());
                    return;
                }
                finally
                {
                    mPdfDocument.Close();
                    mPdfDocument = null;
                }
                callback.OnWriteFinished(pages);
            }
예제 #3
0
        public void GeneratePdfFile(string text)
        {
            PrintAttributes printAttrs = new PrintAttributes.Builder()
                                         .SetColorMode(PrintColorMode.Color)
                                         .SetMediaSize(PrintAttributes.MediaSize.IsoA4)
                                         .SetResolution(new PrintAttributes.Resolution("guardianHealthId", "TestReport", 592, 842))
                                         .SetMinMargins(PrintAttributes.Margins.NoMargins)
                                         .Build();

            // Create a document
            PdfDocument document = new PrintedPdfDocument(Android.App.Application.Context, printAttrs);

            // Create a page description
            // A4 : Page size
            // pageWidth The page width in PostScript (1/72th of an inch)
            // pageHeight The page height in PostScript(1 / 72th of an inch).
            PageInfo pageInfo = new PageInfo.Builder(592, 842, 1).Create();

            PdfDocument.Page page = document.StartPage(pageInfo);

            Xamarin.Forms.Button btn = new Xamarin.Forms.Button();


            // File to create page
            File path = GetDirectoryPath();

            System.IO.FileStream fileStream = new System.IO.FileStream(path.ToString(), System.IO.FileMode.Create);

            Canvas canvas = page.Canvas;
            Paint  paint  = new Paint();

            canvas.DrawText("This is a demo pdf file.\n" + text, 10, 10, paint);

            document.FinishPage(page);
            document.WriteTo(fileStream);
            document.Close();
            fileStream.Close();
        }
예제 #4
0
                protected override Java.Lang.Object DoInBackground(params Java.Lang.Object[] native_parms)
                {
                    // Go over all the pages and write only the requested ones.
                    // Create an adapter with the stats and an inflater
                    // to load resources for the printer density.
                    var adapter = new MotoGpStatAdapter(items,
                                                        (LayoutInflater)self.mPrintContext.GetSystemService(
                                                            Context.LayoutInflaterService));

                    int  currentPage       = -1;
                    int  pageContentHeight = 0;
                    int  viewType          = -1;
                    View view = null;

                    Android.Graphics.Pdf.PdfDocument.Page page = null;
                    var dummyParent = new LinearLayout(self.mPrintContext);

                    dummyParent.Orientation = Android.Widget.Orientation.Vertical;

                    // The content is laid out and rendered in screen pixels with
                    // the width and height of the paper size times the print
                    // density but the PDF canvas size is in points which are 1/72",
                    // so we will scale down the content.
                    float scale = System.Math.Min(
                        (float)mPdfDocument.PageContentRect.Width() / self.mRenderPageWidth,
                        (float)mPdfDocument.PageContentRect.Height() / self.mRenderPageHeight);

                    int itemCount = adapter.Count;

                    for (int i = 0; i < itemCount; i++)
                    {
                        // Be nice and respond to cancellation.
                        if (IsCancelled)
                        {
                            return(null);
                        }

                        // Get the next view.
                        int nextViewType = adapter.GetItemViewType(i);
                        if (viewType == nextViewType)
                        {
                            view = adapter.GetView(i, view, dummyParent);
                        }
                        else
                        {
                            view = adapter.GetView(i, null, dummyParent);
                        }
                        viewType = nextViewType;

                        // Measure the next view
                        self.MeasureView(view);

                        // Add the height but if the view crosses the page
                        // boundary we will put it to the next one.
                        pageContentHeight += view.MeasuredHeight;
                        if (currentPage < 0 || pageContentHeight > self.mRenderPageHeight)
                        {
                            pageContentHeight = view.MeasuredHeight;
                            currentPage++;
                            // Done with the current page - finish it.
                            if (page != null)
                            {
                                mPdfDocument.FinishPage(page);
                            }
                            // If the page is requested, render it.
                            if (self.ContainsPage(pages, currentPage))
                            {
                                page = mPdfDocument.StartPage(currentPage);
                                page.Canvas.Scale(scale, scale);
                                // Keep track which pages are written.
                                mWrittenPages.Append(mWrittenPages.Size(), currentPage);
                            }
                            else
                            {
                                page = null;
                            }
                        }

                        // If the current view is on a requested page, render it.
                        if (page != null)
                        {
                            // Layout an render the content.
                            view.Layout(0, 0, view.MeasuredWidth, view.MeasuredHeight);
                            view.Draw(page.Canvas);
                            // Move the canvas for the next view.
                            page.Canvas.Translate(0, view.Height);
                        }
                    }

                    // Done with the last page.
                    if (page != null)
                    {
                        mPdfDocument.FinishPage(page);
                    }

                    // Write the data and return success or failure.
                    try {
                        var fos = new Java.IO.FileOutputStream(destination.FileDescriptor);
                        mPdfDocument.WriteTo(new OutputStreamInvoker(fos));
                        // Compute which page ranges were written based on
                        // the bookkeeping we maintained.
                        var pageRanges = self.ComputeWrittenPageRanges(mWrittenPages);
                        callback.OnWriteFinished(pageRanges);
                    } catch (IOException) {
                        callback.OnWriteFailed((string)null);
                    } finally {
                        mPdfDocument.Close();
                    }

                    return(null);
                }