Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Wed Jun 13, 2018 2:59 pm

Hello,

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 :) .

walid.rezzouqui
 
Posts: 4
Joined: Wed Jun 13, 2018 2:52 pm

Thu Jun 14, 2018 2:59 am

Hello,

Thank you for your inquiry.
Please upgrade to the latest hotfix(Spire.PDF Pack(Hot Fix) Version:4.6.1) and refer to the code below to accomplish your task.
Code: Select all
//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");


Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Wed Jun 27, 2018 8:42 am

Hello, issue solved thanks,

We just wanna know if its normal that the quality decrease in conversion ?

because it can cause some problèms.

walid.rezzouqui
 
Posts: 4
Joined: Wed Jun 13, 2018 2:52 pm

Wed Jun 27, 2018 8:51 am

Hello,

Thank you for your response.
Generally, the quality would not decrease.
If the quality got decreased in your case, please share the original document and the output file.

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Wed Jun 27, 2018 10:44 am

Hello,

path that the file take is :

To database:
upload from web as PDF
Convert pdf to base64 and save it

From database:
Getting base64 file
Convert it to Pdf (base64 to pdf) and save it as stream ( document.SaveToStream(memoryStream); )

walid.rezzouqui
 
Posts: 4
Joined: Wed Jun 13, 2018 2:52 pm

Thu Jun 28, 2018 6:05 am

Hello,

Thank you for sharing the details.
After an initial test with (Spire.PDF Pack(Hot Fix) Version:4.6.8), I did not reproduce the issue you mentioned. Attached is the output file on my side.
I have noticed that the output document you provided was not the same as the original one, you have added the signature. Did you do that using Spire.PDF? If so, please share the complete code related to Spire. Besides, the low-quality issue could also be caused by an incorrect base64 string. Please check twice on your side. See if the base64 string you get from the database is the same as the previous one.

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Fri Jun 29, 2018 10:06 am

Hello,
Signature methode:
Code: Select all
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;
            }


For modification that we made to our document, we add few things:
- stamp
- Signature
- Timbre


For base64 string that we made after those modification is the same that we get for "to pdf converting"

And from base64 to pdf we use:

Code: Select all
        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;
            }
        }


Thanks for your help!

walid.rezzouqui
 
Posts: 4
Joined: Wed Jun 13, 2018 2:52 pm

Fri Jun 29, 2018 10:16 am

Hello,

Thank you for your response.
I have noticed that you were not using our product to sign the pdf. Since the conversion works well with the original template, the issue should be caused by other operations. Have you ever checked the file after adding the signature? Did it become fuzzy? Please save the pdf file directly after signing and send it to us.

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Tue Apr 19, 2022 1:09 pm

public IActionResult Index()
{
PdfDocument doc = new PdfDocument();
var base64s = "Base64PdfFile ";
byte[] newBytes = Convert.FromBase64String(base64s);
doc.LoadFromBytes(newBytes);

//doc.LoadFromFile("Test1.pdf");
PdfCertificate cert = new PdfCertificate("emudra.pfx", "emudhra");
int PageNumber = doc.Pages.Count-1;
var signature = new PdfSignature(doc, doc.Pages[PageNumber], cert, "Requestd1");
signature.SignDetailsFont = new PdfFont(PdfFontFamily.Helvetica, 11f);

/* if (ApproverType == "1")
{
signature.Bounds = new RectangleF(new PointF(420, 450), new SizeF(260, 90));
}
else
{*/
signature.Bounds = new RectangleF(new PointF(420, 550), new SizeF(260, 90));
/*}*/
//signature.Bounds = new RectangleF(new PointF(370, 690), new SizeF(200, 50));
signature.IsTag = true;
signature.DigitalSignerLable = "Digitally signed by : ";
// signature.DigitalSigner = "NIKHIL SOHONI ";
signature.DigitalSigner = "RANE SWATI";

//signature.DistinguishedName = "DN: ";
//signature.LocationInfoLabel = "Location: ";
//signature.LocationInfo = " Banglore2";

//signature.ReasonLabel = "Reason: ";
//signature.Reason = "This is Signed for Bank Statement";

signature.DateLabel = "Date: ";
signature.Date = DateTime.Now;

//signature.ContactInfoLabel = "Contact: ";
//signature.ContactInfo = "123456789";

signature.Certificated = false;
var aa= signature.DocumentPermissions = PdfCertificationFlags.ForbidChanges;
string base64String = Convert.ToBase64String(newBytes);
// doc.SaveToFile("D:/digitalSignature/SpirePDF/Spire/Spire/Test3.pdf");
return Json(new { file = base64String });
// return View();
}

prasadbabu
 
Posts: 1
Joined: Tue Apr 19, 2022 12:56 pm

Wed Apr 20, 2022 6:01 am

Hello prasadbabu,

Are you having an issue with our Spire.PDF?
I simulated a PDF file and tested it with the code you provided, but didn't find any issues. If you are having an issue, please provide more detailed instructions and provide your input files (if any) for us to investigate further. You could attach them here or send them to us via email (support@e-iceblue.com). Thanks in advance.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1648
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.PDF