-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPDFworkerClass.cs
More file actions
200 lines (125 loc) · 4.84 KB
/
Copy pathPDFworkerClass.cs
File metadata and controls
200 lines (125 loc) · 4.84 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;
namespace PDFworker
{
class PDFworkerClass
{
// źródło kodu
// https://stackoverflow.com/questions/3579058/rotating-pdf-in-c-sharp-using-itextsharp
public static void RotatePages(string inputFile, string outputFile,
int start, int end)
{
// get input document
PdfReader inputPdf = new PdfReader(inputFile);
// retrieve the total number of pages
int pageCount = inputPdf.NumberOfPages;
if (end < start || end > pageCount)
{
end = pageCount;
}
// load the input document
Document inputDoc =
new Document(inputPdf.GetPageSizeWithRotation(1));
// create the filestream
using (FileStream fs = new FileStream(outputFile, FileMode.Create))
{
// create the output writer
PdfWriter outputWriter = PdfWriter.GetInstance(inputDoc, fs);
inputDoc.Open();
PdfContentByte cb1 = outputWriter.DirectContent;
// copy pages from input to output document
for (int i = start; i <= end; i++)
{
inputDoc.SetPageSize(inputPdf.GetPageSizeWithRotation(1));
inputDoc.NewPage();
PdfImportedPage page =
outputWriter.GetImportedPage(inputPdf, i);
int rotation = inputPdf.GetPageRotation(i);
if (rotation == 90 || rotation == 270)
{
cb1.AddTemplate(page, 0, -1f, 1f, 0, 0,
inputPdf.GetPageSizeWithRotation(i).Height);
}
else
{
cb1.AddTemplate(page, -1f, 0, 0, -1f,
inputPdf.GetPageSizeWithRotation(i).Width,
inputPdf.GetPageSizeWithRotation(i).Height);
}
}
inputDoc.Close();
}
}
public static void Test(string inputFile, string outputFile)
{
Console.WriteLine("Test");
String formFile = inputFile;
String newFile = outputFile;
PdfReader reader = new PdfReader(formFile);
PdfDictionary dict = reader.GetPageN(1);
foreach (var v in dict)
{
Console.WriteLine("{0}", v);
}
PdfObject obj = dict.GetDirectObject(PdfName.CONTENTS);
if (obj is PRStream stream)
{
byte[] data = PdfReader.GetStreamBytes(stream);
string dd = System.Text.Encoding.ASCII.GetString(data);
Console.WriteLine(dd);
stream.SetData(Encoding.ASCII.GetBytes(dd));
}
//PdfStamper stamper = new PdfStamper(reader, new FileStream(newFile, FileMode.Create));
//AcroFields fields = reader.AcroFields; // stamper.AcroFields;
//foreach(var v in fields.Fields)
//{
// Console.WriteLine("{0}", v);
//}
// set form fields
//fields.SetField("{TO}", "John Doe");
//fields.SetField("{FROM}", "2 Milky Way, London");
//stamper.FormFlattening = true;
//stamper.Close();
reader.Close();
}
public static void CopyPages(string inputFile, string outputFile,
int start, int end)
{
PdfReader inputPdf = new PdfReader(inputFile);
PdfReader.unethicalreading = true;
// retrieve the total number of pages
int pageCount = inputPdf.NumberOfPages;
if (end < start || end > pageCount)
{
end = pageCount;
}
using (var output = new MemoryStream())
{
var document = new Document();
var writer = new PdfCopy(document, output);
document.Open();
foreach (var file in new[] { inputFile })
{
var reader = new PdfReader(file);
PdfReader.unethicalreading = true;
PdfImportedPage page;
for (int p = start; p <= end; p++)
{
page = writer.GetImportedPage(reader, p);
writer.AddPage(page);
}
}
document.Close();
File.WriteAllBytes(outputFile, output.ToArray());
}
}
}
}