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 Feb 24, 2021 1:58 pm

Hi, I'm trying to convert a pdf to an image, but it fails pretty fast. I'm using your code from the tutorial:
System.Drawing.Image img = null;
byte[] file = new byte[File.Length];
Buffer.BlockCopy(File, 0, file, 0, file.Length);
CultureInfo cc = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
Spire.Pdf.PdfDocument pdfDocument1 = new Spire.Pdf.PdfDocument();
pdfDocument1.LoadFromFile(@"sample.pdf");
img = pdfDocument1.SaveAsImage(0, Spire.Pdf.Graphics.PdfImageType.Metafile);
Thread.CurrentThread.CurrentCulture = cc;


Saving page #1 as Image file error
Last edited by habibullina_af on Thu Feb 25, 2021 6:50 am, edited 1 time in total.

habibullina_af
 
Posts: 3
Joined: Wed Feb 24, 2021 1:54 pm

Thu Feb 25, 2021 3:33 am

Hello,

Thanks for your inquiry!

I created a PDF file and save it as an image file by the code you provided with the latest Spire.PDF V7.2.9 but did not reproduce your issue. If you were not using the latest version, I recommend you have a try first. If the issue still exists, please provide us with your input file for further investigation. Thanks in advance!

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Thu Feb 25, 2021 6:49 am

[quote="Marcia.Zhou"]Hello,

Thanks, i used last version, but i still have error:
at System.Number.ParseSingle(String value, NumberStyles options, NumberFormatInfo numfmt)
at sprᤌ.ᜁ(String[] A_0, Boolean A_1)
at sprᤌ.ᜀ(sprᥲ A_0)
at sprᤌ.᜻()
at sprᤌ.ᜍ(Boolean A_0)
at spr⯰.ᜀ(Graphics A_0, sprᤃ A_1, sprᥱ A_2, sprᥳ A_3, Image A_4)
at spr⯰.ᜂ(Int32 A_0, Int32 A_1, Int32 A_2)
at Spire.Pdf.PdfDocumentBase.ᜂ(Int32 A_0)
at Spire.Pdf.PdfDocumentBase.ᜀ(Int32 A_0, PdfImageType A_1)

habibullina_af
 
Posts: 3
Joined: Wed Feb 24, 2021 1:54 pm

Thu Feb 25, 2021 7:52 am

Hello,

Thanks for your feedback!

This error you encountered is mostly related to the data in the document, your input file is very important for us to investigate your issue. To protect your privacy, you can send your input file to us via email ([email protected]). Thanks in advance.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Thu Feb 25, 2021 8:29 am

Thanks for your feedback!

I sent the document to you by mail

habibullina_af
 
Posts: 3
Joined: Wed Feb 24, 2021 1:54 pm

Thu Feb 25, 2021 8:43 am

Hello,

I have received your email, and thanks for sharing more information!

I have reproduced your issue with the PDF you provided and logged it in our issue tracking system with the ticket SPIREPDF-4070 for further investigation.

We will let you know if there is any update. Sorry for the inconvenience caused.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Fri Mar 12, 2021 3:57 am

Hello,

Thanks for your patience.

Glad to inform you that we just released Spire.PDF Pack(Hot Fix) Version:7.3.1 which fixes the issue that the application threw System.FormatException when converting PDF to Image.

Please download the fix version from the following links to test.
Website link: https://www.e-iceblue.com/Download/download-pdf-for-net-now.html
Nuget link:https://www.nuget.org/packages/Spire.PDF/7.3.1

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Mon Mar 22, 2021 8:04 am

Hello,

Hope you are doing well!

Has the issue been solved now? Could you please give us some feedback at your convenience?

Thanks in advance.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Tue Apr 08, 2025 10:19 am

Hello,

I'm facing a similar issue with Spire PDF Pro Edition 10.6.
From time to time (with the same PDF file, then same base64 value) I get an exception System.FormatException ("Invalid length for a Base 64 character array or string.", while the length is OK) on the SaveAsImage method, while the PDF is still the same. The issue happens with any PDF a use.
And sometimes it works again....


