[]
        
(Showing Draft Content)

Render Excel Range Inside PDF

GcExcel .NET enables users to render Excel cell ranges inside PDF.

This feature is useful especially when you're dealing with bulk data in the spreadsheets and you want to render only specific Excel range inside an existing PDF file. For instance - let's say you have a worksheet containing large amounts of sales data with fields such as "Number of Products Sold", "Area Sales Manager", "Region" etc. but you want to export only a chunk of useful data (like only "Number of Products Sold" and "Region") at some location in a PDF file and not all the data (you don't want to include the "Area Sales Manager" information). In this scenario, the "Render Excel Range Inside PDF" feature can be used to select some specific ranges in the worksheet and render them to specific location in a PDF file to generate full PDF reports.

In order to render Excel range inside the PDF file, you need to first create an instance of the PrintManager class and then use the Draw() method to render the Excel range on a PDF page at a location. In case, you want to add some extra information in your PDF file (data which is not present in your Excel file), you can use the AppendPage() method of the PrintManager class after configuring all the pagination settings. Finally, call the UpdatePageNumberAndPageSettings() method in order to update the indexes of the page number and the page settings for each page. When everything is done, simply save your PDF file using the GcPdfDocument.Save() method.

Note: In order to render Excel cell ranges inside PDF, you should have a valid license for GrapeCity Documents for PDF.

Refer to the following example code to allow users to render Excel ranges inside the PDF file.

// 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 GrapeCity Documents 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");

Refer to the following example code to allow users to render Excel ranges inside the PDF file along with some custom textual information at runtime to the specified location on the page.

// 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 GrapeCity Documents 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);
   }