2

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();
    }

2 Answers 2

1

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();
    }
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.