-3
I have the following scenario:
I receive two files encoded as Base64, each representing a pdf. I need to read those files and turn them into a single encoded file like Base64.
Input example:
PDF1 = Base64 Sgvsbg8=
PDF2 = Base64 V29ybgq=
Expected exit:
PDF3 representing the union of PDF1 and PDF2 in Base64
I tried to use the package PdfHandler, but I’m not able to use in VS Code, follow what I tried:
    public static class PdfHandler
    {
        public static string MergeBase64PdfFiles(IEnumerable<string> base64Files)
        {
            if (base64Files.Count() == 0)
                return null;
    
                if (base64Files.Count() == 1)
                    return base64Files.First();
    
                using (var pdfFinal = new PdfDocument())
                {
                    foreach(var base64File in base64Files)
                        using (PdfDocument pdf = Base64ToStream(base64File))
                            CopyPages(pdf, pdfFinal);
    
                    return PdfDocumentToBase64(pdfFinal);
                }
            }
    
            static PdfDocument Base64ToStream(string base64)
            {
                using(var stream = new MemoryStream(Convert.FromBase64String(base64)))
                    return PdfReader.Open(stream, PdfDocumentOpenMode.Import);
            }
    
            static string PdfDocumentToBase64(PdfDocument pdf)
            {
                using (var sm = new MemoryStream())
                {
                    pdf.Save(sm);
                    var bytes = sm.ToArray();
                    return Convert.ToBase64String(bytes);
                }
            }
    
            static void CopyPages(PdfDocument from, PdfDocument to)
            {
                for (int i = 0; i < from.PageCount; i++)
                    to.AddPage(from.Pages[i]);
            }
        }
    }
There’s already a package for that?
– novic
The question statement talks about mending two strings, while the body talks about mending two Pdfs. I’m confused :)
– epx
Yes @epx, in the statement the two pdfs are converted into string Base64. What I need is to join the pdfs and keep them in string base 64.
– tssantana