I am trying to render multiple PDF pages on a custom View in Xamarin.
public override void Draw( AG.Canvas canvas ) {
base.Draw( canvas );
if( !(Parent is AW.ScrollView p) || pdfRenderer == null )
return;
int topPage = pdfRenderer.PageCount * p.ScrollY / Height;
while( topPage < pdfRenderer.PageCount && topPage * screenPageHeight < p.ScrollY + p.Height ) {
using( var page = pdfRenderer.OpenPage( topPage ) ) {
page.Render( bitmap, null, null, PdfRenderMode.ForDisplay );
page.Close();
}
AG.Rect pageRect = new AG.Rect {
Left = 0,
Top = topPage * screenPageHeight,
Right = Width,
Bottom = (topPage + 1) * screenPageHeight,
};
canvas.DrawBitmap( bitmap, null, pageRect, null );
topPage++;
}
}
The output is drawing the page from the last loop run in all pageRect's. I can imagine why it might happen, but the real question is how I can fix this code to draw all pages without creating a separate bitmap for each page.
EDIT. Debugger screenshots attached. (At canvas.DrawBitmap breakpoint)
EDIT. Added java code. Expected: one red and one green rect. Observed: two green rects.
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new TestView());
}
class TestView extends View {
public TestView() {
super(MainActivity.this);
}
Bitmap bmp = Bitmap.createBitmap(600, 200, Bitmap.Config.ARGB_8888);
Paint paint = new Paint();
void FillBitmap(int color) {
Canvas canvas = new Canvas(bmp);
canvas.drawColor(color);
}
protected void onDraw (Canvas canvas) {
FillBitmap(Color.RED);
canvas.drawBitmap(bmp, 60, 120, paint);
FillBitmap(Color.GREEN);
canvas.drawBitmap(bmp, 60, 440, paint);
}
protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(720, 720);
}
}
}