[SOLVED] How to convert XML to Byte to File/PDF

Issue

I have successfully done "find and replace" which created an xml. Now I want to convert the newly created xml file to pdf which will be attached as a file and sent in a mail.

The result of the Base64String was tested on a base64 pdf file converter but the pdf cannot be opened. Got this error: Something went wrong couldn’t open the file

HOW CAN I MAKE THIS WORK?

        public async Task<string> CreateDocument(string PolicyNumber)
        {
            var policy = await _context.Policy.SingleOrDefaultAsync(p => p.PolicyNumber == PolicyNumber);

            ArgumentNullException.ThrowIfNull(policy, "Policy Not Available");
            //CreatePolicyDocument

            //create policy document
            var files = @"C:\Users\PATHTODOCUMENT\holderTest.docx";

            using (WordprocessingDocument wordDoc =  WordprocessingDocument.Open(files, true))
            {

                string docText;
                using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
                {
                    docText = sr.ReadToEnd();
                }
                Regex regexText = new Regex("XCONCLUSION_DATEX");
                var newWordText = regexText.Replace(docText, "Hi Everyone!");

                using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
                {
                    sw.Write(newWordText);


                    Encoding encoding = Encoding.UTF8;
                    byte[] docAsBytes = encoding.GetBytes(newWordText);
                    File.WriteAllBytes("hello.pdf", docAsBytes);
                    var file = Convert.ToBase64String(docAsBytes);
                 
                }


            }

            //send message
            //
            return "";
        }

Solution

PDF file has its own construct,so you can’t generate a pdf file with the contentstring of xml file dirctly.

If you really want to convert xml contentstring to PDF,you could try iTextSharp.
enter image description here

I tried with a simple demo,and here’s the code:

[HttpPost]
        public IActionResult XMLtoPDF([FromForm] Try T1)
        {            
            var streamContent = new StreamContent(T1.file.OpenReadStream());
            var filestring = streamContent.ReadAsStringAsync().Result;
            MemoryStream outputStream = new MemoryStream();
            Document doc = new Document();
            PdfWriter writer = PdfWriter.GetInstance(doc, outputStream);
            //PDF settings
            PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ, 0, doc.PageSize.Height, 1f);            
            doc.Open();            
            var paragraph = new Paragraph(filestring);
            doc.Add(paragraph);
            doc.Close();
            outputStream.Close();
            var pdfArray = outputStream.ToArray();          
            return File(pdfArray, "application/pdf");
        }

Result:
enter image description here

Answered By – Ruikai Feng

Answer Checked By – Willingham (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *