【问题标题】:How to Create a pdf using syncfusion in c# .net如何在 c# .net 中使用 syncfusion 创建 pdf
【发布时间】:2021-09-09 12:21:23
【问题描述】:

我正在尝试使用 syncfusion 创建一个 pdf,并使用此链接上显示的示例: https://www.syncfusion.com/kb/10673/how-to-create-a-pdf-document-in-aws-lambda

我创建了一个小型控制台应用程序,但在读取带有 HELP 注释的行中的数据时遇到问题。它正在寻找路径而不是读取示例链接中显示的字符串。 任何使用过类似产品的人都可以提出修复建议吗?谢谢

using System;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Drawing;
using System.IO;
using System.Text.Json;
using Newtonsoft.Json;

namespace pdf
{
    class Program
    {
        static void Main(string[] args)
        {
       
            var result = GetData();
            var stream = new StreamReader(result); //----> How do I read this? HELP
            JsonReader reader = new JsonTextReader(stream);
            var serilizer = new Newtonsoft.Json.JsonSerializer();
            var responseText = serilizer.Deserialize(reader);

            //Convert Base64String into PDF document
            byte[] bytes = Convert.FromBase64String(responseText.ToString());
            FileStream fileStream = new FileStream("Sample.pdf", FileMode.Create);
            BinaryWriter writer = new BinaryWriter(fileStream);
            writer.Write(bytes, 0, bytes.Length);
            writer.Close();
            System.Diagnostics.Process.Start("Sample.pdf");
        }

        static string GetData()
        {
            //Create a new PDF document
            PdfDocument document = new PdfDocument();

            //Add a page to the document
            PdfPage page = document.Pages.Add();

            //Create PDF graphics for the page
            PdfGraphics graphics = page.Graphics;

            //Set the standard font
            PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20);

            //Draw the text
            graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, new PointF(0, 0));

            string imagePath = Path.GetFullPath(@"Data\logo.png");

            //Load the image from the disk
            FileStream imageStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read);

            PdfBitmap image = new PdfBitmap(imageStream);

            //Draw the image
            graphics.DrawImage(image, 30, 30, 100, 25);

            //Save the document into stream
            MemoryStream stream = new MemoryStream();

            //Save the PDF document  
            document.Save(stream);
            document.Close();
         

    
            return (Convert.ToBase64String(stream.ToArray()));
           
        }
    }
}

【问题讨论】:

    标签: c# .net .net-core


    【解决方案1】:

    通过GetData方法直接返回流并将流传递给StreamReader

    static Stream GetData()
    {
        ...
        // remove return (Convert.ToBase64String(stream.ToArray()));
        return stream;
    }
    

    【讨论】:

      猜你喜欢
      • 2021-08-30
      • 2021-06-07
      • 2011-01-01
      • 2015-04-27
      • 2021-12-05
      • 2016-04-09
      • 2012-07-03
      • 1970-01-01
      • 2019-03-22
      相关资源
      最近更新 更多