Welcome Guest Search | Active Topics | Sign In | Register

Document Metadata in MVCToPDF Options
BrianW
Posted: Wednesday, October 23, 2013 2:04:25 PM
Rank: Newbie
Groups: Member

Joined: 10/23/2013
Posts: 2
I'm using the MVC to PDF functionality and am struggling with getting the document metadata (author, creation date, title, etc) set in the PDF to be returned to the client. I have implemented the OnResultExecuted method and am setting the values on MVCToPDF.Result.PdfDocument.Info but the returned file doesn't include them. The examples I've seen all do an explicit save of the file on the server which I'd like to avoid if at all possible. Is there a way to do this? My code follows:

Code: C#
[RenderAsPDF(AutoConvert=false)]
        public ActionResult DocPrint()
        {
            HtmlToPdf.Options.PageSize = PdfPageSizes.Letter;
            HtmlToPdf.Options.OutputArea = new RectangleF(0.5f, 0.5f, 7.5f, 10f);
            var model = GetModel();
            MVCToPDF.ResultFileName = "Result.pdf";
            MVCToPDF.RenderAsPDF();
            return View(model);
        }

        public override void OnResultExecuted(ResultExecutedContext filterContext)
        {
            base.OnResultExecuted(filterContext);
            EO.Pdf.HtmlToPdfResult result = MVCToPDF.Result;
            if (result != null)
            {
                result.PdfDocument.Info.Author = "Initech";
                result.PdfDocument.Info.CreationDate = DateTime.Now;
                result.PdfDocument.Info.Creator = "Initech";
                result.PdfDocument.Info.Title = MVCToPDF.ResultFileName;
            }
        }
eo_support
Posted: Wednesday, October 23, 2013 2:26:14 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,196
Hi,

You would use something like this:

Code: C#
protected override void OnResultExecuted(ResultExecutedContext filterContext)
{
    //Set up a handler to be called after every page is rendered
    EO.Pdf.HtmlToPdf.Options.AfterRenderPage = 
        new EO.Pdf.PdfPageEventHandler(AfterRenderPage);

    base.OnResultExecuted(filterContext);
}

private bool m_bDocInfoSet;

private void AfterRenderPage(object sender, EO.Pdf.PdfPageEventArgs e)
{
    //Only set the document info once
    if (!m_bDocInfoSet)
    {
        //Set the document info
        e.Page.Document.Info.Author = "test";
        .....

        m_bDocInfoSet = true;
    }
}


The reason that you can not use MVCToPDF.Result is because at that moment the result has already been sent to the client. So it's too late. EO.Pdf.HtmlToPdf.Options.AfterRenderPage provides an opportunity to plug in your own code before the file has been sent to the client.

Hope this helps. Please feel free to let us know if it works for you.

Thanks!
BrianW
Posted: Wednesday, October 23, 2013 2:40:21 PM
Rank: Newbie
Groups: Member

Joined: 10/23/2013
Posts: 2
I'm not sure about setting the event handler in the OnResultExecuted as it seems like something that should be set up before that, so I've got it hooked up in the OnActionExecuting along with setting the page size/margins and its working nicely. Thanks so much for the quick response.
eo_support
Posted: Wednesday, October 23, 2013 2:45:44 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,196
OK. Glad that it's working for you. Please feel free to let us know if you have any other questions.

Thanks!


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.