mike29892
mike29892

Reputation: 263

Xamarin Android: Cannot convert Java.IO.ByteArrayOutputStream expression to type System.IO.Stream

I am using Xamarin Android and on line "document.WriteTo(outPut);" I am getting a compile time error. The equivalent code compiles in Java, but using Xamarin and c# I am having some type of casting/conversion issue. Does anyone know how to fix this? What I am trying to do is take my created pdf and turn it into a byte array.

 Byte[] MakePDFFromImages(){
        // open a new document
        PrintAttributes printAttributes = new PrintAttributes.Builder().
            SetColorMode(Android.Print.PrintColorMode.Color).
            SetMediaSize(PrintAttributes.MediaSize.IsoA4).
            SetResolution(new PrintAttributes.Resolution("zooey","test", 450, 700)).
            SetMinMargins(PrintAttributes.Margins.NoMargins).
            Build();

        PrintedPdfDocument document = new PrintedPdfDocument (Activity.BaseContext, printAttributes);

        // start a page
        Android.Graphics.Pdf.PdfDocument.Page page = document.StartPage(0);

        ImageView imageView = new ImageView (Activity.BaseContext);
        imageView.SetImageBitmap (_imageArray [0]);

        imageView.Draw(page.Canvas);

        document.FinishPage(page);

        ByteArrayOutputStream outPut = new ByteArrayOutputStream();
        try {
            document.WriteTo(outPut);
            document.Close();
            outPut.Close();
        }
        catch(Exception){

        }

        return outPut.ToByteArray();
    }

Upvotes: 2

Views: 1884

Answers (2)

Jason
Jason

Reputation: 89117

If you read the Xamarin API docs, the WriteTo() method of PDFDocument expects a .NET System.IO.Stream, not a Java stream.

This sample shows how to use the PDFDocument class in Xamarin

Upvotes: 0

Evk
Evk

Reputation: 101493

PrintedPdfDocument.Write expects .NET Stream, while you are passing it Java ByteArrayOutputStream. To fix, use .NET MemoryStream:

    Byte[] MakePDFFromImages()
    {
        // open a new document
        PrintAttributes printAttributes = new PrintAttributes.Builder().
            SetColorMode(Android.Print.PrintColorMode.Color).
            SetMediaSize(PrintAttributes.MediaSize.IsoA4).
            SetResolution(new PrintAttributes.Resolution("zooey", "test", 450, 700)).
            SetMinMargins(PrintAttributes.Margins.NoMargins).
            Build();

        PrintedPdfDocument document = new PrintedPdfDocument(Activity.BaseContext, printAttributes);

        // start a page
        Android.Graphics.Pdf.PdfDocument.Page page = document.StartPage(0);

        ImageView imageView = new ImageView(Activity.BaseContext);
        imageView.SetImageBitmap(_imageArray[0]);

        imageView.Draw(page.Canvas);

        document.FinishPage(page);

        var outPut = new MemoryStream();
        try
        {
            document.WriteTo(outPut);
            document.Close();
            outPut.Close();
        }
        catch (Exception)
        {

        }

        return outPut.ToArray();
    }

Upvotes: 1

Related Questions