I’ve a problem adding an image to an existing pdf file.
I followed instructions from Aspose Pdf Java documentation.
I use the same signature and code file with 2 different pdf
One pdf shows the new added image perfetcly the other doesn’t.
No errors are shown in both executions.
In the both cases the size of the new file is a little larger then the original one.
What’s wrong ?
Here my code
Tks
public byte[] addLayer(byte[] xpDocument, byte[] xpLayer, DocumentoFirmabile xpCoordinate) throws IOException {
Document doc = new Document(new ByteArrayInputStream(xpDocument));
PageCollection collection = doc.getPages();
int size = collection.size();
Page page = collection.get_Item(size); //ATTENZIONE base 1
if(xpLayer != null) {
InputStream imageStream = new ByteArrayInputStream(xpLayer);
String imageName = page.getResources().getImages().add(imageStream);
page.getContents().add(new GSave());
int lowerLeftX = xpCoordinate.getLowerLeftX();
int lowerLeftY = xpCoordinate.getLowerLeftY();
int upperRightX = xpCoordinate.getUpperRightX();
int upperRightY = xpCoordinate.getUpperRightY();
Rectangle rectangle = new Rectangle(lowerLeftX, lowerLeftY, upperRightX, upperRightY);
Matrix matrix = new Matrix(new double[] { rectangle.getURX() - rectangle.getLLX(), 0, 0, rectangle.getURY() - rectangle.getLLY(), rectangle.getLLX(), rectangle.getLLY() });
page.getContents().add(new ConcatenateMatrix(matrix));
page.getContents().add(new Do(imageName));
page.getContents().add(new GRestore());
imageStream.close();
}
ByteArrayOutputStream output = new ByteArrayOutputStream();
doc.save(output);
return output.toByteArray();
}