Beispiel #1
0
                public MyOnWriteAsyncTask(MyPrintDocumentAdapter self, PageRange[] pages, ParcelFileDescriptor destination,
                                          CancellationSignal cancellationSignal, WriteResultCallback callback)
                {
                    this.self               = self;
                    this.pages              = pages;
                    this.destination        = destination;
                    this.cancellationSignal = cancellationSignal;
                    this.callback           = callback;

                    items        = ((MotoGpStatAdapter)self.pcc.ListAdapter).CloneItems();
                    mPdfDocument = new PrintedPdfDocument(self.mPrintContext, self.mPrintAttributes);
                }
        public override void OnLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes,
                                      CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras)
        {
            document = new PrintedPdfDocument(context, newAttributes);

            CalculateScale(newAttributes);

            var printInfo = new PrintDocumentInfo
                            .Builder("pdf-test.pdf")
                            .SetContentType(PrintContentType.Document)
                            .SetPageCount(1)
                            .Build();

            callback.OnLayoutFinished(printInfo, true);
        }
Beispiel #3
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);
            }
Beispiel #4
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();
        }
Beispiel #5
0
            public override void OnLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras)
            {
                mPdfDocument = new PrintedPdfDocument(CrossPrint.appcontext, newAttributes);

                if (cancellationSignal.IsCanceled)
                {
                    callback.OnLayoutCancelled();
                    return;
                }
                int pages = computePageCount(newAttributes);

                if (pages > 0)
                {
                    PrintDocumentInfo info = new PrintDocumentInfo
                                             .Builder("pdf.pdf")
                                             .SetContentType(PrintContentType.Document)
                                             .SetPageCount(pages).Build();
                    callback.OnLayoutFinished(info, true);
                }
                else
                {
                    callback.OnLayoutFailed("Page count calculation failed.");
                }
            }