// Create to a pdf file stream FileOutputStream outputStream = null; try { outputStream = new FileOutputStream("SaveSheetBackgroundToPDF.pdf"); } catch (FileNotFoundException e) { e.printStackTrace(); } // Create a new workbook Workbook workbook = new Workbook(); IWorksheet worksheet = workbook.getWorksheets().get(0); worksheet.getRange("A1").setValue("GrapeCity Documents for Excel"); worksheet.getRange("A1").getFont().setSize(25); //Set a background image for worksheet InputStream inputStream = this.getResourceStream("logo.png"); try { byte[] bytes = new byte[inputStream.available()]; inputStream.read(bytes, 0, bytes.length); worksheet.setBackgroundPicture(bytes); }catch (IOException ioe){ // Log ignored error of your code // log.debug(ioe.getMessage()); } PdfSaveOptions pdfSaveOptions = new PdfSaveOptions(); //Print the background image when saving pdf. //The background image will be centered on every page of the sheet. pdfSaveOptions.setPrintBackgroundPicture(true); //Save the workbook into pdf file. workbook.save(outputStream, pdfSaveOptions); // Close the file stream try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); }
// Create to a pdf file stream var outputStream: FileOutputStream? = null try { outputStream = FileOutputStream("SaveSheetBackgroundToPDF.pdf") } catch (e: FileNotFoundException) { e.printStackTrace() } // Create a new workbook var workbook = Workbook() val worksheet = workbook.worksheets.get(0) worksheet.getRange("A1").setValue("GrapeCity Documents for Excel") worksheet.getRange("A1").font.size = 25.0 //Set a background image for worksheet val inputStream = this.getResourceStream("logo.png") try { val bytes = ByteArray(inputStream!!.available()) inputStream!!.read(bytes, 0, bytes.size) worksheet.backgroundPicture = bytes } catch (ioe: IOException) { ioe.printStackTrace() } val pdfSaveOptions = PdfSaveOptions() //Print the background image when saving pdf. //The background image will be centered on every page of the sheet. pdfSaveOptions.printBackgroundPicture = true //Save the workbook into pdf file. workbook.save(outputStream, pdfSaveOptions) // Close the file stream try { if(outputStream != null){ outputStream.close() } } catch (e: IOException) { e.printStackTrace() }