Document Solutions for Excel, .NET 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 BackgroundPicture property of the IWorksheet interface.

    The PrintBackgroundPicture property in PdfSaveOptions class renders the background image in the center of the page while exporting worksheet to PDF document.

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

    C#
    Copy Code
    // Initialize workbook
    Workbook workbook = new Workbook();
    // Fetch default worksheet 
    IWorksheet worksheet = workbook.Worksheets[0];
    worksheet.Range["A1"].Value = "Documents for Excel";
    worksheet.Range["A1"].Font.Size = 25;
    
    using (FileStream pictureStream = File.Open(@"background-image.png", FileMode.Open, FileAccess.Read))
    {
        MemoryStream pictureMemoryStream = new MemoryStream();
        pictureStream.CopyTo(pictureMemoryStream);
        byte[] picturebytes = pictureMemoryStream.ToArray();
    
        //Add background image of the worksheet
        worksheet.BackgroundPicture = picturebytes;
    }
    PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
    //Print the background picture in the centre of exported pdf file
    pdfSaveOptions.PrintBackgroundPicture = true;
    
    // Saving workbook to pdf
    workbook.Save(@"PrintBackgroundPicture.pdf", pdfSaveOptions);

     

    Render Multiple Background Images

    Multiple background images can be rendered in DsExcel using the BackgroundPictures property 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.

    C#
    Copy Code
    Workbook workbook = new Workbook();
    IWorksheet worksheet = workbook.Worksheets[0];
    
    //Add two background pictures in the worksheet
    IBackgroundPicture picture1 = worksheet.BackgroundPictures.AddPictureInPixel("logo.png", 100, 100, 350, 250);
    IBackgroundPicture picture2 = worksheet.BackgroundPictures.AddPictureInPixel("watermark.png", 180, 10, 150, 100);
    
    //Set the border style of the destination rectangle
    picture1.Line.Color.RGB = Color.Red;
    picture1.Line.Weight = 1;
    
    //The background picture will be resized to fill the destination dimensions.The aspect ratio is not preserved.
    picture1.BackgroundImageLayout = ImageLayout.Tile;
    //Sets the rounded corner of the destination rectangle
    picture1.CornerRadius = 50;
    //Sets the transparency of the background pictures
    picture1.Transparency = 0.5;
    picture2.Transparency = 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.