Skip to content

Commit 0780ebe

Browse files
Merge pull request #4 from SyncfusionExamples/empty_page_issue
Removing the Last empty page in the Html converter output pdf document.
2 parents 76bb976 + 0e8e199 commit 0780ebe

File tree

4 files changed

+53
-11
lines changed

4 files changed

+53
-11
lines changed

Controllers/HomeController.cs

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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.

HTMLToPDF_WebApplication.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Syncfusion.HtmlToPdfConverter.Net.Windows" Version="25.2.6" />
10+
<PackageReference Include="Syncfusion.HtmlToPdfConverter.Net.Windows" Version="27.1.52" />
1111
</ItemGroup>
1212

1313
</Project>

License.txt

Whitespace-only changes.

Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ public class Program
44
{
55
public static void Main(string[] args)
66
{
7-
Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense("");
7+
string licenseValidation = System.IO.File.ReadAllText(Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, "License.txt")));
8+
Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense(licenseValidation);
89
var builder = WebApplication.CreateBuilder(args);
910

1011
// Add services to the container.

0 commit comments

Comments
 (0)