Skip to content

Commit 5f10647

Browse files
Merge pull request #393 from SyncfusionExamples/ES-266965-Add-QRcode-in-Word-document
ES-266965- Add the sample Add-QRcode-in-Word-document
2 parents edb9a77 + 5fd7c51 commit 5f10647

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-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 16
4+
VisualStudioVersion = 16.0.31911.196
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Add-QRcode-in-Word-document", "Add-QRcode-in-Word-document\Add-QRcode-in-Word-document.csproj", "{D3AF529E-DB54-4294-A876-DD42E1E472D0}"
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+
{D3AF529E-DB54-4294-A876-DD42E1E472D0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{D3AF529E-DB54-4294-A876-DD42E1E472D0}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{D3AF529E-DB54-4294-A876-DD42E1E472D0}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{D3AF529E-DB54-4294-A876-DD42E1E472D0}.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 = {58137FF9-5AE1-4514-9929-3A8A7DA1DFEB}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Add_QRcode_in_Word_document</RootNamespace>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
11+
<PackageReference Include="Syncfusion.Pdf.Imaging.Net.Core" Version="*" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<None Update="Output\.gitkeep">
16+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
17+
</None>
18+
</ItemGroup>
19+
20+
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
using Syncfusion.Drawing;
4+
using Syncfusion.Pdf.Barcode;
5+
using Syncfusion.Pdf.Graphics;
6+
using System.IO;
7+
8+
namespace Add_QRcode_in_Word_document
9+
{
10+
class Program
11+
{
12+
static void Main(string[] args)
13+
{
14+
using (WordDocument document = new WordDocument())
15+
{
16+
// Inserts one section and one paragraph in the Word document
17+
document.EnsureMinimal();
18+
19+
// Gets the last paragraph of the document
20+
WParagraph paragraph = document.LastParagraph;
21+
22+
// Appending text to the Paragraph
23+
paragraph.AppendText("QR Code \n");
24+
25+
// Generate QR code and get the FileStream
26+
FileStream qrImageStream = CreateQRCode();
27+
28+
// Append QR code image to paragraph
29+
paragraph.AppendPicture(qrImageStream);
30+
31+
// Close the stream after appending the picture
32+
qrImageStream.Close();
33+
34+
// Saves the Word document file to file system
35+
using (FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
36+
{
37+
document.Save(outputStream, FormatType.Docx);
38+
}
39+
}
40+
}
41+
42+
static FileStream CreateQRCode()
43+
{
44+
PdfQRBarcode qrBarcode = new PdfQRBarcode();
45+
46+
// Sets the Input mode to Binary mode
47+
qrBarcode.InputMode = InputMode.BinaryMode;
48+
49+
// Automatically select the Version
50+
qrBarcode.Version = QRCodeVersion.Auto;
51+
52+
// Set the Error correction level to high
53+
qrBarcode.ErrorCorrectionLevel = PdfErrorCorrectionLevel.High;
54+
55+
// Set dimension for each block
56+
qrBarcode.XDimension = 4;
57+
qrBarcode.Text = "This is a sample QR Code";
58+
59+
// Generate a temporary file path
60+
string tempFilePath = Path.GetTempFileName() + ".png";
61+
62+
// Save the QR code as an image to a file
63+
using (Stream imageStream = qrBarcode.ToImage(new SizeF(300, 300)))
64+
{
65+
using (FileStream fileStream = new FileStream(tempFilePath, FileMode.Create, FileAccess.Write))
66+
{
67+
imageStream.CopyTo(fileStream);
68+
}
69+
}
70+
// Return FileStream for the saved image
71+
return new FileStream(tempFilePath, FileMode.Open, FileAccess.Read);
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)