Skip to content

Commit e2cc2df

Browse files
Merge pull request #210 from SyncfusionExamples/260329
260329: Added the code sample for create PDF page based on the image size using PdfUnitConvertor class
2 parents b458a2e + 4ddf7b1 commit e2cc2df

File tree

5 files changed

+81
-0
lines changed

5 files changed

+81
-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}") = "Unit-conversion-in-image-position", "Unit-conversion-in-image-position\Unit-conversion-in-image-position.csproj", "{9268CB04-C4C4-42C3-B54F-028667D9C921}"
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+
{9268CB04-C4C4-42C3-B54F-028667D9C921}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{9268CB04-C4C4-42C3-B54F-028667D9C921}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{9268CB04-C4C4-42C3-B54F-028667D9C921}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{9268CB04-C4C4-42C3-B54F-028667D9C921}.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 = {C5FD0AEE-BADF-4195-9FA4-7B85BD875DD8}
24+
EndGlobalSection
25+
EndGlobal
20.9 KB
Loading

Images/Unit-conversion-in-image-position/.NET/Unit-conversion-in-image-position/Output/gitkeep.txt

Whitespace-only changes.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using Syncfusion.Drawing;
2+
using Syncfusion.Pdf;
3+
using Syncfusion.Pdf.Graphics;
4+
5+
//Create a new PDF document
6+
using (PdfDocument document = new PdfDocument())
7+
{
8+
using (FileStream stream = new FileStream(Path.GetFullPath(@"Data/Input.png"), FileMode.Open, FileAccess.Read))
9+
{
10+
//Load the image from the disk
11+
PdfBitmap image = new PdfBitmap(stream);
12+
13+
//Add the first section to the PDF document
14+
PdfSection section = document.Sections.Add();
15+
16+
//Initialize unit converter
17+
PdfUnitConverter converter = new PdfUnitConverter();
18+
19+
//Convert the image size from pixel to points
20+
SizeF size = converter.ConvertFromPixels(image.PhysicalDimension, PdfGraphicsUnit.Point);
21+
22+
//Set section size based on the image size
23+
section.PageSettings.Size = size;
24+
25+
// Set section orientation based on the image size (by default Portrait)
26+
if (image.Width > image.Height)
27+
section.PageSettings.Orientation = PdfPageOrientation.Landscape;
28+
29+
//Set a margin for the section
30+
section.PageSettings.Margins.All = 0;
31+
32+
//Add a page to the section
33+
PdfPage page = section.Pages.Add();
34+
35+
//Draw image
36+
page.Graphics.DrawImage(image, 0, 0);
37+
38+
//Save the document
39+
document.Save(Path.GetFullPath(@"Output/Output.pdf"));
40+
}
41+
}
Lines changed: 15 additions & 0 deletions
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>Unit_conversion_in_image_position</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.Pdf.Imaging.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
</Project>

0 commit comments

Comments
 (0)