-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathImageHelper.cs
148 lines (127 loc) · 5.07 KB
/
ImageHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using iDiTect.Pdf.Editing;
using iDiTect.Pdf.Editing.Flow;
using iDiTect.Pdf.IO;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
namespace iDiTect.Pdf.Demo
{
public static class ImageHelper
{
public static void AddImage()
{
PdfFile pdfFile = new PdfFile();
PdfDocument document;
using (FileStream fs = File.OpenRead("sample.pdf"))
{
//Read pdf document from stream
document = pdfFile.Import(fs);
}
//Get first page of pdf
PdfPage page = document.Pages[0];
//Create page level builder
PageContentBuilder builder = new PageContentBuilder(page);
//Add image using builder's DrawImage directly
using (Stream imgStream = File.OpenRead("sample.jpg"))
{
//Set image position
builder.Position.Translate(100, 100);
//insert image as original size
builder.DrawImage(imgStream);
//insert image with customized size
//builder.DrawImage(imgStream, new Size(80, 80));
}
//Add image using Block object
using (Stream imgStream = File.OpenRead("sample2.jpg"))
{
//Set image position
builder.Position.Translate(100, 400);
Block block = new Block();
//insert image as original size
//block.InsertImage(imgStream);
//insert image with customized size
block.InsertImage(imgStream, new Size(100, 100));
builder.DrawBlock(block);
}
using (FileStream fs = File.OpenWrite("InsertImage.pdf"))
{
pdfFile.Export(document, fs);
}
}
public static void AddImageInLine()
{
PdfDocument document = new PdfDocument();
//Create document level builder
using (PdfDocumentBuilder builder = new PdfDocumentBuilder(document))
{
builder.InsertParagraph();
//You can insert same image to different position in page, or insert different images to respective position
using (Stream sampleImage = File.OpenRead("sample.jpg"))
{
builder.InsertImageInline(sampleImage, new Size(40, 40));
builder.InsertText(", second one:");
builder.InsertImageInline(sampleImage, new Size(100, 100));
builder.InsertText(" and third one:");
builder.InsertImageInline(sampleImage, new Size(100, 60));
builder.InsertText(" the end.");
}
}
using (FileStream fs = File.OpenWrite("InsertImageInLine.pdf"))
{
PdfFile pdfFile = new PdfFile();
pdfFile.Export(document, fs);
}
}
public static void AddImageAsPage()
{
PdfDocument document = new PdfDocument();
using (Stream imgStream = File.OpenRead("sample.jpg"))
{
iDiTect.Pdf.Resources.ImageSource image = new iDiTect.Pdf.Resources.ImageSource(imgStream);
//Create a new page with image's size
PdfPage page = new PdfPage();
page.Size = new Size(image.Width, image.Height);
PageContentBuilder builder = new PageContentBuilder(page);
//draw image to page at position (0,0)
builder.DrawImage(image);
document.Pages.Add(page);
}
using (FileStream fs = File.OpenWrite("ConvertImageToPdf.pdf"))
{
PdfFile pdfFile = new PdfFile();
pdfFile.Export(document, fs);
}
}
public static void AddImageWatermark()
{
PdfFile pdfFile = new PdfFile();
PdfDocument document;
using (FileStream fs = File.OpenRead("sample.pdf"))
{
//Read pdf document from stream
document = pdfFile.Import(fs);
}
//Get first page of pdf
PdfPage page = document.Pages[0];
PageContentBuilder builder = new PageContentBuilder(page);
//Set watermark image position
builder.Position.Translate(100, 100);
using (Stream stream = File.OpenRead("watermark.png"))
{
//Insert watermark image as original size
builder.DrawImage(stream);
//Insert watermark image in customized size
//builder.DrawImage(stream, new Size(80, 80));
}
using (FileStream fs = File.OpenWrite("ImageWatermark.pdf"))
{
pdfFile.Export(document, fs);
}
}
}
}