I am trying to add PNG as a new layer to PDF.
When I checked the created PDF,
the image quality seems to be degraded.
Is there a problem with the code?
Or is it another problem?
Thank you
PNG and PDF files:
I attached sample files.
pom.xml:
- Code: Select all
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.pdf</artifactId>
<version>5.1.0</version>
</dependency>
code:
- Code: Select all
String srcPng = ...;
String srcPdf = ...;
String dstPdf = ...;
PdfDocument srcDocument = null;
try {
srcDocument = new PdfDocument(srcPdf);
PdfLayer pdfLayer = srcDocument.getLayers().addLayer("add001");
PdfCanvas canvas = pdfLayer.createGraphics(srcDocument.getPages().get(0).getCanvas());
PdfImage pngImage = PdfImage.fromImage(srcPng);
canvas.drawImage(pngImage, new Point2D.Float(0, 0));
srcDocument.saveToFile(dstPdf, FileFormat.PDF);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (srcDocument != null) {
srcDocument.close();
}
}