Skip to content

Commit 3155ab5

Browse files
Merge pull request #219 from SyncfusionExamples/263542
263542: Crop a PDF Page from HTML to PDF document using C#
2 parents 870052a + 9dd863a commit 3155ab5

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.14.36616.10 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Crop-page-from-HTML-to-PDF-document", "Crop-page-from-HTML-to-PDF-document\Crop-page-from-HTML-to-PDF-document.csproj", "{77219185-C36B-4F5F-823C-450F4B0C8D6E}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{77219185-C36B-4F5F-823C-450F4B0C8D6E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{77219185-C36B-4F5F-823C-450F4B0C8D6E}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{77219185-C36B-4F5F-823C-450F4B0C8D6E}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{77219185-C36B-4F5F-823C-450F4B0C8D6E}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {BC7C80E1-9850-4820-AAF7-CF9390605A84}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Crop_page_from_HTML_to_PDF_document</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.HtmlToPdfConverter.Net.Windows" Version="*" />
13+
</ItemGroup>
14+
15+
</Project>

HTML to PDF/Blink/Crop-page-from-HTML-to-PDF-document/.NET/Crop-page-from-HTML-to-PDF-document/Output/gitkeep.txt

Whitespace-only changes.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Syncfusion.Drawing;
2+
using Syncfusion.HtmlConverter;
3+
using Syncfusion.Pdf;
4+
using Syncfusion.Pdf.Graphics;
5+
6+
// Create an HTML-to-PDF converter instance
7+
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
8+
// Convert the given URL to a PDF document
9+
using (PdfDocument document = htmlConverter.Convert("https://www.google.com"))
10+
using (PdfDocument croppedDocument = new PdfDocument())
11+
{
12+
// Get the first page from the converted PDF
13+
PdfPage srcPage = document.Pages[0];
14+
// Create a template of the entire source page
15+
PdfTemplate template = srcPage.CreateTemplate();
16+
// Define the crop height (3.6 inches converted to points)
17+
float cropHeight = 3.6f * 72f;
18+
float pageWidth = srcPage.GetClientSize().Width; // Get original page width
19+
// Configure the new document's page settings for cropping
20+
croppedDocument.PageSettings.Margins.All = 0;
21+
croppedDocument.PageSettings.Width = pageWidth;
22+
croppedDocument.PageSettings.Height = cropHeight;
23+
// Add a new page to the cropped document
24+
PdfPage destPage = croppedDocument.Pages.Add();
25+
// Draw the cropped template onto the new page
26+
destPage.Graphics.DrawPdfTemplate(template, new PointF(0, 0));
27+
// Save the cropped PDF document
28+
croppedDocument.Save(Path.GetFullPath(@"Output/Output.pdf"));
29+
}

0 commit comments

Comments
 (0)