Skip to content

Commit a65928a

Browse files
Merge pull request #230 from SyncfusionExamples/995577
995577: Added image as background in PDF document.
2 parents 2b87406 + 5851c25 commit a65928a

File tree

5 files changed

+77
-0
lines changed

5 files changed

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

0 commit comments

Comments
 (0)