Dear Aspose,
Hello.
I’m trying to convert PDF files into PNG by each page.
However depending on input PDF file, an error as below occurs and conversion process stops.
Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
at #=zjr9I_kCMLB35JyqolpZ05aVuiJDWBtV45w==.#=zWxbtAr4UJj6M()
at #=zjr9I_kCMLB35JyqolpZ05aVuiJDWBtV45w==.#=zdvRsufPdD57X(Int32 #=zPzmtuJHygu5K, Int32& #=zY4iO00vql6sJ)
at #=zjr9I_kCMLB35JyqolpZ05aVuiJDWBtV45w==.#=zg1dWcME=(Int32 #=zPzmtuJHygu5K)
at Aspose.Pdf.PageCollection.#=z8ptdGYBwmJRgbl0quYl63gs=(Int32 #=zyUBcjts=)
at Aspose.Pdf.PageCollection.#=z7MJAYiY=(Int32 #=zyUBcjts=)
at Aspose.Pdf.PageCollection.get_Item(Int32 index)
at ConsoleApp1.Program.Main(String[] args) in /app/src/ConsoleApp1/Program.cs:line 260
“The line 260” falls on “pngDevice.Process(pdfDocument.Pages[pageCount], imageStream);” in the part concerned of my script.
String filepath = args[0];
String filename = Path.GetFileName(args[0]);
// Make directories
Directory.CreateDirectory(args[1] + "/" + filename);
String txt_outpath = args[1] + "/" + filename + "/" + "txt";
String png_outpath = args[1] + "/" + filename + "/" + "png";
Directory.CreateDirectory(txt_outpath);
Directory.CreateDirectory(png_outpath);
// Open document
Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(filepath);
// Aspose.Pdf.Text.FolderFontSource source = new Aspose.Pdf.Text.FolderFontSource("../data/Fonts_windows");
pdfDocument.Save(args[1] + "/" + filename + "/"+ "result.pdf");
// Create Aspose.Pdf.RenderingOptions to enable font hinting
RenderingOptions opts = new RenderingOptions();
opts.UseFontHinting = true;
for (int pageCount = 1; pageCount <= pdfDocument.Pages.Count; pageCount++)
{
using (FileStream imageStream = new FileStream(png_outpath +"/"+ $"output{pageCount}.png", FileMode.Create))
{
// Create PNG device with specified attributes
// Width, Height, Resolution, Quality
// Quality [0-100], 100 is Maximum
// Create Resolution object
Resolution resolution = new Resolution(150);
PngDevice pngDevice = new PngDevice(resolution);
//// Set predefined rendering options
pngDevice.RenderingOptions = opts;
// Convert a particular page and save the image to stream
pngDevice.Process(pdfDocument.Pages[pageCount], imageStream);
// Close stream
imageStream.Close();
}
// Create TextAbsorber object to extract text
TextAbsorber absorber = new TextAbsorber();
// Accept the absorber for first page
pdfDocument.Pages[pageCount].Accept(absorber);
// Get the extracted text
string extractedText = absorber.Text;
// Create a writer and open the file
TextWriter tw = new StreamWriter(txt_outpath +"/"+ $"output{pageCount}.txt");
// Write a line of text to the file
tw.WriteLine(extractedText);
// Close the stream
tw.Close();
}
Would you please give some clue to resolve this scenario?
I attached the input PDF file.
"20070215.pdf (934.3 KB)
And I find this error in the environment Linux (Ubuntu “18.04.5 LTS (Bionic Beaver)”) + Docker as well as Windows.