Articles in this section
Category / Section

How to Export Diagram to PDF?

2 mins read

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

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied