Convert each PDF page to image

Hi,

i have the requirement that a user must not be able to select a text inside the pdf document. So i thought the best way to achieve this is to convert each page inside the pdf document into an image.

I already have a working code for that functionality but unfortunately the conversion from each page into an image is extremely slow.

Does anybody know a better / faster solution to transform each pdf page into an image ?

For example converting a pdf document with > 200 pages takes more than half an hour

Here is my code:

final Resolution resolution = new Resolution(200);
		final JpegDevice jpegDevice = new JpegDevice(resolution);

		// 4. Wandle jede Seite in eine Bilddatei um und füge diese in das end Dokument ein
		start = System.currentTimeMillis();
		final Document resultDocument = new Document();
		for (int i = 1; i <= asposeDocument.getPages().size(); i++)
		{
			final Page sourcePage = asposeDocument.getPages().get_Item(i);
			final Page targetPage = resultDocument.getPages().add();

			try (ByteArrayOutputStream baos = new ByteArrayOutputStream())
			{
				jpegDevice.process(sourcePage, baos);

				try (ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()))
				{
					targetPage.addImage(bais, new com.aspose.pdf.Rectangle(0, targetPage.getPageInfo().getHeight(),
							targetPage.getPageInfo().getWidth(), 0));
				}
			}
		}
		log.info("Converting pdf page to image took: " + (System.currentTimeMillis() - start) + " ms");

@zesman2

Could you please share your input PDF file here for testing? We will investigate the issue and provide you more information on it.

Sorry, here is the PDF i am using

@zesman2

We have tested the scenario using the latest version of Aspose.PDF for Java 22.8 and it takes around four minutes for conversion. So, please use Aspose.PDF for Java 22.8.

However, we have logged this problem in our issue tracking system as PDFJAVA-41968. We will inform you via this forum thread once there is an update available on it. We apologize for your inconvenience.

Is this the only approach for converting pdf pages to images? There isnt a another way to do this?

@zesman2

You can use Facade PdfConverter class to convert PDF pages to images. Please refer to the following article for more detail.

Convert PDF File

What is the benefit when using the pdfconverter over the image device implementation?

@zesman2

Your can avoid the performance issue using PdfConverter class. Please let us know if you face any issue.