@@ -17,6 +17,8 @@ namespace HTMLToPDF_WebApplication.Controllers
1717 public class HomeController : Controller
1818 {
1919 private readonly ILogger < HomeController > _logger ;
20+ private static float headerHeight = 30 ;
21+ private static float footerHeight = 60 ;
2022
2123 public HomeController ( ILogger < HomeController > logger )
2224 {
@@ -172,14 +174,53 @@ public ActionResult ExportToPDF(URL url)
172174
173175 for ( int i = 0 ; i < doc . Pages . Count ; i ++ )
174176 {
175- PdfTextWebLink weblink = new PdfTextWebLink ( ) ;
176- weblink . Text = " View blog Link" ;
177- weblink . Font = new PdfStandardFont ( PdfFontFamily . Helvetica , 8 , PdfFontStyle . Bold ) ;
178- weblink . Brush = brush ;
179- weblink . Url = url . BlogLink ;
180- weblink . DrawTextWebLink ( doc . Pages [ i ] . Graphics , new PointF ( 250 , 570 ) ) ;
177+ bool isPageRemoved = false ;
178+ if ( i == doc . Pages . Count - 1 )
179+ {
180+ // Get the first page of the loaded PDF document
181+ PdfPageBase lastPage = doc . Pages [ i ] ;
182+
183+ TextLineCollection lineCollection = new TextLineCollection ( ) ;
184+
185+ // Extract text from the first page with bounds
186+ lastPage . ExtractText ( out lineCollection ) ;
187+
188+ RectangleF textBounds = new RectangleF ( 0 , headerHeight , blinkConverterSettings . PdfPageSize . Height , blinkConverterSettings . PdfPageSize . Width - headerHeight - footerHeight ) ;
189+
190+ string extractText = "" ;
191+
192+ //Get the text provided in the bounds
193+ foreach ( var txtLine in lineCollection . TextLine )
194+ {
195+ foreach ( TextWord word in txtLine . WordCollection )
196+ {
197+ // Check if the word is within textBounds by comparing coordinates
198+ if ( word . Bounds . Left >= textBounds . Left &&
199+ word . Bounds . Right <= textBounds . Right &&
200+ word . Bounds . Top >= textBounds . Top &&
201+ word . Bounds . Bottom <= textBounds . Bottom )
202+ {
203+ extractText += " " + word . Text ;
204+ }
205+ }
206+ }
207+ if ( string . IsNullOrEmpty ( extractText ) )
208+ {
209+ doc . Pages . Remove ( lastPage ) ;
210+ isPageRemoved = true ;
211+ }
212+ }
213+ if ( ! isPageRemoved )
214+ {
215+ PdfTextWebLink weblink = new PdfTextWebLink ( ) ;
216+ weblink . Text = " View blog Link" ;
217+ weblink . Font = new PdfStandardFont ( PdfFontFamily . Helvetica , 8 , PdfFontStyle . Bold ) ;
218+ weblink . Brush = brush ;
219+ weblink . Url = url . BlogLink ;
220+ weblink . DrawTextWebLink ( doc . Pages [ i ] . Graphics , new PointF ( 250 , 570 ) ) ;
221+ isPageRemoved = false ;
222+ }
181223 }
182-
183224 //Creating the stream object
184225 stream = new MemoryStream ( ) ;
185226 //Save the document as stream
@@ -196,7 +237,7 @@ public ActionResult ExportToPDF(URL url)
196237 private static PdfPageTemplateElement AddHeader ( SizeF pdfPageSize , string headerText )
197238 {
198239 //Create PDF page template element for header with bounds.
199- PdfPageTemplateElement header = new PdfPageTemplateElement ( new RectangleF ( 0 , 0 , pdfPageSize . Height , 30 ) ) ;
240+ PdfPageTemplateElement header = new PdfPageTemplateElement ( new RectangleF ( 0 , 0 , pdfPageSize . Height , headerHeight ) ) ;
200241 //Create font and brush for header element.
201242 PdfFont font = new PdfStandardFont ( PdfFontFamily . Helvetica , 8 ) ;
202243 if ( headerText == string . Empty )
@@ -232,7 +273,7 @@ private static PdfPageTemplateElement AddHeader(SizeF pdfPageSize, string header
232273 private static PdfPageTemplateElement AddFooter ( SizeF pdfPageSize , string url )
233274 {
234275 //Create PDF page template element for footer with bounds.
235- PdfPageTemplateElement footer = new PdfPageTemplateElement ( new RectangleF ( 0 , 0 , pdfPageSize . Height , 60 ) ) ;
276+ PdfPageTemplateElement footer = new PdfPageTemplateElement ( new RectangleF ( 0 , 0 , pdfPageSize . Height , footerHeight ) ) ;
236277 //Create font and brush for header element.
237278 PdfFont font = new PdfStandardFont ( PdfFontFamily . Helvetica , 8 ) ;
238279 //Create page number field.
0 commit comments