-
Notifications
You must be signed in to change notification settings - Fork 48
995577: Added image as background in PDF document. #230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
Images/Add-image-as-background-in-PDF/.NET/Add-image-as-background-in-PDF.slnx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| <Solution> | ||
| <Project Path="Add-image-as-background-in-PDF/Add-image-as-background-in-PDF.csproj" /> | ||
| </Solution> |
15 changes: 15 additions & 0 deletions
15
...ckground-in-PDF/.NET/Add-image-as-background-in-PDF/Add-image-as-background-in-PDF.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <RootNamespace>Background_image</RootNamespace> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Syncfusion.Pdf.Imaging.NET" Version="*" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
Binary file added
BIN
+102 KB
...d-image-as-background-in-PDF/.NET/Add-image-as-background-in-PDF/Data/Image.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
59 changes: 59 additions & 0 deletions
59
Images/Add-image-as-background-in-PDF/.NET/Add-image-as-background-in-PDF/Program.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| using Syncfusion.Drawing; | ||
| using Syncfusion.Pdf; | ||
| using Syncfusion.Pdf.Graphics; | ||
|
|
||
| // Create the new PDF document | ||
| using (PdfDocument document = new PdfDocument()) | ||
| { | ||
| //Set margin | ||
| document.PageSettings.Margins.All = 0; | ||
| // Add a new page to the document | ||
| PdfPage page = document.Pages.Add(); | ||
| // Get the client size | ||
| SizeF clientSize = page.GetClientSize(); | ||
| // Load the background image from disk | ||
| using FileStream imageStream = new FileStream( | ||
| Path.GetFullPath(@"Data/Image.jpg"), | ||
| FileMode.Open, | ||
| FileAccess.Read | ||
| ); | ||
| PdfBitmap image = new PdfBitmap(imageStream); | ||
| // Save the current graphics state, apply transparency, draw the image to cover the page, then restore. | ||
| PdfGraphicsState state = page.Graphics.Save(); | ||
| page.Graphics.SetTransparency(0.2f); // 20% opacity for the background image | ||
| page.Graphics.DrawImage( | ||
| image, | ||
| new PointF(0, 0), | ||
| new SizeF(clientSize.Width, clientSize.Height) | ||
| ); | ||
| page.Graphics.Restore(state); | ||
| // Define a margin for the text content. | ||
| const float margin = 40f; | ||
| // Text bounds: fill the page within margins. | ||
| RectangleF textBounds = new RectangleF( | ||
| margin, | ||
| margin, | ||
| clientSize.Width - margin * 2, | ||
| clientSize.Height - margin * 2 | ||
| ); | ||
| // Body font for the paragraph. | ||
| PdfFont bodyFont = new PdfStandardFont(PdfFontFamily.TimesRoman, 16, PdfFontStyle.Regular); | ||
| // Sample paragraph text. | ||
| string paragraphText = | ||
| "Adventure Works Cycles, the fictitious company on which the AdventureWorks sample databases " + | ||
| "are based, is a large, multinational manufacturing company. The company manufactures and sells " + | ||
| "metal and composite bicycles to North American, European and Asian commercial markets. While " + | ||
| "its base operation is located in Washington with 290 employees, several regional sales teams " + | ||
| "are located throughout their market base."; | ||
| // Create a text element and configure layout to paginate if content exceeds current page. | ||
| PdfTextElement textElement = new PdfTextElement(paragraphText, bodyFont, PdfBrushes.Black); | ||
| PdfLayoutFormat layoutFormat = new PdfLayoutFormat | ||
| { | ||
| Break = PdfLayoutBreakType.FitPage, // Fit within page bounds | ||
| Layout = PdfLayoutType.Paginate // Continue to subsequent pages if needed | ||
| }; | ||
| // Draw the text paragraph in the defined bounds. | ||
| textElement.Draw(page, textBounds, layoutFormat); | ||
| // Save the PDF document to disk. | ||
| document.Save(Path.GetFullPath(@"Output/Output.pdf")); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.