Nidhin Paul
Nidhin Paul

Reputation: 93

PDF file is not get generating properly in C# .net core project from MemoryStream

I have below code to create a pdf file

To read contents i used

String path = System.IO.Directory.GetCurrentDirectory()
                        +Path.DirectorySeparatorChar.ToString() + "Templates"
                          + Path.DirectorySeparatorChar.ToString() + 
                             "CoverLetter.docx";

string content = System.IO.File.ReadAllText(path);
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
writer.WriteLine(content);
writer.Flush();
                        
stream.Position = 0; 

And then to write content i used

using (FileStream file = new FileStream(@"C:\Users\nithinpaul\Documents\Testing\test.pdf", 
FileMode.CreateNew, FileAccess.Write))
                    {
                        byte[] content1 = stream.ToArray();
                        file.Write(content1, 0, (int)content1.Length);

                        //byte[] bytes = new byte[stream.Length];
                        //stream.Read(bytes, 0, (int)stream.Length);
                        //file.Write(bytes, 0, bytes.Length);
                        stream.Close();
                    }

Pdf is generated in my local disk but when i try to open it its showing like something went wrong. and when i try to open it using notepad the content look like below. Any idea?

enter image description here

And in my .docx i have some texts like this

Application Number – {{AppNumber}}
First Name: {{FirstName}}
Contact Number: {{ContactNumber}}
Email Id: {{EmailId}}

Upvotes: 0

Views: 879

Answers (1)

Ryan Thomas
Ryan Thomas

Reputation: 1982

From our discussion in the comments, what I believe you are trying to achieve is the following.

  1. Open a Word Document
  2. Replace some values in said word document
  3. Save the document as a PDF file.

To do this we will make use of a Nuget Package called FreeSpire.Doc, please note there are a number of alternative packages that will do the same thing and I suggest you do some research into which suits your requirements the most as free versions have limitations. You also probably want to avoid having a dependency on Microsoft Office.

Anyway, this code will achieve what you want with FreeSpire.Doc

//Open the template document    
var doc = new Document("PathToTemplate.docx");
//Replace the values
doc.Replace("{{AppNumber}}", "AppNumberValue", true, true);
doc.Replace("{{FirstName}}", "FirstNameValue", true, true);
doc.Replace("{{ContactNumber}}", "ContactNumberValue", true, true);
doc.Replace("{{EmailId}}", "EmailIdValue", true, true);
//Save the document as a PDF file.
doc.SaveToFile("templateConverted.pdf", FileFormat.PDF);

Don't forget the using statement for the Nuget package.

using Spire.Doc;

You can also use the SaveToStream method to save the document to a Stream instead of a File. The Document also supports a constructor that will accept a Steam.

Upvotes: 1

Related Questions