Hi, I’m trying to create a page header with color background and an image like this:
image.png (4.2 KB)
I’m using ASPOSE pdf for Java and here’s my code:
Document document = new Document();
Page pdfPage = document.getPages().add();
// setting page widths and margins
pdfPage.getPageInfo().setWidth(918);
pdfPage.getPageInfo().setHeight(1188);
pdfPage.getPageInfo().setMargin(new MarginInfo(60, 60, 60, 60));
double pageWidth = pdfPage.getPageInfo().getWidth();
FloatingBox coverHeaderBox = new FloatingBox((float) (pageWidth - 120), 120);
coverHeaderBox.setBackgroundColor(Color.getBlack());
Image logo;
try (InputStream is = bannerResource.getInputStream()) {
Image image = new Image();
image.setImageStream(is);
image.setMargin(new MarginInfo(120, 0, 30, 30));
logo = image;
}
coverHeaderBox.getParagraphs().add(logo);
// adds the floating box to the page
pdfPage.getParagraphs().add(coverHeaderBox);
// saving the document
try (OutputStream os = Files.newOutputStream(resultResource.getFile().toPath())) {
document.save(os, SaveFormat.Pdf);
}
and the result doesn’t respect left margin, screenshot from the resulting document:
image.png (26.3 KB)