Skip to content

Commit

Permalink
Removing the Last empty page in the Html converter output pdf document.
Browse files Browse the repository at this point in the history
  • Loading branch information
karmegams02 committed Oct 8, 2024
1 parent 76bb976 commit 0e8e199
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 11 deletions.
59 changes: 50 additions & 9 deletions Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ namespace HTMLToPDF_WebApplication.Controllers
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
private static float headerHeight = 30;
private static float footerHeight = 60;

public HomeController(ILogger<HomeController> logger)
{
Expand Down Expand Up @@ -172,14 +174,53 @@ public ActionResult ExportToPDF(URL url)

for (int i = 0; i < doc.Pages.Count; i++)
{
PdfTextWebLink weblink = new PdfTextWebLink();
weblink.Text = " View blog Link";
weblink.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 8, PdfFontStyle.Bold);
weblink.Brush = brush;
weblink.Url = url.BlogLink;
weblink.DrawTextWebLink(doc.Pages[i].Graphics, new PointF(250, 570));
bool isPageRemoved = false;
if (i == doc.Pages.Count - 1)
{
// Get the first page of the loaded PDF document
PdfPageBase lastPage = doc.Pages[i];

TextLineCollection lineCollection = new TextLineCollection();

// Extract text from the first page with bounds
lastPage.ExtractText(out lineCollection);

RectangleF textBounds = new RectangleF(0, headerHeight, blinkConverterSettings.PdfPageSize.Height, blinkConverterSettings.PdfPageSize.Width - headerHeight - footerHeight);

string extractText = "";

//Get the text provided in the bounds
foreach (var txtLine in lineCollection.TextLine)
{
foreach (TextWord word in txtLine.WordCollection)
{
// Check if the word is within textBounds by comparing coordinates
if (word.Bounds.Left >= textBounds.Left &&
word.Bounds.Right <= textBounds.Right &&
word.Bounds.Top >= textBounds.Top &&
word.Bounds.Bottom <= textBounds.Bottom)
{
extractText += " " + word.Text;
}
}
}
if (string.IsNullOrEmpty(extractText))
{
doc.Pages.Remove(lastPage);
isPageRemoved = true;
}
}
if (!isPageRemoved)
{
PdfTextWebLink weblink = new PdfTextWebLink();
weblink.Text = " View blog Link";
weblink.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 8, PdfFontStyle.Bold);
weblink.Brush = brush;
weblink.Url = url.BlogLink;
weblink.DrawTextWebLink(doc.Pages[i].Graphics, new PointF(250, 570));
isPageRemoved = false;
}
}

//Creating the stream object
stream = new MemoryStream();
//Save the document as stream
Expand All @@ -196,7 +237,7 @@ public ActionResult ExportToPDF(URL url)
private static PdfPageTemplateElement AddHeader(SizeF pdfPageSize, string headerText)
{
//Create PDF page template element for header with bounds.
PdfPageTemplateElement header = new PdfPageTemplateElement(new RectangleF(0, 0, pdfPageSize.Height, 30));
PdfPageTemplateElement header = new PdfPageTemplateElement(new RectangleF(0, 0, pdfPageSize.Height, headerHeight));
//Create font and brush for header element.
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 8);
if (headerText == string.Empty)
Expand Down Expand Up @@ -232,7 +273,7 @@ private static PdfPageTemplateElement AddHeader(SizeF pdfPageSize, string header
private static PdfPageTemplateElement AddFooter(SizeF pdfPageSize, string url)
{
//Create PDF page template element for footer with bounds.
PdfPageTemplateElement footer = new PdfPageTemplateElement(new RectangleF(0, 0, pdfPageSize.Height, 60));
PdfPageTemplateElement footer = new PdfPageTemplateElement(new RectangleF(0, 0, pdfPageSize.Height, footerHeight));
//Create font and brush for header element.
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 8);
//Create page number field.
Expand Down
2 changes: 1 addition & 1 deletion HTMLToPDF_WebApplication.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.HtmlToPdfConverter.Net.Windows" Version="25.2.6" />
<PackageReference Include="Syncfusion.HtmlToPdfConverter.Net.Windows" Version="27.1.52" />
</ItemGroup>

</Project>
Empty file added License.txt
Empty file.
3 changes: 2 additions & 1 deletion Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ public class Program
{
public static void Main(string[] args)
{
Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense("");
string licenseValidation = System.IO.File.ReadAllText(Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, "License.txt")));
Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense(licenseValidation);
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
Expand Down

0 comments on commit 0e8e199

Please sign in to comment.