We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to Export Diagram to PDF?

Platform: ASP.NET MVC |
Control: Diagram |
Published Date: May 27, 2015 |
Last Revised Date: May 30, 2019

EJMVC

Diagram can be exported as an image file (Jpeg, png, bmp, svg…), but there is no direct way to export as PDF. However, you can embed the exported image into a PDF file by using Essential PDF. Refer to the following PDF assemblies in the web.config file along with our Diagram assemblies.

 

PDFAssembly:

Syncfusion.Pdf.Base

 

DiagramAssembly:

Syncfusion.Core

Syncfusion.EJ

Syncfusion.EJ.MVC

 

The following namespace should be referred in the application along with the Diagram namespace.

Namespace

using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf;

CSHTML

function GeneratePDF() {
  var fileType = "png";
  var fileName = "diagram";
  var region = "content";
  var diagram = $("#Diagram1").data("ejDiagram");
   var image = diagram.exportDiagram({ mode: "data"});
  $.ajax({
     url: '/Diagram/createPDF',
     type: "POST",
     dataType: 'text',
     data: { base64data: image },
     success: function (data) {                 
     window.open("../Images/" + data);
     },
     error: function (xhr) {
      }
   })
  }

 

Controller

 [AcceptVerbs(HttpVerbs.Post)]
public ActionResult createPDF()
{
  string filename = "sample.pdf";
  string filepath = null;
  if (Request.Form["base64data"] != null)
  {
   string image = Request.Form["base64data"].ToString();
   image = image.Substring(image.IndexOf(',') + 1);
   byte[] data = Convert.FromBase64String(image);
   var path = Path.Combine(Server.MapPath("~/Images"), "snapshot.png");
   // An image is generated.
   System.IO.File.WriteAllBytes(path, data); 
   Thread t = new Thread(CreateDocument);
   t.SetApartmentState(ApartmentState.STA);
   t.Start();
   t.Join();
   filepath = Path.Combine(Server.MapPath("~/Images"), "sample.pdf");
   //Saves the PDF document.
   document.Save(filepath);
   document.Close(true);                
  }
  return Content(filename);
}
public void CreateDocument()
{          
 //Creates a PDF document.
 PdfPage page = document.Pages.Add();
 PdfGraphics graphics = page.Graphics;
 string imgPath = ResolveApplicationImagePath("snapshot.png");
 graphics.DrawImage(PdfImage.FromFile(imgPath), 100, 200, 300, 300);            
}
protected string ResolveApplicationImagePath(string fileName)
{
 string dataPath = new System.IO.DirectoryInfo(Request.PhysicalApplicationPath + "\\Images").FullName;
 return string.Format("{0}\\{1}", dataPath, fileName);
}

 

Sample:http://www.syncfusion.com/downloads/support/directtrac/general/ze/PDFSample2006957137

2X faster development

The ultimate ASP.NET MVC UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon