Skip to content

Commit 0159514

Browse files
994481: Added Sample for add custom stamp annotation without Blurriness
1 parent 4b18dec commit 0159514

File tree

9 files changed

+169
-0
lines changed

9 files changed

+169
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Application x:Class="Stamp_blur_Solution.App"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:local="clr-namespace:Stamp_blur_Solution"
5+
StartupUri="MainWindow.xaml">
6+
<Application.Resources>
7+
8+
</Application.Resources>
9+
</Application>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Configuration;
2+
using System.Data;
3+
using System.Windows;
4+
5+
namespace Stamp_blur_Solution
6+
{
7+
/// <summary>
8+
/// Interaction logic for App.xaml
9+
/// </summary>
10+
public partial class App : Application
11+
{
12+
}
13+
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.Windows;
2+
3+
[assembly: ThemeInfo(
4+
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
5+
//(used if a resource is not found in the page,
6+
// or application resource dictionaries)
7+
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
8+
//(used if a resource is not found in the page,
9+
// app, or any theme specific resource dictionaries)
10+
)]
343 KB
Binary file not shown.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Window x:Class="Stamp_blur_Solution.MainWindow"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6+
xmlns:local="clr-namespace:Stamp_blur_Solution" xmlns:PdfViewer="clr-namespace:Syncfusion.Windows.PdfViewer;assembly=Syncfusion.PdfViewer.WPF"
7+
mc:Ignorable="d"
8+
Title="MainWindow" Height="450" Width="800">
9+
<Grid>
10+
<Grid.RowDefinitions>
11+
<RowDefinition Height="Auto"/>
12+
<RowDefinition Height="*"/>
13+
</Grid.RowDefinitions>
14+
<Button Content="Button" HorizontalAlignment="Left" Margin="10" Grid.Row="0" VerticalAlignment="Top" Click="Button_Click"/>
15+
<PdfViewer:PdfViewerControl x:Name="PDFViewer" Grid.Row ="1"/>
16+
</Grid>
17+
</Window>
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
using Syncfusion.Pdf.Graphics;
2+
using Syncfusion.Windows.PdfViewer;
3+
using System.Drawing;
4+
using System.IO;
5+
using System.Windows;
6+
using System.Windows.Controls;
7+
using System.Windows.Media.Imaging;
8+
9+
namespace Stamp_blur_Solution
10+
{
11+
/// <summary>
12+
/// Interaction logic for MainWindow.xaml
13+
/// </summary>
14+
public partial class MainWindow : Window
15+
{
16+
public MainWindow()
17+
{
18+
InitializeComponent();
19+
PDFViewer.Load("../../../Data/Input.pdf");
20+
}
21+
private void Button_Click(object sender, RoutedEventArgs e)
22+
{
23+
var unitConvertor = new PdfUnitConvertor();
24+
25+
// Stamp properties
26+
int SX = (int)unitConvertor.ConvertToPixels(125, PdfGraphicsUnit.Point);
27+
int SY = (int)unitConvertor.ConvertToPixels(108, PdfGraphicsUnit.Point);
28+
string StampNo = "01";
29+
int StampSize = 100;
30+
31+
int StampWidth = (int)unitConvertor.ConvertToPixels(20, PdfGraphicsUnit.Point);
32+
int StampHeight = (int)unitConvertor.ConvertToPixels(20, PdfGraphicsUnit.Point);
33+
34+
// 1. Create a high-resolution Bitmap for the custom stamp
35+
using (var customStampBitmap = new Bitmap(StampSize, StampSize))
36+
{
37+
using (Graphics g = Graphics.FromImage(customStampBitmap))
38+
{
39+
g.Clear(Color.Transparent);
40+
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
41+
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
42+
43+
// Draw ellipse
44+
using (var pen = new Pen(Color.Red, 3))
45+
{
46+
g.DrawEllipse(pen, 3, 3, StampSize - 6, StampSize - 6);
47+
}
48+
49+
// Draw stamp number text (centered)
50+
using (var font = new Font("Helvetica", 28))
51+
{
52+
SizeF textSize = g.MeasureString(StampNo, font);
53+
var textPoint = new System.Drawing.PointF(
54+
(StampSize - textSize.Width) / 2f,
55+
(StampSize - textSize.Height) / 2f);
56+
g.DrawString(StampNo, font, Brushes.Red, textPoint);
57+
}
58+
}
59+
60+
// 2. Convert Bitmap to BitmapImage for WPF PdfViewer
61+
BitmapImage bitmapImage;
62+
using (var ms = new MemoryStream())
63+
{
64+
customStampBitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
65+
ms.Position = 0;
66+
67+
bitmapImage = new BitmapImage();
68+
bitmapImage.BeginInit();
69+
bitmapImage.StreamSource = ms;
70+
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
71+
bitmapImage.EndInit();
72+
}
73+
74+
// 3. Create WPF Image control
75+
var stampImage = new System.Windows.Controls.Image
76+
{
77+
Source = bitmapImage
78+
};
79+
80+
// 4. Create PdfStampAnnotation
81+
var stampAnnotation = new PdfStampAnnotation(stampImage);
82+
83+
// 5. Define position and size for the stamp in PDF
84+
var position = new System.Windows.Point(SX, SY);
85+
var stampSizeObj = new System.Drawing.Size(StampWidth, StampHeight);
86+
87+
// 6. Add the custom image stamp to the PDF viewer
88+
// NOTE: Ensure 'pdfViewer' is your PdfViewerControl instance defined in XAML.
89+
PDFViewer.AddStamp(stampAnnotation, 1, position, stampSizeObj);
90+
}
91+
}
92+
93+
}
94+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>WinExe</OutputType>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<PackageReference Include="Syncfusion.PdfViewer.WPF" Version="*" />
9+
<PackageReference Include="System.Drawing.Common" Version="10.0.1" />
10+
</ItemGroup>
11+
<Import Project="targets\MultiTargeting.targets" />
12+
</Project>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<Solution>
2+
<Project Path="Stamp_blur_Solution.csproj" />
3+
</Solution>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project>
2+
<PropertyGroup>
3+
<TargetFrameworks>net462;net8.0-windows;net9.0-windows;net10.0-windows</TargetFrameworks>
4+
<UseWPF>true</UseWPF>
5+
<GenerateAssemblyInfo>False</GenerateAssemblyInfo>
6+
<GenerateDocumentationFile>True</GenerateDocumentationFile>
7+
<EnableDefaultItems>True</EnableDefaultItems>
8+
<EnableDefaultEmbeddedResourceItems>True</EnableDefaultEmbeddedResourceItems>
9+
</PropertyGroup>
10+
</Project>

0 commit comments

Comments
 (0)