3

I'm working with PDFTron and it works all nice and dandy for doing stuff with PDF's. I made an app with xamarin in which people open up alot of pdf's to sign them. This works fine as well.

The problem starts however when one zooms in and out. Then the memory suddenly starts filling up. After zooming in at a few documents the app crashes..

Anyone an idea what's happening?

This is the activity in which all the magic happens:

Init PDFViewCtrl

    private PDFViewCtrl _mPdfViewCtrl;
    SetupViewer(filePath);

When backbutton is pressed

    public override void OnBackPressed()
    {
        base.OnBackPressed();
        _syncManager = null;
        DisposePdfCtrl();

        Finish();

    }

Saving doc

_mPdfViewCtrl.Doc.Save(filePath, 0);

Setup Pdf view

private void SetupViewer(string filePath)
    {
        try
        {

        PDFNet.SetDefaultDiskCachingEnabled(true);
        PDFNet.SetViewerCache(100 * 1024 * 1024, false);
        }
        catch (pdftron.Common.PDFNetException ex)
        {
        Logger.log(Title, "Failed to setup viewer");


        }

        try
        {

        Logger.log(Title, $"Setting up pdf viewer");
        SetContentView(Resource.Layout.PdfView);

        var toolbar = FindViewById<Toolbar>(Resource.Id.toolbar);
        SetSupportActionBar(toolbar);

        SupportActionBar.Title = "Sign Document";

        Logger.log(Title, $"Toolbar is setup.");
        _mPdfViewCtrl = FindViewById<PDFViewCtrl>(Resource.Id.pdfviewctrl);

        _mPdfViewCtrl.PagePresentationMode = PDFViewCtrl.PagePresentationModes.e_single_page;
        _mPdfViewCtrl.PageViewMode = PDFViewCtrl.PageViewModes.e_fit_page;
        _mPdfViewCtrl.PageRefViewMode = PDFViewCtrl.PageViewModes.e_fit_page;
        _mPdfViewCtrl.SetHighlightFields(true);

        Logger.log(Title, $"Opening file in viewer: {filePath}");

        var docToOpen = new PDFDoc(filePath);
        docToOpen.InitStdSecurityHandler("");

        if (!_allowChange)
        {
            docToOpen.FlattenAnnotations();
        }

        Logger.log(Title, "setting file to pdf viewer");
        _mPdfViewCtrl.Doc = docToOpen;




        var toolManager = new ToolManager(_mPdfViewCtrl);
        Logger.log(Title, "setting file to pdf viewer");
        _mPdfViewCtrl.ToolManager = toolManager;



        }
        catch (Exception ex)
        {
        Logger.log(Title, $"Error while setting up pdf view: {ex.Message}");
        }
    }

On pause

    protected override void OnPause()
    {
        base.OnPause();
        DisposePdfCtrl();
    }

On Resume

    protected override async void OnResume()
    {
        base.OnResume();
        try
        {
        if (!_busy)
        {

            _mPdfViewCtrl?.Resume();
        }
        }
        catch (ObjectDisposedException)
        {
        SetupViewer(filePath);
        }
    }

On Destroy

protected override void OnDestroy()
{
    base.OnDestroy();
    DisposePdfCtrl();

}

Disposing

bool disposed = false;
private void DisposePdfCtrl()
{
    if (_disposing)
    return;

    _disposing = true;
    if (disposed) return;
        try
        {
            var oldTool = _mPdfViewCtrl.ToolManager;
            var oldDoc = _mPdfViewCtrl.Doc;

            _mPdfViewCtrl.Doc.Unlock();
            _mPdfViewCtrl?.PurgeMemory();

            _mPdfViewCtrl?.CloseDoc();
            _mPdfViewCtrl?.CloseTool();


            oldDoc?.Dispose();
            oldTool?.Dispose();

            _mPdfViewCtrl?.Dispose();
            _mPdfViewCtrl = null;
            _syncManager = null;
            GC.Collect();
            this.Dispose();

        } catch (Exception ex)
        {

        }
        disposed = true;
    _disposing = false;
}

On low mem

public override void OnLowMemory()
{
    base.OnLowMemory();
    Logger.log(Title, "Low on memory");
    _mPdfViewCtrl?.PurgeMemory();
}
S B
  • 309
  • 2
  • 9

0 Answers0