在C# .NET中使用Syncfusion创建PDF时StreamReader读取Base64字符串的问题求助
Hey there, let's break down what's going wrong here and fix it quickly.
The core issue is this: your GetData() method already returns a Base64-encoded string of the generated PDF. But you're trying to pass that string directly to StreamReader, which expects a stream (like a file or memory stream) as input—not a plain string. That's why you're hitting problems at the StreamReader(result) line.
Even worse, the extra JSON serialization/deserialization step here is totally unnecessary. You're taking a perfectly valid Base64 string, wrapping it in a JSON reader, then converting it back to a string—this can introduce formatting issues or just waste cycles.
Here's the corrected code
We can strip out the unnecessary stream reader and JSON handling steps entirely, since we already have the Base64 string we need:
using System; using Syncfusion.Pdf; using Syncfusion.Pdf.Graphics; using Syncfusion.Drawing; using System.IO; namespace pdf { class Program { static void Main(string[] args) { string pdfBase64 = GetData(); // Convert Base64 string directly to byte array byte[] pdfBytes = Convert.FromBase64String(pdfBase64); // Write bytes to a PDF file (using statements ensure proper resource cleanup) using (FileStream fileStream = new FileStream("Sample.pdf", FileMode.Create)) using (BinaryWriter writer = new BinaryWriter(fileStream)) { writer.Write(pdfBytes, 0, pdfBytes.Length); } 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 with proper stream disposal using (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 with proper cleanup using (MemoryStream stream = new MemoryStream()) { document.Save(stream); document.Close(); return Convert.ToBase64String(stream.ToArray()); } } } }
Key improvements made:
- Removed unused
System.Text.JsonandNewtonsoft.Jsonreferences since JSON handling isn't needed here - Simplified the
Mainmethod: directly use the Base64 string fromGetData()to convert to bytes - Added
usingstatements for all streams to ensure proper resource disposal, preventing memory leaks - Cleaned up image loading logic with a
usingblock for the image file stream
This should resolve your reading issue and generate the PDF correctly. The Lambda example you referenced uses streams/Base64 for cloud input/output, but in a console app, we can keep things much simpler since we're working directly with local files.
内容的提问来源于stack exchange,提问作者Megatron213

