When a pdfDocument (Aspose.Pdf.Document object) has it “Save” method called; then the pdf is converted to an image, the image does not have text in the same cases.
Here are two formats of demo code
1. Github link to complete solution
https://github.com/zoldello/Aspose.Pdf.Document.SaveIssueDemo/tree/master
2. Code itself
//////////////
/Add license/
// var licence = new Aspose.Pdf.License();
//licence.SetLicense(Add YOUR LICENCE HERE);
var imageWidth = 77;
var imageHeight = 109;
var resolution = new Aspose.Pdf.Devices.Resolution(300);
var imageDevice = new Aspose.Pdf.Devices.PngDevice(imageWidth, imageHeight, resolution);
var path = Path.GetDirectoryName(Application.ExecutablePath).Replace(“bin\Debug”, string.Empty);
var testFile = path + @“TestImages\Superheros.pdf”;
var imageLocation = path + “GeneratedImages\Superheros.png”;
var dummypath = path + “DummyFolder\Superheroes.pdf”;
var pdfDocument = new Aspose.Pdf.Document(testFile);
imageDevice.RenderingOptions.SystemFontsNativeRendering = true;
/Having this line results in “GeneratedImages\Superheros.png”; being empty. Not having it makes the said png have the text from the source-pdf
/
pdfDocument.Save(dummypath);
if (imageDevice != null) {
using (var imageStream = new FileStream(imageLocation, FileMode.OpenOrCreate))
{
imageDevice.Process(pdfDocument.Pages[1], imageStream);
imageStream.Close();
}
}
}
/////////////////
I attached sample files with the error
Hi Phil,
…<o:p></o:p>
pdfDocument.Save(dummypath);
pdfDocument = new Document(testFile);
....
Hi Phil,
var imageWidth = 300;
var imageHeight = 300;
var resolution = new Aspose.Pdf.Devices.Resolution(300);
var imageDevice = new Aspose.Pdf.Devices.PngDevice(imageWidth, imageHeight, resolution);
var path = Path.GetDirectoryName(Application.ExecutablePath).Replace("bin\\Debug", string.Empty);
var testFile = @"c:/pdftest/CV_Test_2.pdf";
var imageLocation = "c:/pdftest/Superheros.png";
// var dummypath = path + "DummyFolder\\Superheroes.pdf";
var pdfDocument = new Aspose.Pdf.Document(testFile);
imageDevice.RenderingOptions.SystemFontsNativeRendering = true;
// pdfDocument.Save(dummypath);
if (imageDevice != null)
{
for (int counter = 1; counter < doc.Pages.Count; counter++)
{
using (var imageStream = new FileStream("c:/pdftest/CV_Test_2_" + counter+".png", FileMode.OpenOrCreate))
{
imageDevice.Process(pdfDocument.Pages[counter], imageStream);
imageStream.Close();
}
}
}
Is this a workaround to a bug? Its seems counter-intuitive to have to reinitialize after saving.
tilal.ahmad:
Hi Phil,Thanks for your inquiry. Please note Save() method close some related objects, so you need to re-initialize the document object as following for further processing.....
pdfDocument.Save(dummypath);
pdfDocument = new Document(testFile);
....
Please feel free to contact us for any further assistance.Best Regards,
Is this a workaround to a bug? Its seems counter-intuitive to have to reinitialize after saving.
Hi Phil,