[]
        
(Showing Draft Content)

Render Excel Range Inside PDF

DsExcel allows you to render selected worksheet ranges at specific locations in a new or existing PDF document. This is useful when a worksheet contains a large amount of data but only selected fields or ranges need to appear in the final report.

For example, a sales worksheet may contain product quantities, regions, and sales manager information. You can render only the product and region data in the PDF without including the remaining worksheet content.

Use the Draw() method of the PrintManager class to render a range on a PDF page. Specify the upper-left position with a PointF object, or use a RectangleF object to control both the position and size. The GetSize method returns the natural rendering size of a range and can help calculate its placement.

When working with paginated PageInfo objects instead, use AppendPage() to append a generated page to a GcPdfDocument. If pages are added, removed, or reordered in a PageInfo collection, call UpdatePageNumberAndPageSettings() to update the page numbers, page count, and page settings before saving the result.

Note: A valid Document Solutions for PDF license is required to render Excel ranges in PDF documents.

Render a Range in a New PDF Document

The following example creates a PDF document and renders the range A4:C8 at a specified position on its first page.

// Initialize workbook.
Workbook workbook = new Workbook();

// Fetch default worksheet.
IWorksheet worksheet = workbook.Worksheets[0];

// Set values.
worksheet.Range["A4:C4"].Value = new string[]
{ "Device", "Quantity", "Unit Price" };
worksheet.Range["A5:C8"].Value = new object[,]
 {
    { "T540p", 12, 9850 },
    { "T570", 5, 7460 },
    { "Y460", 6, 5400 },
    { "Y460F", 8, 6240 }
 };

// Set styles.
worksheet.Range["A4:C4"].Font.Bold = true;
worksheet.Range["A4:C4"].Font.Color = Color.White;
worksheet.Range["A4:C4"].Interior.Color = Color.LightBlue;
worksheet.Range["A5:C8"].Borders[BordersIndex.InsideHorizontal].Color =
Color.Orange;
worksheet.Range["A5:C8"].Borders[BordersIndex.InsideHorizontal].LineStyle =
BorderLineStyle.DashDot;

/* NOTE: To use this feature, you should have a valid license 
for Document Solutions for PDF.*/

// Create a PDF document.
GcPdfDocument doc = new GcPdfDocument();
Page page = doc.NewPage();
GcPdfGraphics g = page.Graphics;

// Create an instance of the PrintManager class.
PrintManager printManager = new PrintManager();

// Draw the Range "A4:C8" to the specified location on the page.
printManager.Draw(page, new PointF(30, 100), worksheet.Range["A4:C8"]);

// Save the modified pages into PDF file.
doc.Save(@"RenderExcelRangesInsidePDFBasic.pdf");

Render Multiple Ranges in an Existing PDF Document

You can also render ranges from different worksheets on different pages of an existing PDF document. Use GetSize to determine the natural size of each range and FindText to position it relative to existing text.

The following example renders three worksheet ranges on pages 4, 5, and 6 of an existing financial report.

static void Main(string[] args)
{
    // Create a PDF file stream.
    FileStream outputStream =
    new FileStream("RenderExcelRangesInsidePDFAdvance.pdf", FileMode.Create);

    // Create a new workbook.
    var workbook = new GrapeCity.Documents.Excel.Workbook();
    Stream fileStream =
    GetResourceStream("xlsx\\FinancialReport.xlsx");
    workbook.Open(fileStream);
    IWorksheet worksheet = workbook.Worksheets[0];

    /* NOTE: To use this feature, you should have a valid license 
       for Document Solutions for PDF.*/

    // Create a PDF document.
    Pdf.GcPdfDocument doc = new Pdf.GcPdfDocument();
    doc.Load(GetResourceStream("Acme-Financial Report 2018.pdf"));

    // Create an instance of the PrintManager class.
    Excel.PrintManager printManager = new Excel.PrintManager();

    // Draw the contents of the sheet3 to the fourth page.
    IRange printArea1 = workbook.Worksheets[2].Range["A3:C24"];
    SizeF size1 = printManager.GetSize(printArea1);
    RectangleF position1 =
    doc.FindText(new GrapeCity.Documents.Pdf.FindTextParams
    ("Proposition enhancements are", true, true),
    new GrapeCity.Documents.Common.OutputRange(4, 4))[0].Bounds.ToRect();
    printManager.Draw(doc.Pages[3],
    new RectangleF(position1.X + position1.Width +
    70, position1.Y, size1.Width, size1.Height), printArea1);

    // Draw the contents of the sheet1 to the fifth page.
    IRange printArea2 = workbook.Worksheets[0].Range["A4:E29"];
    SizeF size2 = printManager.GetSize(printArea2);
    RectangleF position2 =
    doc.FindText(new GrapeCity.Documents.Pdf.FindTextParams(
    "expenditure, an improvement in working", true, true),
    new GrapeCity.Documents.Common.OutputRange(5, 5))[0].Bounds.ToRect();
    printManager.Draw(doc.Pages[4],
    new RectangleF(position2.X, position2.Y +
    position2.Height + 20, size2.Width, size2.Height), printArea2);

    // Draw the contents of the sheet2 to the sixth page.
    IRange printArea3 = workbook.Worksheets[1].Range["A2:E28"];
    SizeF size3 = printManager.GetSize(printArea3);
    RectangleF position3 =
    doc.FindText(new GrapeCity.Documents.Pdf.FindTextParams
    ("company will be able to continue", true, true),
    new GrapeCity.Documents.Common.OutputRange(6, 6))[0].Bounds.ToRect();
    printManager.Draw(doc.Pages[5],
    new RectangleF(position3.X, position3.Y +
    position3.Height + 20, doc.Pages[5].Size.Width -
    position3.X * 2 - 10, size3.Height), printArea3);

    // Save the modified pages into PDF file.
    doc.Save(outputStream);

    // Close the PDF stream.
    outputStream.Close();

}

static Stream GetResourceStream(string resourcePath)
{
    string resource = "RenderExcelRangesInsideAPDF.Resource." +
    resourcePath.Replace("\\", ".");
    var assembly = typeof(Program).GetTypeInfo().Assembly;
    return assembly.GetManifestResourceStream(resource);
}

For more information, please refer to Render sheet ranges inside a PDF(Advanced usage).