Skip to content

Commit 0ab50fd

Browse files
committed
995577: Added image as background in PDF document.
1 parent 2b87406 commit 0ab50fd

File tree

5 files changed

+75
-0
lines changed

5 files changed

+75
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<Solution>
2+
<Project Path="Add-image-as-background-in-PDF/Add-image-as-background-in-PDF.csproj" />
3+
</Solution>
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>Background_image</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.Pdf.Imaging.NET" Version="*" />
13+
</ItemGroup>
14+
15+
</Project>
102 KB
Loading

Images/Add-image-as-background-in-PDF/.NET/Add-image-as-background-in-PDF/Output/gitkeep.txt

Whitespace-only changes.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using Syncfusion.Drawing;
2+
using Syncfusion.Pdf;
3+
using Syncfusion.Pdf.Graphics;
4+
5+
// Create the new PDF document
6+
using (PdfDocument document = new PdfDocument())
7+
{
8+
// Add a new page to the document
9+
PdfPage page = document.Pages.Add();
10+
// Get the client size
11+
SizeF clientSize = page.GetClientSize();
12+
// Load the background image from disk
13+
using FileStream imageStream = new FileStream(
14+
Path.GetFullPath(@"Data/Image.jpg"),
15+
FileMode.Open,
16+
FileAccess.Read
17+
);
18+
PdfBitmap image = new PdfBitmap(imageStream);
19+
// Save the current graphics state, apply transparency, draw the image to cover the page, then restore.
20+
PdfGraphicsState state = page.Graphics.Save();
21+
page.Graphics.SetTransparency(0.2f); // 20% opacity for the background image
22+
page.Graphics.DrawImage(
23+
image,
24+
new PointF(0, 0),
25+
new SizeF(clientSize.Width, clientSize.Height)
26+
);
27+
page.Graphics.Restore(state);
28+
// Define a small margin for the text content.
29+
const float margin = 10f;
30+
// Text bounds: fill the page within margins.
31+
RectangleF textBounds = new RectangleF(
32+
margin,
33+
margin,
34+
clientSize.Width - margin * 2,
35+
clientSize.Height - margin * 2
36+
);
37+
// Body font for the paragraph.
38+
PdfFont bodyFont = new PdfStandardFont(PdfFontFamily.TimesRoman, 16, PdfFontStyle.Regular);
39+
// Sample paragraph text.
40+
string paragraphText =
41+
"Adventure Works Cycles, the fictitious company on which the AdventureWorks sample databases " +
42+
"are based, is a large, multinational manufacturing company. The company manufactures and sells " +
43+
"metal and composite bicycles to North American, European and Asian commercial markets. While " +
44+
"its base operation is located in Washington with 290 employees, several regional sales teams " +
45+
"are located throughout their market base.";
46+
// Create a text element and configure layout to paginate if content exceeds current page.
47+
PdfTextElement textElement = new PdfTextElement(paragraphText, bodyFont, PdfBrushes.Black);
48+
PdfLayoutFormat layoutFormat = new PdfLayoutFormat
49+
{
50+
Break = PdfLayoutBreakType.FitPage, // Fit within page bounds
51+
Layout = PdfLayoutType.Paginate // Continue to subsequent pages if needed
52+
};
53+
// Draw the text paragraph in the defined bounds.
54+
textElement.Draw(page, textBounds, layoutFormat);
55+
// Save the PDF document to disk.
56+
document.Save(Path.GetFullPath(@"Output/Output.pdf"));
57+
}

0 commit comments

Comments
 (0)