Document Solutions for Excel, Java Edition | Document Solutions
File Operations / Export to PDF / Support Sheet Background Image
In This Topic
    Support Sheet Background Image
    In This Topic

    DsExcel supports sheet background image which can be included while exporting the worksheet to a PDF file. This is very useful for displaying company logos and watermarks in PDF documents.

    Render Background Image

    In a worksheet, you can set a background image using the setBackgroundPicture method of the IWorksheet interface.

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

    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("Documents for Excel");
    worksheet.getRange("A1").getFont().setSize(25);
    
    // Load an image from a specific file in input stream
    InputStream inputStream = ClassLoader.getSystemResourceAsStream("background-picture.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);

    Render Multiple Background Images

    Multiple background images can be rendered in DsExcel using the getBackgroundPictures method of the IWorksheet interface. These images can be included while exporting the worksheet to PDF documents. The background images in PDF are drawn based on the gridlines and can be positioned anywhere in the document by specifying the coordinates of the destination rectangle.

    Further, the image transparency, border, corner radius and other formatting options can also be applied. For setting the corner radius, the minimum value is 0 and the maximum value is the height or width (whichever is smaller) of the destination rectangle divided by two. The ImageLayout enum can be used to specify the way the image should be placed to fill the destination rectangle in PDF.

    DsExcel also supports JSON export of background mages by using ToJSON method. However, the image is discarded when exported to Excel.

    Refer to the following example code to include multiple background images while exporting to PDF document.

    Java
    Copy Code
    Workbook workbook = new Workbook();
    IWorksheet worksheet = workbook.getWorksheets().get(0);
                
    //Add a background picture in the worksheet
    IBackgroundPicture picture1 = worksheet.getBackgroundPictures().addPictureInPixel("image.png", 10, 10, 250, 150);
    IBackgroundPicture picture2 = worksheet.getBackgroundPictures().addPictureInPixel("ConvertShapeToImage.png", 180, 10, 150, 100);
    
    //Set the border style of the destination rectangle.
    picture1.getLine().getColor().setRGB(Color.GetRed());
    picture1.getLine().setWeight(1);
    
    //The background picture will be resized to fill the destination dimensions.The aspect ratio is not preserved.
    picture1.setBackgroundImageLayout(ImageLayout.Tile);
    
    //Sets the rounded corner of the destination rectangle.
    picture1.setCornerRadius(50);
    //Sets the transparency of the background picture.
    picture1.setTransparency(0.5);
    picture2.setTransparency(0.5);
    
    //Save to PDF file
    workbook.save("ExportBackgroundImageToPDF.pdf");

    Limitation

    DsExcel uses the first background image found from the first worksheet to last worksheet while exporting to JSON.

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