[FIXED] Xfinium PDF: Unable to save pdf file. Receiving error root missing entry

Issue

I have a problem saving the Xfinium PDF. I loaded a document and simply draw the line across the page and save. The file generated but when I used that file and loaded back to PdfFixedDocument I have the error

Root entry is missing in file trailer

My code is very simple :

var pdf = new PdfFixedDocument (document.Location);
        var page = pdf.Pages [pageIndex];
        var graphics = page.Graphics;
        var directory = FileUtilities.GetExternalPrivateDirectory (PdfCore.CACHE_DIRECTORY);
        //var png = FileUtilities.GetFile (directory + "/test.pdf");  
        //var rawStream = File.OpenWrite (png.AbsolutePath);
        var stream = new FileStream (directory + "/test.pdf", FileMode.Create);
        pdf.BeginSave (stream);

        graphics.DrawLine(new Xfinium.Pdf.Graphics.PdfPen (),
            new Xfinium.Pdf.Graphics.PdfPoint (0,0),
            new Xfinium.Pdf.Graphics.PdfPoint (page.Width, page.Height));

        page.SaveGraphics (); 

        pdf.EndSave ();

Solution

Instead of manually creating the stream and using BeginSave / EndSave, just try using Save:

var pdf = new PdfFixedDocument();
var page = pdf.Pages.Add();
var graphics = page.Graphics;
                graphics.DrawLine(new Xfinium.Pdf.Graphics.PdfPen(),
    new Xfinium.Pdf.Graphics.PdfPoint(0, 0),
    new Xfinium.Pdf.Graphics.PdfPoint(page.Width, page.Height));
var directory = Environment.GetExternalStoragePublicDirectory(Environment.DirectoryDownloads);
pdf.Save(Path.Combine(directory.Path, "test.pdf"));

Java.IO.File pdfFILE = new Java.IO.File(Path.Combine(directory.Path, "test.pdf"));
Intent intent = new Intent(Intent.ActionView);
intent.SetDataAndType(Uri.FromFile(pdfFILE), "application/pdf");
StartActivity(intent);

enter image description here

Answered By – SushiHangover

Answer Checked By – Mildred Charles (Easybugfix Admin)

Leave a Reply

(*) Required, Your email will not be published