Documents for Excel, .NET 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 PrintBackgroundPicture property 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.

    Copy Code
    // Initialize workbook
    Workbook workbook = new Workbook();
    // Fetch default worksheet 
    IWorksheet worksheet = workbook.Worksheets[0];
    worksheet.Range["A1"].Value = "GrapeCity Documents for Excel";
    worksheet.Range["A1"].Font.Size = 25;
    using (FileStream pictureStream = File.Open(@"grapecity.png", FileMode.Open, FileAccess.Read))
        MemoryStream pictureMemoryStream = new MemoryStream();
        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);

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