-
Notifications
You must be signed in to change notification settings - Fork 1
/
Netcpdf.cs
3607 lines (3314 loc) · 130 KB
/
Netcpdf.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Text;
using System.Runtime.InteropServices;
using System.Collections.Generic;
namespace CoherentGraphics
{
///<summary>The Coherent PDF Library for .NET</summary>
public class Cpdf
{
///<summary>PDF document. Use the 'using' keyword, or call Dispose to make sure PDFs are deallocated.</summary>
public class Pdf: IDisposable
{
internal int pdf = -1;
private bool disposed = false;
internal Pdf(int pdf)
{
this.pdf = pdf;
}
///<summary>Force disposal of the PDF.</summary>
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
internal virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
[DllImport("cpdf")] static extern void cpdf_deletePdf(int pdf);
//Console.WriteLine($"**************deleting PDF {this.pdf}");
cpdf_deletePdf(this.pdf);
this.pdf = -1;
disposed = true;
}
}
///<summary>Class destructor</summary>
~Pdf()
{
Dispose(disposing: false);
}
}
/// <summary>Any function in this library may raise the CPDFError exception.</summary>
public class CPDFError : Exception
{
/// <summary>Construct a CPDFError which carries a string.</summary>
public CPDFError(string error) : base(error)
{
}
}
static void checkerror()
{
if (lastError() != 0)
{
clearError();
throw new CPDFError(lastErrorString());
}
}
static List<int> list_of_range(int r)
{
[DllImport("cpdf")] static extern int cpdf_rangeLength(int r);
[DllImport("cpdf")] static extern int cpdf_rangeGet(int r, int n);
List<int> l = new List<int>();
for (int x = 0; x < cpdf_rangeLength(r); x++)
{
l.Add(cpdf_rangeGet(r, x));
}
checkerror();
return l;
}
static int range_of_list(List<int> l)
{
[DllImport("cpdf")] static extern void cpdf_deleteRange(int r);
[DllImport("cpdf")] static extern int cpdf_blankRange();
[DllImport("cpdf")] static extern int cpdf_rangeAdd(int r, int n);
int r = cpdf_blankRange();
for (int x = 0; x < l.Count; x++)
{
int rn = cpdf_rangeAdd(r, l[x]);
cpdf_deleteRange(r);
r = rn;
}
checkerror();
return r;
}
/// <summary>CHAPTER 0. Preliminaries</summary>
private void dummych0()
{
}
/// <summary>
/// Initialises the library. Must be called before any other function.
/// </summary>
public static void startup()
{
[DllImport("cpdf")] static extern void cpdf_startup(IntPtr[] argv);
IntPtr[] args = {};
cpdf_startup(args);
checkerror();
}
/// <summary>
/// Returns a string giving the version number of the CPDF library.
/// </summary>
public static string version()
{
[DllImport("cpdf")] static extern IntPtr cpdf_version();
string s = Marshal.PtrToStringUTF8(cpdf_version());
checkerror();
return s;
}
/// <summary>
/// Some operations have a fast mode. The default is 'slow' mode, which works
/// even on old-fashioned files. For more details, see section 1.13 of the
/// CPDF manual. This function sets the mode to fast globally.
/// </summary>
public static void setFast()
{
[DllImport("cpdf")] static extern void cpdf_setFast();
cpdf_setFast();
checkerror();
}
/// <summary>
/// Some operations have a fast mode. The default is 'slow' mode, which works
/// even on old-fashioned files. For more details, see section 1.13 of the
/// CPDF manual. This functions sets the mode to slow globally.
/// </summary>
public static void setSlow()
{
[DllImport("cpdf")] static extern void cpdf_setSlow();
cpdf_setSlow();
checkerror();
}
/// <summary>
/// Not to be called directly. Errors in .NET cpdf are raised by exceptions.
/// </summary>
private static int lastError()
{
[DllImport("cpdf")] static extern int cpdf_fLastError();
return cpdf_fLastError();
}
/// <summary>
/// Not to be called directly. Errors in .NET cpdf are raised by exceptions.
/// </summary>
private static string lastErrorString()
{
[DllImport("cpdf")] static extern IntPtr cpdf_fLastErrorString();
return Marshal.PtrToStringUTF8(cpdf_fLastErrorString());
}
/// <summary>
/// Not to be called directly. Errors in .NET cpdf are raised by exceptions.
/// </summary>
private static void clearError()
{
[DllImport("cpdf")] static extern void cpdf_clearError();
cpdf_clearError();
}
/// <summary>
/// A debug function which prints some information about
/// resource usage. This can be used to detect if PDFs or ranges are being
/// deallocated properly. Contrary to its name, it may be run at any time.
/// </summary>
public static void onExit()
{
[DllImport("cpdf")] static extern void cpdf_onExit();
cpdf_onExit();
checkerror();
}
/// <summary>CHAPTER 1. Basics</summary>
private void dummych1()
{
}
/// <summary>
/// Loads a PDF file from a given file. Supply
/// a user password (possibly blank) in case the file is encrypted. It won't be
/// decrypted, but sometimes the password is needed just to load the file.
/// </summary>
public static Pdf fromFile(string filename, string userpw)
{
[DllImport("cpdf")] static extern int cpdf_fromFile(string filename, string userpw);
int res = cpdf_fromFile(filename, userpw);
checkerror();
return new Pdf(res);
}
/// <summary>
/// Loads a PDF from a file, doing only minimal
/// parsing. The objects will be read and parsed when they are actually
/// needed. Use this when the whole file won't be required. Also supply a user
/// password (possibly blank) in case the file is encrypted. It won't be
/// decrypted, but sometimes the password is needed just to load the file.
/// </summary>
public static Pdf fromFileLazy(string filename, string userpw)
{
[DllImport("cpdf")] static extern int cpdf_fromFileLazy(string filename, string userpw);
int res = cpdf_fromFileLazy(filename, userpw);
checkerror();
return new Pdf(res);
}
/// <summary>
/// Loads a file from memory given any user password.
/// </summary>
public static Pdf fromMemory(byte[] data, string userpw)
{
[DllImport("cpdf")] static extern int cpdf_fromMemory(byte[] data, int length, string userpw);
int pdf = cpdf_fromMemory(data, data.Length, userpw);
checkerror();
return new Pdf(pdf);
}
/// <summary>
/// Loads a file from memory, given a
/// pointer and a length, and the user password, but lazily like
/// fromFileLazy. The caller must use AllocHGlobal / Marshal.Copy / FreeHGlobal
/// itself. It must not free the memory until the PDF is also gone.
/// </summary>
public static Pdf fromMemoryLazy(IntPtr data, int length, string userpw)
{
[DllImport("cpdf")] static extern int cpdf_fromMemoryLazy(IntPtr data, int length, string userpw);
int pdf = cpdf_fromMemoryLazy(data, length, userpw);
checkerror();
return new Pdf(pdf);
}
/// <summary>
/// To enumerate the list of currently allocated PDFs, call
/// startEnumeratePDFs which gives the number, n, of PDFs allocated, then
/// enumeratePDFsInfo and enumeratePDFsKey with index numbers from
/// 0...(n - 1). Call endEnumeratePDFs to clean up.
/// </summary>
public static int startEnumeratePDFs()
{
[DllImport("cpdf")] static extern int cpdf_startEnumeratePDFs();
int res = cpdf_startEnumeratePDFs();
checkerror();
return res;
}
/// <summary>
/// To enumerate the list of currently allocated PDFs, call
/// startEnumeratePDFs which gives the number, n, of PDFs allocated, then
/// enumeratePDFsInfo and enumeratePDFsKey with index numbers from
/// 0...(n - 1). Call endEnumeratePDFs to clean up.
/// </summary>
public static int enumeratePDFsKey(int n)
{
[DllImport("cpdf")] static extern int cpdf_enumeratePDFsKey(int n);
int res = cpdf_enumeratePDFsKey(n);
checkerror();
return res;
}
/// <summary>
/// To enumerate the list of currently allocated PDFs, call
/// startEnumeratePDFs which gives the number, n, of PDFs allocated, then
/// enumeratePDFsInfo and enumeratePDFsKey with index numbers from
/// 0...(n - 1). Call endEnumeratePDFs to clean up.
/// </summary>
public static string enumeratePDFsInfo(int n)
{
[DllImport("cpdf")] static extern IntPtr cpdf_enumeratePDFsInfo(int n);
string res = Marshal.PtrToStringUTF8(cpdf_enumeratePDFsInfo(n));
checkerror();
return res;
}
/// <summary>
/// To enumerate the list of currently allocated PDFs, call
/// startEnumeratePDFs which gives the number, n, of PDFs allocated, then
/// enumeratePDFsInfo and enumeratePDFsKey with index numbers from
/// 0...(n - 1). Call endEnumeratePDFs to clean up.
/// </summary>
public static void endEnumeratePDFs()
{
[DllImport("cpdf")] static extern void cpdf_endEnumeratePDFs();
cpdf_endEnumeratePDFs();
checkerror();
}
/// <summary>
/// Converts a figure in centimetres to points (72 points to 1 inch)
/// </summary>
public static double ptOfCm(double i)
{
[DllImport("cpdf")] static extern double cpdf_ptOfCm(double i);
double res = cpdf_ptOfCm(i);
checkerror();
return res;
}
/// <summary>
/// Converts a figure in millimetres to points (72 points to 1 inch)
/// </summary>
public static double ptOfMm(double i)
{
[DllImport("cpdf")] static extern double cpdf_ptOfMm(double i);
double res = cpdf_ptOfMm(i);
checkerror();
return res;
}
/// <summary>
/// Converts a figure in inches to points (72 points to 1 inch)
/// </summary>
public static double ptOfIn(double i)
{
[DllImport("cpdf")] static extern double cpdf_ptOfIn(double i);
double res = cpdf_ptOfIn(i);
checkerror();
return res;
}
/// <summary>
/// Converts a figure in points to centimetres (72 points to 1 inch)
/// </summary>
public static double cmOfPt(double i)
{
[DllImport("cpdf")] static extern double cpdf_cmOfPt(double i);
double res = cpdf_cmOfPt(i);
checkerror();
return res;
}
/// <summary>
/// Converts a figure in points to millimetres (72 points to 1 inch)
/// </summary>
public static double mmOfPt(double i)
{
[DllImport("cpdf")] static extern double cpdf_mmOfPt(double i);
double res = cpdf_mmOfPt(i);
checkerror();
return res;
}
/// <summary>
/// Converts a figure in points to inches (72 points to 1 inch)
/// </summary>
public static double inOfPt(double i)
{
[DllImport("cpdf")] static extern double cpdf_inOfPt(double i);
double res = cpdf_inOfPt(i);
checkerror();
return res;
}
/// <summary>
/// Parses a page specification with reference
/// to a given PDF (the PDF is supplied so that page ranges which reference
/// pages which do not exist are rejected).
/// </summary>
public static List<int> parsePagespec(Pdf pdf, string pagespec)
{
[DllImport("cpdf")] static extern int cpdf_parsePagespec(int pdf, string pagespec);
[DllImport("cpdf")] static extern void cpdf_deleteRange(int r);
int r = cpdf_parsePagespec(pdf.pdf, pagespec);
List<int> r_out = list_of_range(r);
cpdf_deleteRange(r);
checkerror();
return r_out;
}
/// <summary>
/// Validates a page specification so far as is
/// possible in the absence of the actual document. Result is true if valid.
/// </summary>
public static bool validatePagespec(string pagespec)
{
[DllImport("cpdf")] static extern int cpdf_validatePagespec(string pagespec);
int res = cpdf_validatePagespec(pagespec);
checkerror();
return (res > 0);
}
/// <summary>
/// Builds a page specification from a page
/// range. For example, the range containing 1,2,3,6,7,8 in a document of 8
/// pages might yield "1-3,6-end"
/// </summary>
public static string stringOfPagespec(Pdf pdf, List<int> r)
{
[DllImport("cpdf")] static extern IntPtr cpdf_stringOfPagespec(int pdf, int r);
[DllImport("cpdf")] static extern void cpdf_deleteRange(int r);
int rn = range_of_list(r);
string s = Marshal.PtrToStringUTF8(cpdf_stringOfPagespec(pdf.pdf, rn));
cpdf_deleteRange(rn);
checkerror();
return s;
}
/// <summary>
/// Creates a range with no pages in.
/// </summary>
public static List<int> blankRange()
{
return new List<int>();
}
/// <summary>
/// Builds a range from one page to another inclusive. For
/// example, range(3,7) gives the range 3,4,5,6,7
/// </summary>
public static List<int> range(int f, int t)
{
[DllImport("cpdf")] static extern int cpdf_range(int f, int t);
[DllImport("cpdf")] static extern void cpdf_deleteRange(int r);
int rn = cpdf_range(f, t);
List<int> l = list_of_range(rn);
cpdf_deleteRange(rn);
checkerror();
return l;
}
/// <summary>
/// The range containing all the pages in a given document.
/// </summary>
public static List<int> all(Pdf pdf)
{
[DllImport("cpdf")] static extern int cpdf_all(int pdf);
[DllImport("cpdf")] static extern void cpdf_deleteRange(int r);
int rn = cpdf_all(pdf.pdf);
List<int> r = list_of_range(rn);
cpdf_deleteRange(rn);
checkerror();
return r;
}
/// <summary>
/// Makes a range which contains just the even pages of
/// another range.
/// </summary>
public static List<int> even(List<int> r_in)
{
[DllImport("cpdf")] static extern int cpdf_even(int pdf);
[DllImport("cpdf")] static extern void cpdf_deleteRange(int r);
int range_in = range_of_list(r_in);
int rn = cpdf_even(range_in);
List<int> r = list_of_range(rn);
cpdf_deleteRange(rn);
cpdf_deleteRange(range_in);
checkerror();
return r;
}
/// <summary>
/// Makes a range which contains just the odd pages of another
/// range.
/// </summary>
public static List<int> odd(List<int> r_in)
{
[DllImport("cpdf")] static extern int cpdf_odd(int pdf);
[DllImport("cpdf")] static extern void cpdf_deleteRange(int r);
int range_in = range_of_list(r_in);
int rn = cpdf_odd(range_in);
List<int> r = list_of_range(rn);
cpdf_deleteRange(rn);
cpdf_deleteRange(range_in);
checkerror();
return r;
}
/// <summary>
/// Makes the union of two ranges giving a range
/// containing the pages in range a and range b.
/// </summary>
public static List<int> rangeUnion(List<int> a, List<int> b)
{
[DllImport("cpdf")] static extern int cpdf_rangeUnion(int a, int b);
[DllImport("cpdf")] static extern void cpdf_deleteRange(int r);
int a_r = range_of_list(a);
int b_r = range_of_list(b);
int rn = cpdf_rangeUnion(a_r, b_r);
List<int> r = list_of_range(rn);
cpdf_deleteRange(a_r);
cpdf_deleteRange(b_r);
cpdf_deleteRange(rn);
checkerror();
return r;
}
/// <summary>
/// Makes the difference of two ranges, giving a range
/// containing all the pages in a except for those which are also in b.
/// </summary>
public static List<int> difference(List<int> a, List<int> b)
{
[DllImport("cpdf")] static extern int cpdf_difference(int a, int b);
[DllImport("cpdf")] static extern void cpdf_deleteRange(int r);
int a_r = range_of_list(a);
int b_r = range_of_list(b);
int rn = cpdf_difference(a_r, b_r);
List<int> r = list_of_range(rn);
cpdf_deleteRange(a_r);
cpdf_deleteRange(b_r);
cpdf_deleteRange(rn);
checkerror();
return r;
}
/// <summary>
/// Deduplicates a range, making a new one.
/// </summary>
public static List<int> removeDuplicates(List<int> a)
{
[DllImport("cpdf")] static extern int cpdf_removeDuplicates(int r);
[DllImport("cpdf")] static extern void cpdf_deleteRange(int r);
int a_r = range_of_list(a);
int rn = cpdf_removeDuplicates(a_r);
List<int> r = list_of_range(rn);
cpdf_deleteRange(a_r);
cpdf_deleteRange(rn);
checkerror();
return r;
}
/// <summary>
/// Gives the number of pages in a range.
/// </summary>
public static int rangeLength(List<int> r)
{
[DllImport("cpdf")] static extern int cpdf_rangeLength(int r);
[DllImport("cpdf")] static extern void cpdf_deleteRange(int r);
int rn = range_of_list(r);
int l = cpdf_rangeLength(rn);
cpdf_deleteRange(rn);
checkerror();
return l;
}
/// <summary>
/// Gets the page number at position n in a range,
/// where n runs from 0 to rangeLength - 1.
/// </summary>
public static int rangeGet(List<int> r, int n)
{
[DllImport("cpdf")] static extern int cpdf_rangeGet(int r, int n);
[DllImport("cpdf")] static extern void cpdf_deleteRange(int r);
int rn = range_of_list(r);
int n_out = cpdf_rangeGet(rn, n);
cpdf_deleteRange(rn);
checkerror();
return n_out;
}
/// <summary>
/// Adds the page to a range, if it is not already
/// there.
/// </summary>
public static List<int> rangeAdd(List<int> r, int page)
{
[DllImport("cpdf")] static extern int cpdf_rangeAdd(int r, int page);
[DllImport("cpdf")] static extern void cpdf_deleteRange(int r);
int rn = range_of_list(r);
int r2 = cpdf_rangeAdd(rn, page);
List<int> r_out = list_of_range(r2);
cpdf_deleteRange(rn);
cpdf_deleteRange(r2);
checkerror();
return r_out;
}
/// <summary>
/// Returns true if the page is in the range,
/// false otherwise.
/// </summary>
public static bool isInRange(List<int> r, int page)
{
[DllImport("cpdf")] static extern int cpdf_isInRange(int r, int page);
[DllImport("cpdf")] static extern void cpdf_deleteRange(int r);
int rn = range_of_list(r);
int res = cpdf_isInRange(rn, page);
cpdf_deleteRange(rn);
checkerror();
return (res > 0);
}
/// <summary>
/// Returns the number of pages in a PDF.
/// </summary>
public static int pages(Pdf pdf)
{
[DllImport("cpdf")] static extern int cpdf_pages(int pdf);
int res = cpdf_pages(pdf.pdf);
checkerror();
return res;
}
/// <summary>
/// Returns the number of pages in a given
/// PDF, with given user password. It tries to do this as fast as
/// possible, without loading the whole file.
/// </summary>
public static int pagesFast(string password, string filename)
{
[DllImport("cpdf")] static extern int cpdf_pagesFast(string password, string filename);
int res = cpdf_pagesFast(password, filename);
checkerror();
return res;
}
/// <summary>
/// Writes the file to a given
/// filename. If linearize is true, it will be linearized if a linearizer is
/// available. If make_id is true, it will be given a new ID.
/// </summary>
public static void toFile(Pdf pdf, string filename, bool linearize, bool make_id)
{
[DllImport("cpdf")] static extern void cpdf_toFile(int pdf, string filename, int linearize, int make_id);
cpdf_toFile(pdf.pdf, filename, linearize ? 1 : 0, make_id ? 1 : 0);
checkerror();
}
/// <summary>
/// Writes the file to a given filename. If
/// make_id is true, it will be given a new ID. If preserve_objstm is true,
/// existing object streams will be preserved. If generate_objstm is true,
/// object streams will be generated even if not originally present. If
/// compress_objstm is true, object streams will be compressed (what we
/// usually want). WARNING: the pdf argument will be invalid after this call,
/// and should be not be used again.
/// </summary>
public static void toFileExt(Pdf pdf, string filename, bool linearize, bool make_id, bool preserve_objstm, bool generate_objstm, bool compress_objstm)
{
[DllImport("cpdf")] static extern void cpdf_toFileExt(int pdf, string filename, int linearize, int make_id, int preserve_objstm, int generate_objstm, int compress_objstm);
cpdf_toFileExt(pdf.pdf, filename, linearize ? 1 : 0, make_id ? 1 : 0, preserve_objstm ? 1 : 0, generate_objstm ? 1 : 0, compress_objstm ? 1 : 0);
checkerror();
}
/// <summary>
/// Writes a PDF file
/// and returns as an array of bytes.
/// </summary>
public static byte[] toMemory(Pdf pdf, bool linearize, bool makeid)
{
[DllImport("cpdf")] static extern IntPtr cpdf_toMemory(int pdf, int linearize, int makeid, ref int len);
int len = 0;
IntPtr data = cpdf_toMemory(pdf.pdf, linearize ? 1 : 0, makeid ? 1 : 0, ref len);
var databytes = new byte[len];
Marshal.Copy(data, databytes, 0, len);
[DllImport("cpdf")] static extern void cpdf_free(IntPtr ptr);
cpdf_free(data);
checkerror();
return databytes;
}
/// <summary>
/// Returns true if a document is encrypted, false
/// otherwise.
/// </summary>
public static bool isEncrypted(Pdf pdf)
{
[DllImport("cpdf")] static extern int cpdf_isEncrypted(int pdf);
int res = cpdf_isEncrypted(pdf.pdf);
checkerror();
return (res > 0);
}
/// <summary>
/// Attempts to decrypt a PDF using the given
/// user password. An exception is raised if the decryption fails.
/// </summary>
public static void decryptPdf(Pdf pdf, string userpw)
{
[DllImport("cpdf")] static extern void cpdf_decryptPdf(int pdf, string userpw);
cpdf_decryptPdf(pdf.pdf, userpw);
checkerror();
}
/// <summary>
/// Attempts to decrypt a PDF using the
/// given owner password. Raises an exception if the decryption fails.
/// </summary>
public static void decryptPdfOwner(Pdf pdf, string ownerpw)
{
[DllImport("cpdf")] static extern void cpdf_decryptPdfOwner(int pdf, string ownerpw);
cpdf_decryptPdfOwner(pdf.pdf, ownerpw);
checkerror();
}
/// <summary>Permissions</summary>
public enum Permission
{
/// <summary>Cannot edit the document</summary>
NoEdit,
/// <summary>Cannot print the document</summary>
NoPrint,
/// <summary>Cannot copy the document</summary>
NoCopy,
/// <summary>Cannot annotate the document</summary>
NoAnnot,
/// <summary>Cannot edit forms in the document</summary>
NoForms,
/// <summary>Cannot extract information</summary>
NoExtract,
/// <summary>Cannot assemble into a bigger document</summary>
NoAssemble,
/// <summary>Cannot print high quality</summary>
NoHqPrint
}
/// <summary>Encryption methods</summary>
public enum EncryptionMethod
{
/// <summary>40 bit RC4 encryption</summary>
Pdf40bit,
/// <summary>128 bit RC4 encryption</summary>
Pdf128bit,
/// <summary>128 bit AES encryption, do not encrypt metadata</summary>
Aes128bitfalse,
/// <summary>128 bit AES encryption, encrypt metadata</summary>
Aes128bittrue,
/// <summary>Deprecated. Do not use for new files</summary>
Aes256bitfalse,
/// <summary>Deprecated. Do not use for new files</summary>
Aes256bittrue,
/// <summary>256 bit AES encryption, do not encrypt metadata</summary>
Aes256bitisofalse,
/// <summary>256 bit AES encryption, encrypt metadata</summary>
Aes256bitiosotrue,
}
/// <summary>
/// Writes a file as encrypted.
/// </summary>
public static void toFileEncrypted(Pdf pdf, EncryptionMethod encryption_method, List<Permission> permissions, string ownerpw, string userpw, bool linearize, bool makeid, string filename)
{
[DllImport("cpdf")] static extern void cpdf_toFileEncrypted(int pdf, int encryption_method, int[] permissions, int permission_length, string ownerpw, string userpw, int linearize, int makeid, string filename);
int[] perms = Array.ConvertAll(permissions.ToArray(), value => (int) value);
cpdf_toFileEncrypted(pdf.pdf, (int) encryption_method, perms, permissions.Count, ownerpw, userpw, linearize ? 1 : 0, makeid ? 1 : 0, filename);
checkerror();
}
/// <summary>
/// Writes a file as encrypted with extra parameters. WARNING: the
/// pdf argument will be invalid after this call, and should not be used again.
/// </summary>
public static void toFileEncryptedExt(Pdf pdf, EncryptionMethod encryption_method, List<Permission> permissions, string ownerpw, string userpw, bool linearize, bool makeid, bool preserve_objstm, bool generate_objstm, bool compress_objstm, string filename)
{
[DllImport("cpdf")] static extern void cpdf_toFileEncryptedExt(int pdf, int encryption_method, int[] permissions, int permission_length, string ownerpw, string userpw, int linearize, int makeid, int preserve_objstm, int generate_objstm, int compress_objstm, string filename);
int[] perms = Array.ConvertAll(permissions.ToArray(), value => (int) value);
cpdf_toFileEncryptedExt(pdf.pdf, (int) encryption_method, perms, permissions.Count, ownerpw, userpw, linearize ? 1 : 0, makeid ? 1 : 0, preserve_objstm ? 1 : 0, generate_objstm ? 1 : 0, compress_objstm ? 1 : 0, filename);
checkerror();
}
/// <summary>
/// Returns true if the given permission
/// (restriction) is present.
/// </summary>
public static bool hasPermission(Pdf pdf, Permission permission)
{
[DllImport("cpdf")] static extern int cpdf_hasPermission(int pdf, int permission);
int res = cpdf_hasPermission(pdf.pdf, (int) permission);
checkerror();
return (res > 0);
}
/// <summary>
/// Returns the encryption method currently in use on
/// a document.
/// </summary>
public static int encryptionKind(Pdf pdf)
{
[DllImport("cpdf")] static extern int cpdf_encryptionKind(int pdf);
int res = cpdf_encryptionKind(pdf.pdf);
return res;
}
/// <summary>CHAPTER 2. Merging and Splitting</summary>
private void dummych2()
{
}
/// <summary>
/// Given a list of PDFs,
/// merges the files into a new one, which is returned.
/// </summary>
public static Pdf mergeSimple(List<Pdf> pdfs)
{
[DllImport("cpdf")] static extern int cpdf_mergeSimple(int[] pdfs, int length);
List<int> c_pdfs_lst = new List<int>();
for (int x = 0; x < pdfs.Count; x++)
{
c_pdfs_lst.Add(pdfs[x].pdf);
}
int res = cpdf_mergeSimple(c_pdfs_lst.ToArray(), pdfs.Count);
checkerror();
return new Pdf(res);
}
/// <summary>
/// Merges the
/// PDFs. If retain_numbering is true page labels are not rewritten. If
/// remove_duplicate_fonts is true, duplicate fonts are merged. This is useful
/// when the source documents for merging originate from the same source.
/// </summary>
public static Pdf merge(List<Pdf> pdfs, bool retain_numbering, bool remove_duplicate_fonts)
{
[DllImport("cpdf")] static extern int cpdf_merge(int[] pdfs, int length, int retain_numbering, int remove_duplicate_fonts);
List<int> c_pdfs_lst = new List<int>();
for (int x = 0; x < pdfs.Count; x++)
{
c_pdfs_lst.Add(pdfs[x].pdf);
}
int res = cpdf_merge(c_pdfs_lst.ToArray(), pdfs.Count, retain_numbering ? 1 : 0, remove_duplicate_fonts ? 1 : 0);
checkerror();
return new Pdf(res);
}
/// <summary>
/// The same as merge, except that it has an additional
/// argument - a list of page ranges. This is used to select the pages to
/// pick from each PDF. This avoids duplication of information when multiple
/// discrete parts of a source PDF are included.
/// </summary>
public static Pdf mergeSame(List<Pdf> pdfs, bool retain_numbering, bool remove_duplicate_fonts, List<List<int>> ranges)
{
[DllImport("cpdf")] static extern int cpdf_mergeSame(int[] pdfs, int length, int retain_numbering, int remove_duplicate_fonts, int[] ranges);
[DllImport("cpdf")] static extern void cpdf_deleteRange(int r);
List<int> c_pdfs_lst = new List<int>();
for (int x = 0; x < pdfs.Count; x++)
{
c_pdfs_lst.Add(pdfs[x].pdf);
}
int[] c_pdfs = c_pdfs_lst.ToArray();
List<int> int_ranges = ranges.ConvertAll(range_of_list);
int[] c_ranges = int_ranges.ToArray();
int result = cpdf_mergeSame(c_pdfs, pdfs.Count, retain_numbering ? 1 : 0, remove_duplicate_fonts ? 1 : 0, c_ranges);
for (int x = 0; x < c_ranges.Length; x++) {
cpdf_deleteRange(c_ranges[x]);
}
checkerror();
return new Pdf(result);
}
/// <summary>
/// Returns a new document with just those pages
/// in the page range.
/// </summary>
public static Pdf selectPages(Pdf pdf, List<int> r)
{
[DllImport("cpdf")] static extern int cpdf_selectPages(int pdf, int r);
[DllImport("cpdf")] static extern void cpdf_deleteRange(int r);
int rn = range_of_list(r);
int res = cpdf_selectPages(pdf.pdf, rn);
cpdf_deleteRange(rn);
checkerror();
return new Pdf(res);
}
/// <summary>CHAPTER 3. Pages</summary>
private void dummych3()
{
}
/// <summary>
/// Scales the page dimensions
/// and content by the given scale, about (0, 0). Other boxes (crop etc. are
/// altered as appropriate)
/// </summary>
public static void scalePages(Pdf pdf, List<int> range, double sx, double sy)
{
[DllImport("cpdf")] static extern void cpdf_scalePages(int pdf, int range, double sx, double sy);
[DllImport("cpdf")] static extern void cpdf_deleteRange(int r);
int rn = range_of_list(range);
cpdf_scalePages(pdf.pdf, rn, sx, sy);
cpdf_deleteRange(rn);
checkerror();
}
/// <summary>
/// Scales the content to fit
/// new page dimensions (width x height) multiplied by scale (typically 1.0).
/// Other boxes (crop etc. are altered as appropriate)
/// </summary>
public static void scaleToFit(Pdf pdf, List<int> range, double sx, double sy, double scale)
{
[DllImport("cpdf")] static extern void cpdf_scaleToFit(int pdf, int range, double sx, double sy, double scale);
[DllImport("cpdf")] static extern void cpdf_deleteRange(int r);
int rn = range_of_list(range);
cpdf_scaleToFit(pdf.pdf, rn, sx, sy, scale);
cpdf_deleteRange(rn);
checkerror();
}
/// <summary>Built-in paper sizes</summary>
public enum Papersize
{
/// <summary>A0 Portrait paper</summary>
A0portrait,
/// <summary>A1 Portrait paper</summary>
A1portrait,
/// <summary>A2 Portrait paper</summary>
A2portrait,
/// <summary>A3 Portrait paper</summary>
A3portrait,
/// <summary>A4 Portrait paper</summary>
A4portrait,
/// <summary>A5 Portrait paper</summary>
A5portrait,
/// <summary>A0 Landscape paper</summary>
A0landscape,
/// <summary>A1 Landscape paper</summary>
A1landscape,
/// <summary>A2 Landscape paper</summary>
A2landscape,
/// <summary>A3 Landscape paper</summary>
A3landscape,
/// <summary>A4 Landscape paper</summary>
A4landscape,
/// <summary>A5 Landscape paper</summary>
A5landscape,
/// <summary>US Letter Portrait paper</summary>
Usletterportrait,
/// <summary>US Letter Landscape paper</summary>
Usletterlandscape,
/// <summary>US Legal Portrait paper</summary>
Uslegalportrait,
/// <summary>US Legal Landscape paper</summary>
Uslegallandscape
}
/// <summary>
/// Scales the page content
/// to fit the given page size, possibly multiplied by scale (typically 1.0)
/// </summary>
public static void scaleToFitPaper(Pdf pdf, List<int> range, Papersize papersize, double scale)
{
[DllImport("cpdf")] static extern void cpdf_scaleToFitPaper(int pdf, int range, int papersize, double scale);
[DllImport("cpdf")] static extern void cpdf_deleteRange(int r);
int rn = range_of_list(range);
cpdf_scaleToFitPaper(pdf.pdf, rn, (int) papersize, scale);
cpdf_deleteRange(rn);
checkerror();
}
/// <summary>Position anchors</summary>
public enum Anchor
{
/// <summary>Absolute centre</summary>
PosCentre,
/// <summary>Absolute left</summary>
PosLeft,
/// <summary>Absolute right</summary>
PosRight,
/// <summary>The top centre of the page</summary>
Top,
/// <summary>The top left of the page</summary>
TopLeft,
/// <summary>The top right of the page</summary>
TopRight,
/// <summary>The left hand side of the page, halfway down</summary>
Left,
/// <summary>The bottom left of the page</summary>
BottomLeft,
/// <summary>The bottom middle of the page</summary>
Bottom,
/// <summary>The bottom right of the page</summary>
BottomRight,
/// <summary>The right hand side of the page, halfway down</summary>
Right,
/// <summary>Diagonal, bottom left to top right</summary>
Diagonal,
/// <summary>Diagonal, top left to bottom right</summary>
ReverseDiagonal
}
/// <summary>Positions on the page. Used for scaling about a point, and adding text.
/// A position is an anchor and zero or one or two parameters. Constructors are provided.
/// <para> </para>
/// PosCentre: Two parameters, x and y
/// <para> </para>
/// PosLeft: Two parameters, x and y
/// <para> </para>
/// PosRight: Two parameters, x and y
/// <para> </para>
/// Top: One parameter - distance from top
/// <para> </para>
/// TopLeft: One parameter - distance from top left
/// <para> </para>
/// TopRight: One parameter - distance from top right
/// <para> </para>