We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Add text on PDF converted from Excel

Hi , i want to add some text in PDF page but my PDF is a result from ExcelToPdfConverter.

I tried this:
                //Open the Excel Sheet to Convert                 ExcelToPdfConverter converter = new ExcelToPdfConverter(wSheet);
                //Intialize PDF Document                 PdfDocument pdfDocument = new PdfDocument();
                //Add page                  pdfDocument.Pages.Add();                     //                 RectangleF rect = new RectangleF(0, 0, pdfDocument.Pages[0].GetClientSize().Width, 50);                 //Create page template                 PdfPageTemplateElement header = new PdfPageTemplateElement(rect);                 Color activeColor = Color.FromArgb(44, 71, 120);                 PdfSolidBrush brush = new PdfSolidBrush(activeColor);                 PdfPen pen = new PdfPen(Color.DarkBlue, 3f);                 PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 16, PdfFontStyle.Bold);                 //Set formattings for the text                 PdfStringFormat format = new PdfStringFormat();                 format.Alignment = PdfTextAlignment.Center;                 format.LineAlignment = PdfVerticalAlignment.Middle;                 //Draw title                 header.Graphics.DrawString("TITLE", font, brush, new RectangleF(0, 0, header.Width, header.Height), format);                                  pdfDocument.Template.Top = header;                                  //Initialize converter settings                 ExcelToPdfConverterSettings cs= new ExcelToPdfConverterSettings();                 cs.TemplateDocument = pdfDocument;                 //Convert Excel Document into PDF document                 pdfDocument = converter.Convert(cs);

With this code, I obtain a PDF with 2 pages. The first is a blank page with header "TITLE", the second is a page with header "TITLE" and the excel sheet converted .

How to remove the first page ? And , this is a right way to achieve my target?



1 Reply

SK Surya Kumar Syncfusion Team April 21, 2017 07:28 AM UTC

Hi Nicola, 
 
Thank you for using Syncfusion Products. 
 
In the code snippet, which you have posted in the last update has the code to add new page to the created PDF document before assigning the converted Excel to PDF file as shown below: 
 
This the reason for the addition of the extra page in the beginning of the document. 
 
This can be avoided by adding the new page and setting the width of the header manually, in order to set the default width of the page as header width, please refer the below code snippet: 
 
            //Setting the header bounds 
            RectangleF rect = new RectangleF(0, 0, 515, 50); 
 
            //Create page template 
            PdfPageTemplateElement header = new PdfPageTemplateElement(rect); 

 
Please find the width details of a A4 page: 
Default width: 595. 
Default margin value for left and right: 40 
Width of the page after applying left and right margins:  595-40-40= 515. 
 
We have attached a sample with the made changes, please find it from the below link: 
 
 
Let us know if you need any further assistance regarding this. 
 
Regards, 
Surya Kumar 


Loader.
Live Chat Icon For mobile
Up arrow icon