How to merge two Pdfs into Base64 in one

Asked

Viewed 507 times

-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?

  • The question statement talks about mending two strings, while the body talks about mending two Pdfs. I’m confused :)

  • 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.

3 answers

1

1


I believe that the library you are using is complicated... I recommend the use of Pdfsharp

The process I would make would be the following:

  • Read the document in memory
static PdfDocument Base64ToStream(string base64)
{
    using(var stream = new MemoryStream(Convert.FromBase64String(base64)))
    return PdfReader.Open(stream, PdfDocumentOpenMode.Import);
}
  • Merge pages of pdfs:
using (PdfDocument one = Base64ToStream(b64pdf1))
using (PdfDocument two = Base64ToStream(b64pdf2))
using (PdfDocument outPdf = new PdfDocument())
{                
    CopyPages(one, outPdf);
    CopyPages(two, outPdf);

    Console.WriteLine(PdfDocumentToBase64(outPdf))
}

void CopyPages(PdfDocument from, PdfDocument to)
{
    for (int i = 0; i < from.PageCount; i++)
    {
        to.AddPage(from.Pages[i]);
    }
}
  • And then convert the PDF to Base64:
static string PdfDocumentToBase64(PdfDocument pdf)
{
   using (var sm = new MemoryStream())
    {
      pdf.SaveToStream(sm);
      var bytes = sm.ToArray();
      return Convert.ToBase64String(bytes);
    }
}

-3

string.Join("", listaDeStrings);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.