public static ImageSource ConvertPdfBase64ToImageSource(string base64Pdf)
{
try
{
if (string.IsNullOrWhiteSpace(base64Pdf) || base64Pdf.Length % 4 != 0)
{
throw new ArgumentException("La chaîne Base64 est invalide ou vide.");
}
if (base64Pdf.StartsWith("data:application/pdf;base64,"))
{
base64Pdf = base64Pdf.Substring("data:application/pdf;base64,".Length);
}

byte[] pdfBytes = System.Convert.FromBase64String(base64Pdf);


using (MemoryStream pdfStream = new MemoryStream(pdfBytes))
{
using (Spire.Pdf.PdfDocument pdfDoc = new Spire.Pdf.PdfDocument(pdfStream))
{

using (System.Drawing.Image img = pdfDoc.SaveAsImage(0, Spire.Pdf.Graphics.PdfImageType.Bitmap))
{
return ConvertBitmapToImageSource((Bitmap)img);
}
}
}
}
catch
{
return null;
}
}

Any thought ?

fpernisek
 
Posts: 15
Joined: Wed Jun 15, 2016 7:02 am

Wed Apr 09, 2025 1:52 am

Hello,

Thanks for your inquiry.
I assume the problem is caused by the Base64 string, have you tried loading the pdf file directly? Or can you provide us with a simplified code that reproduces your problem?

Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 757
Joined: Mon Dec 27, 2021 2:23 am

Thu Jul 03, 2025 10:40 am

Here is my code :

public static ImageSource ConvertPdfBase64ToImageSource(string base64Pdf)
{
try
{
if (string.IsNullOrWhiteSpace(base64Pdf) || base64Pdf.Length % 4 != 0)
{
throw new ArgumentException("La chaîne Base64 est invalide ou vide.");
}
if (base64Pdf.StartsWith("data:application/pdf;base64,"))
{
base64Pdf = base64Pdf.Substring("data:application/pdf;base64,".Length);
}

byte[] pdfBytes = System.Convert.FromBase64String(base64Pdf);


using (MemoryStream pdfStream = new MemoryStream(pdfBytes))
{
using (Spire.Pdf.PdfDocument pdfDoc = new Spire.Pdf.PdfDocument(pdfStream))
{

using (System.Drawing.Image img = pdfDoc.SaveAsImage(0, Spire.Pdf.Graphics.PdfImageType.Bitmap))
{
return ConvertBitmapToImageSource((Bitmap)img);
}
}
}
}
catch
{
return null;
}
}

public static ImageSource ConvertImageBase64ToImageSource(string base64Image)
{
byte[] imageBytes = System.Convert.FromBase64String(base64Image);
using (MemoryStream ms = new MemoryStream(imageBytes))
{
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = ms;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
return bitmapImage;
}
}


The exception is raised on pdfDoc.SaveAsImage(), saying that the length is not correct for a base 64 array of char or String, while the base64Pdf value is correct.

fpernisek
 
Posts: 15
Joined: Wed Jun 15, 2016 7:02 am

Fri Jul 04, 2025 9:08 am

Hello,

Thansk for your feedback.
Regarding your issue, I suspect it might be caused by extra or missing characters in the PDF base64 string itself. I recommend double-checking it. Here I simplified your code and did not reproduce your issue after testing. Please run this code on your side and confirm whether the issue exists. Looking forward to your feedback.
Code: Select all
 // 1. Read PDF file as byte array
 string pdfPath = @"CA00001\CA00001.pdf";
 byte[] pdfBytes = File.ReadAllBytes(pdfPath);

 // 2. Convert PDF bytes to Base64 string (optional step)
 string base64String = Convert.ToBase64String(pdfBytes);

 // 3. Convert Base64 back to byte array
 byte[] decodedPdfBytes = Convert.FromBase64String(base64String);

 // 4. Create PDF document from memory stream
 using (MemoryStream pdfStream = new MemoryStream(decodedPdfBytes))
 using (Spire.Pdf.PdfDocument pdfDoc = new Spire.Pdf.PdfDocument(pdfStream))
 {
     // 5. Convert first page to bitmap image
     using (System.Drawing.Image img = pdfDoc.SaveAsImage(0,Spire.Pdf.Graphics.PdfImageType.Bitmap))
     {
         // 6. Save as PNG file
         string outputPath = "res.png";
         img.Save(outputPath, System.Drawing.Imaging.ImageFormat.Png);
     }
 }
Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 757
Joined: Mon Dec 27, 2021 2:23 am

Return to Spire.PDF

cron