Documents for Excel, Java Edition Documentation
Manage File Operations / Export to a PDF File / Support Sheet Background Image
In This Topic
    Support Sheet Background Image
    In This Topic

    In a worksheet, you can set a background image using the BackgroundPicture property of the IWorksheet interface. This background image can be included while exporting a worksheet to a PDF file. This is very useful for displaying company logos and watermarks in PDF documents.

    GcExcel provides the setPrintBackgroundPicture method in PdfSaveOptions class to render the background image in the center of the page while exporting worksheet to PDF file.

    Using Code

    Refer to the following example code to include sheet background image while exporting to PDF document.

    Java
    Copy Code
    Workbook workbook = new Workbook();
    // Fetch default worksheet
    IWorksheet worksheet = workbook.getWorksheets().get(0);
    worksheet.getRange("A1").setValue("GrapeCity Documents for Excel");
    worksheet.getRange("A1").getFont().setSize(25);
    
    // Load an image from a specific file in input stream
    InputStream inputStream = ClassLoader.getSystemResourceAsStream("grapecity.png");
    try {
        byte[] bytes = new byte[inputStream.available()];
        // Read an image from input stream
        inputStream.read(bytes, 0, bytes.length);
    
        // Add background image of the worksheet
        worksheet.setBackgroundPicture(bytes);
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
    
    PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
    // Print the background picture in the centre of exported pdf file
    pdfSaveOptions.setPrintBackgroundPicture(true);
    
    // Saving workbook to pdf
    workbook.save("12-PrintBackgroundPicture.pdf", pdfSaveOptions);

    For more information about adding a background image to a worksheet, refer the Customize Worksheets topic.