Calling Aspose.Pdf.Document.Save causes generated images not to have text

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
    GitHub - zoldello/Aspose.Pdf.Document.SaveIssueDemo
  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,


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.

…<o:p></o:p>

pdfDocument.Save(dummypath);

pdfDocument = new Document(testFile);

....


Please feel free to contact us for any further assistance.

Best Regards,

Hi Phil,


Thanks for contacting support.

I have tested the scenario using latest release of Aspose.Pdf for .NET 11.3.0 and I am unable to notice any issue. As per my observations, the text is properly appearing in resultant files. For your reference, I have also attached the resultant images generated over my end. Can you please try using the latest release and in case you still face the same problem, please share some details regarding your working environment. We are sorry for this inconvenience.

[C#]

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,


Thanks for your inquriy. I am afraid it is by design to close related objects at Save(). However please share some details about your requirement to save PDF document right after loading without any change, so we can guide you accordingly.

Best Regards,