We have some issues to convert pdf from base64 to pdf, in our case we convert Pdf that was generated by spire everything good we convert it to base64 as well good, but how can we get back the pdf from base64 ?
PDF --> Base64 --> PDF
Thanks
//1.PDF --> Base64String
FileStream ReadPDF = new FileStream(@"sample.pdf", FileMode.Open);
byte[] Buffer = new byte[(int)ReadPDF.Length];
ReadPDF.Read(Buffer, 0, (int)ReadPDF.Length);
ReadPDF.Close();
string base64String = Convert.ToBase64String(Buffer);
//2.Base64String --> PDF
MemoryStream ms = new MemoryStream(Convert.FromBase64String(base64String));
PdfDocument doc = new PdfDocument();
doc.LoadFromStream(ms);
doc.SaveToFile("output.pdf");public static byte[] ToSignedPDF(this byte[] input)
{
try
{
MemoryStream memoryStream = new MemoryStream();
X509Store x509Store = new X509Store(StoreLocation.CurrentUser);
x509Store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection x509Certificate2Collection = x509Store.Certificates.Find(X509FindType.FindByIssuerName, "COMP1", false);
X509Certificate2 x509Certificate2 = x509Certificate2Collection[0];
Org.BouncyCastle.X509.X509CertificateParser x509CertificateParser = new Org.BouncyCastle.X509.X509CertificateParser();
Org.BouncyCastle.X509.X509Certificate[] chain = new Org.BouncyCastle.X509.X509Certificate[] { x509CertificateParser.ReadCertificate(x509Certificate2.RawData) };
IExternalSignature externalSignature = new X509Certificate2Signature(x509Certificate2, "SHA-1");
PdfReader pdfReader = new PdfReader(input);
PdfStamper pdfStamper = PdfStamper.CreateSignature(pdfReader, memoryStream, '\0');
PdfSignatureAppearance signatureAppearance = pdfStamper.SignatureAppearance;
signatureAppearance.SignatureRenderingMode = PdfSignatureAppearance.RenderingMode.GRAPHIC_AND_DESCRIPTION;
MakeSignature.SignDetached(signatureAppearance, externalSignature, chain, null, null, null, 0, CryptoStandard.CMS);
return memoryStream.ToArray();
}
catch
{
throw;
} public static byte[] ToPDF(this List<byte[]> input)
{
try
{
MemoryStream memoryStream = new MemoryStream();
using (PdfDocument document = new PdfDocument())
{
foreach (byte[] element in input)
{
MemoryStream imageMemoryStream = new MemoryStream(element);
PdfPageBase page = document.Pages.Add();
Spire.Pdf.Graphics.PdfImage image = Spire.Pdf.Graphics.PdfImage.FromStream(imageMemoryStream);
//page.PageInfo.Margin.Bottom = 0;
//page.PageInfo.Margin.Top = 0;
//page.PageInfo.Margin.Left = 0;
//page.PageInfo.Margin.Right = 0;
//page.PageInfo.IsLandscape = false;
float widthFitRate = image.PhysicalDimension.Width / page.Canvas.ClientSize.Width;
float heightFitRate = image.PhysicalDimension.Height / page.Canvas.ClientSize.Height;
float fitRate = Math.Max(widthFitRate, heightFitRate);
float fitWidth = image.PhysicalDimension.Width / fitRate;
float fitHeight = image.PhysicalDimension.Height / fitRate;
page.Canvas.DrawImage(image, 0, 0, fitWidth, fitHeight);
}
//document.PDFStandard.CompressionLevel(new Document.ConvertOptions()
//{
// LinkDuplcateStreams = true,
// RemoveUnusedObjects = true,
// RemoveUnusedStreams = true,
// CompressImages = true //IMP
//});
document.SaveToStream(memoryStream);
}
return memoryStream.ToArray();
}
catch
{
throw;
}
}