在 ASP.NET Core 中使用 iTextSharp 生成 PDF 格式的图表、图形
Generate charts, graphs in PDF using iTextSharp in ASP.NET Core
我需要在 ASP.NET Core 3.1 的服务器端生成 PDF 文档。我正在使用 iTextSharp 生成 PDF。我能够使用 iTextSharp 创建 PDF 格式的数据表。但是,我无法找到使用同一库将图表或图形添加到 PDF 的解决方案。
我使用过 ChartJS 和其他类似的包,但是 none 它们让我可以在后端创建图表并将其创建为要嵌入 PDF 中的图像。
2020 年 8 月 12 日更新
创建 excel
然后转换为 pdf。
Essential XlsIO
是原生 .NET class 库,可用于创建和修改 Microsoft Excel 文件。
XlsIO 允许您将整个工作簿或单个工作表转换为 PDF 文档。
[Route("/chart")]
public IActionResult CreateChart()
{
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet sheet = workbook.Worksheets[0];
//Inserts the sample data for the chart
sheet.Range["A1"].Text = "Month";
sheet.Range["B1"].Text = "Product A";
sheet.Range["C1"].Text = "Product B";
//Months
sheet.Range["A2"].Text = "Jan";
sheet.Range["A3"].Text = "Feb";
sheet.Range["A4"].Text = "Mar";
sheet.Range["A5"].Text = "Apr";
sheet.Range["A6"].Text = "May";
//Create a random Data
Random r = new Random();
for (int i = 2; i <= 6; i++)
{
for (int j = 2; j <= 3; j++)
{
sheet.Range[i, j].Number = r.Next(0, 500);
}
}
IChartShape chart = sheet.Charts.Add();
//Set chart type
chart.ChartType = ExcelChartType.Line;
//Set Chart Title
chart.ChartTitle = "Product Sales comparison";
//Set first serie
IChartSerie productA = chart.Series.Add("ProductA");
productA.Values = sheet.Range["B2:B6"];
productA.CategoryLabels = sheet.Range["A2:A6"];
//Set second serie
IChartSerie productB = chart.Series.Add("ProductB");
productB.Values = sheet.Range["C2:C6"];
productB.CategoryLabels = sheet.Range["A2:A6"];
//Saving the workbook as stream
//FileStream stream = new FileStream("Chart.xlsx", FileMode.Create, FileAccess.ReadWrite);
//workbook.SaveAs(stream);
//Initialize XlsIO renderer.
XlsIORenderer renderer = new XlsIORenderer();
//Convert Excel document into PDF document
Syncfusion.Pdf.PdfDocument pdfDocument = renderer.ConvertToPDF(sheet);
MemoryStream stream = new MemoryStream();
pdfDocument.Save(stream);
stream.Flush(); //Always catches me out
stream.Position = 0; //Not sure if this is required
//stream.Dispose();
return File(stream, "application/pdf", "chart.pdf");
}
}
Generate PDF with Image by using iTextsharp in ASPNET Core 3.1
这里是action
的代码。
[Route("/pdf")]
public FileStreamResult GeneratePDFwithImage()
{
var imagepath = System.IO.Path.Combine(_env.WebRootPath, "/images") + "/test.png";
Document doc = new Document();
MemoryStream stream = new MemoryStream();
PdfWriter pdfWriter = PdfWriter.GetInstance(doc, stream);
pdfWriter.CloseStream = false;
doc.Open();
doc.Add(new Paragraph("Hello World"));
Image png = Image.GetInstance(imagepath);
doc.Add(png);
doc.Close();
stream.Flush(); //Always catches me out
stream.Position = 0; //Not sure if this is required
return File(stream, "application/pdf", "HelloWorld.pdf");
}
Test of result
我需要在 ASP.NET Core 3.1 的服务器端生成 PDF 文档。我正在使用 iTextSharp 生成 PDF。我能够使用 iTextSharp 创建 PDF 格式的数据表。但是,我无法找到使用同一库将图表或图形添加到 PDF 的解决方案。
我使用过 ChartJS 和其他类似的包,但是 none 它们让我可以在后端创建图表并将其创建为要嵌入 PDF 中的图像。
2020 年 8 月 12 日更新
创建 excel
然后转换为 pdf。
Essential XlsIO
是原生 .NET class 库,可用于创建和修改 Microsoft Excel 文件。
[Route("/chart")]
public IActionResult CreateChart()
{
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet sheet = workbook.Worksheets[0];
//Inserts the sample data for the chart
sheet.Range["A1"].Text = "Month";
sheet.Range["B1"].Text = "Product A";
sheet.Range["C1"].Text = "Product B";
//Months
sheet.Range["A2"].Text = "Jan";
sheet.Range["A3"].Text = "Feb";
sheet.Range["A4"].Text = "Mar";
sheet.Range["A5"].Text = "Apr";
sheet.Range["A6"].Text = "May";
//Create a random Data
Random r = new Random();
for (int i = 2; i <= 6; i++)
{
for (int j = 2; j <= 3; j++)
{
sheet.Range[i, j].Number = r.Next(0, 500);
}
}
IChartShape chart = sheet.Charts.Add();
//Set chart type
chart.ChartType = ExcelChartType.Line;
//Set Chart Title
chart.ChartTitle = "Product Sales comparison";
//Set first serie
IChartSerie productA = chart.Series.Add("ProductA");
productA.Values = sheet.Range["B2:B6"];
productA.CategoryLabels = sheet.Range["A2:A6"];
//Set second serie
IChartSerie productB = chart.Series.Add("ProductB");
productB.Values = sheet.Range["C2:C6"];
productB.CategoryLabels = sheet.Range["A2:A6"];
//Saving the workbook as stream
//FileStream stream = new FileStream("Chart.xlsx", FileMode.Create, FileAccess.ReadWrite);
//workbook.SaveAs(stream);
//Initialize XlsIO renderer.
XlsIORenderer renderer = new XlsIORenderer();
//Convert Excel document into PDF document
Syncfusion.Pdf.PdfDocument pdfDocument = renderer.ConvertToPDF(sheet);
MemoryStream stream = new MemoryStream();
pdfDocument.Save(stream);
stream.Flush(); //Always catches me out
stream.Position = 0; //Not sure if this is required
//stream.Dispose();
return File(stream, "application/pdf", "chart.pdf");
}
}
Generate PDF with Image by using iTextsharp in ASPNET Core 3.1
这里是action
的代码。
[Route("/pdf")]
public FileStreamResult GeneratePDFwithImage()
{
var imagepath = System.IO.Path.Combine(_env.WebRootPath, "/images") + "/test.png";
Document doc = new Document();
MemoryStream stream = new MemoryStream();
PdfWriter pdfWriter = PdfWriter.GetInstance(doc, stream);
pdfWriter.CloseStream = false;
doc.Open();
doc.Add(new Paragraph("Hello World"));
Image png = Image.GetInstance(imagepath);
doc.Add(png);
doc.Close();
stream.Flush(); //Always catches me out
stream.Position = 0; //Not sure if this is required
return File(stream, "application/pdf", "HelloWorld.pdf");
}
Test of result