forked from wmjordan/PDFPatcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPdfContentExport.cs
694 lines (659 loc) · 22.2 KB
/
PdfContentExport.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using iTextSharp.text.pdf;
using PDFPatcher.Common;
using PDFPatcher.Model;
using Matrix = iTextSharp.text.pdf.parser.Matrix;
using NameValuePair = System.Collections.Generic.KeyValuePair<string, string>;
namespace PDFPatcher.Processor
{
sealed class PdfContentExport
{
readonly ExporterOptions _options;
readonly Dictionary<string, string> _resolvedReferences = new Dictionary<string, string>();
readonly List<string> _exportPath = new List<string>();
ImageExtractor _imageExporter;
bool AddReferenceRecord(PdfIndirectReference r, string type) {
var k = String.Concat(r.Number, ' ', r.Generation);
if (_resolvedReferences.ContainsKey(k)) {
return false;
}
_resolvedReferences.Add(k, type);
return true;
}
public PdfContentExport(ExporterOptions options) {
_options = options;
}
internal void ExportTrailer(XmlWriter writer, PdfReader reader) {
Tracker.TraceMessage("导出文档总索引。");
writer.WriteStartElement("Trailer");
writer.WriteAttributeString(Constants.ContentPrefix, "http://www.w3.org/2000/xmlns/", Constants.ContentNamespace);
foreach (var item in reader.Trailer) {
if (PdfName.ROOT.Equals(item.Key) == false) {
ExportPdfDictionaryItem(item, writer);
}
}
ExportCatalog(writer, reader);
writer.WriteEndElement();
}
void ExportCatalog(XmlWriter writer, PdfReader reader) {
Tracker.TraceMessage("导出文档编录。");
writer.WriteStartElement(Constants.Catalog);
writer.WriteAttributeString(Constants.ContentPrefix, "http://www.w3.org/2000/xmlns/", Constants.ContentNamespace);
foreach (var item in reader.Catalog) {
if (PdfName.OUTLINES.Equals(item.Key)) {
if (PdfReader.GetPdfObjectRelease(item.Value) is PdfDictionary o) {
ExportPdfOutline(o, writer);
}
}
else {
ExportPdfDictionaryItem(item, writer);
}
}
writer.WriteEndElement();
}
internal void ExportContents(XmlWriter writer, PdfReader reader) {
if (_options.ExtractImages) {
_imageExporter = new ImageExtractor(_options.Images, reader);
}
writer.WriteStartElement(Constants.Body);
writer.WriteAttributeString(Constants.ContentPrefix, "http://www.w3.org/2000/xmlns/", Constants.ContentNamespace);
var ranges = PageRangeCollection.Parse(_options.ExtractPageRange, 1, reader.NumberOfPages, true);
ExtractPages(reader, ranges, writer);
writer.WriteEndElement();
}
void ExtractPages(PdfReader reader, PageRangeCollection ranges, XmlWriter writer) {
var p = new ExportProcessor(this, writer, _options);
foreach (PageRange r in ranges) {
foreach (var i in r) {
Tracker.TraceMessage(String.Concat("导出第 ", i, " 页。"));
ExtractPage(i, reader, writer, p);
Tracker.IncrementProgress(1);
}
}
}
void ExportPdfOutline(PdfDictionary outline, XmlWriter writer) {
while (outline != null) {
writer.WriteStartElement(Constants.ContentPrefix, "Outline", Constants.ContentNamespace);
foreach (var i in outline) {
switch (i.Key.ToString()) {
case "/First":
case "/Last":
case "/Next":
case "/Parent":
case "/Prev":
continue;
default:
ExportPdfDictionaryItem(i, writer);
break;
}
}
if (PdfReader.GetPdfObjectRelease(outline.Get(PdfName.FIRST)) is PdfDictionary f) {
writer.WriteStartElement(Constants.ContentPrefix, "Outlines", Constants.ContentNamespace);
ExportPdfOutline(f, writer);
writer.WriteEndElement();
}
writer.WriteEndElement();
outline = PdfReader.GetPdfObjectRelease(outline.Get(PdfName.NEXT)) as PdfDictionary;
}
}
/// <summary>
/// 导出 PDFDictionary。
/// </summary>
/// <param name="writer"></param>
/// <param name="dict"></param>
void ExportPdfDictionary(XmlWriter writer, PdfDictionary dict) {
foreach (var item in dict) {
ExportPdfDictionaryItem(item, writer);
}
}
void ExportPdfDictionaryItem(KeyValuePair<PdfName, PdfObject> item, XmlWriter writer) {
var key = PdfHelper.DecodeKeyName(item.Key);
var text = item.Value.ToString();
var value = item.Value as PdfObject;
try {
writer.WriteStartElement(XmlConvert.VerifyNCName(key));
}
catch (XmlException) {
writer.WriteStartElement(Constants.Content.Item, Constants.ContentNamespace);
writer.WriteAttributeString(Constants.Content.Name, Constants.ContentNamespace, key);
}
_exportPath.Add(key);
if (value == null) {
writer.WriteAttributeString(PdfHelper.GetTypeName(value.Type), text);
goto EndElement;
}
switch (value.Type) {
case PdfObject.ARRAY:
var a = value as PdfArray;
if (key == "ColorSpace" && PdfName.INDEXED.Equals(a.GetAsName(0)) && a.ArrayList.Count == 4) {
ExportColorSpaceContent(writer, a);
}
else {
ExportArray(a.ArrayList, writer);
}
break;
case PdfObject.STRING:
text = (value as PdfString).ToUnicodeString();
if (text.StartsWith("<?xml")) {
writer.WriteStartElement(Constants.ContentPrefix, Constants.Content.Value, Constants.ContentNamespace);
writer.WriteCData(text);
writer.WriteEndElement();
}
else {
writer.WriteAttributeString(PdfHelper.GetTypeName(value.Type), PdfHelper.GetValidXmlString(text));
}
break;
case PdfObject.INDIRECT:
writer.WriteAttributeString(Constants.Content.Type, Constants.ContentNamespace, PdfHelper.GetTypeName(PdfObject.INDIRECT));
ExportIndirectReference(value as PdfIndirectReference, writer, key);
break;
case PdfObject.DICTIONARY:
ExportPdfDictionary(writer, value as PdfDictionary);
break;
default:
writer.WriteAttributeString(PdfHelper.GetTypeName(value.Type), text);
break;
}
EndElement:
writer.WriteEndElement();
_exportPath.RemoveAt(_exportPath.Count - 1);
}
void ExportColorSpaceContent(XmlWriter writer, PdfArray a) {
writer.WriteStartElement(Constants.ContentPrefix, "colorSpace", Constants.ContentNamespace);
writer.WriteAttributeString("type", a[0].ToString());
var b = a.GetAsName(1);
if (b != null) {
writer.WriteAttributeString("base", a.GetAsName(1).ToString());
}
writer.WriteAttributeString("hival", a.GetAsNumber(2).ToString());
writer.WriteStartAttribute("lookup");
var cs = a.GetDirectObject(3);
byte[] l = null;
if (cs.IsString()) {
l = (cs as PdfString).GetOriginalBytes();
}
else if (cs.IsStream()) {
l = PdfReader.GetStreamBytes(cs as PRStream);
}
if (l != null) {
int g;
if (b == null) {
g = l.Length;
}
else if (b.Equals(PdfName.DEVICERGB)) {
g = 3;
}
else if (b.Equals(PdfName.DEVICECMYK)) {
g = 4;
}
else {
g = l.Length;
}
for (int i = 0; i < l.Length; i += g) {
writer.WriteBinHex(l, i, g);
writer.WriteString(" ");
}
}
writer.WriteEndAttribute();
if (a[1].IsArray()) {
ExportArray(a.GetAsArray(1).ArrayList, writer);
}
writer.WriteEndElement();
}
void ExportArray(List<PdfObject> array, XmlWriter writer) {
writer.WriteStartElement(Constants.ContentPrefix, "array", Constants.ContentNamespace);
ExportArrayContent(array, writer, false);
writer.WriteEndElement();
}
void ExportArrayContent(List<PdfObject> array, XmlWriter writer, bool writeStringBytes) {
int iType = PdfObject.NULL;
foreach (PdfObject i in array) {
if (iType == PdfObject.NULL) {
iType = i.Type;
}
else if (iType != i.Type) {
iType = PdfObject.NULL;
break;
}
}
if (ValueHelper.IsInCollection(iType, PdfObject.NAME, PdfObject.NUMBER, PdfObject.BOOLEAN)) {
writer.WriteAttributeString(Constants.Content.Type, PdfHelper.GetTypeName(iType));
writer.WriteStartAttribute(Constants.Content.Value);
var l = array;
for (int i = 0; i < l.Count; i++) {
if (i > 0) {
writer.WriteString(" ");
}
writer.WriteString(l[i].ToString());
}
writer.WriteEndAttribute();
}
else {
foreach (PdfObject i in array) {
if (i is not PdfArray subArray) {
if (PdfHelper.GetTypeName(i.Type).Length > 0) {
writer.WriteStartElement(Constants.ContentPrefix, PdfHelper.GetTypeName(i.Type), Constants.ContentNamespace);
}
#if DEBUG
else {
writer.WriteElementString("literal", i.ToString());
continue;
}
#endif
switch (i.Type) {
case PdfObject.INDIRECT:
ExportIndirectReference(i as PdfIndirectReference, writer, String.Empty);
break;
case PdfObject.DICTIONARY:
ExportPdfDictionary(writer, i as PdfDictionary);
break;
case PdfObject.STRING:
if (writeStringBytes) {
var bytes = (i as PdfString).GetBytes();
var bl = bytes.Length;
if (bl > 0) {
int l = 2 * bl + ((bl + 1) >> 1) - 1;
var chArray = new char[l];
int num = 0, bi = 0;
foreach (byte b in bytes) {
if ((bi % 2) == 0 && bi > 0) {
chArray[num++] = ' ';
}
chArray[num++] = "0123456789ABCDEF"[b >> 4];
chArray[num++] = "0123456789ABCDEF"[b & 15];
bi++;
}
writer.WriteAttributeString(Constants.Content.Type, "字节");
writer.WriteAttributeString(Constants.Content.Value, new string(chArray));
}
break;
}
goto default;
default:
writer.WriteAttributeString(Constants.Content.Value, PdfHelper.GetValidXmlString(i.ToString()));
break;
}
writer.WriteEndElement();
}
else {
writer.WriteStartElement(Constants.ContentPrefix, "array", Constants.ContentNamespace);
ExportArrayContent(subArray.ArrayList, writer, writeStringBytes);
writer.WriteEndElement();
}
}
}
}
void ExportIndirectReference(PdfIndirectReference r, XmlWriter writer, string key) {
var i = PdfReader.GetPdfObjectRelease(r);
writer.WriteAttributeString(Constants.Content.ResourceID, Constants.ContentNamespace, r.ToString());
if (AddReferenceRecord(r, key) == false || i == null) {
return;
}
switch (i.Type) {
case PdfObject.DICTIONARY:
var type = (i as PdfDictionary).GetAsName(PdfName.TYPE);
if (type != null) {
var n = PdfHelper.GetPdfNameString(type);
writer.WriteAttributeString(Constants.Content.RefType, Constants.ContentNamespace, n);
switch (n) {
case "Page":
case "Pages":
return;
default:
if (key == "Parent") {
return;
}
break;
}
}
ExportPdfDictionary(writer, i as PdfDictionary);
break;
case PdfObject.ARRAY:
ExportArray((i as PdfArray).ArrayList, writer);
break;
case PdfObject.STREAM:
var s = i as PdfStream;
ExportPdfDictionary(writer, i as PdfDictionary);
if (_imageExporter != null && PdfName.IMAGE.Equals((i as PdfDictionary).GetAsName(PdfName.SUBTYPE))) {
writer.WriteStartElement(Constants.ContentPrefix, "image", Constants.ContentNamespace);
var info = _imageExporter.InfoList.Find(ii => ii.InlineImage.PdfRef == r);
if (info != null) {
writer.WriteAttributeString(Constants.Content.Path, Path.GetFileName(info.FileName));
}
writer.WriteEndElement();
break;
}
ExportStreamContent(writer, s);
break;
default:
writer.WriteAttributeString(PdfHelper.GetTypeName(i.Type), Constants.ContentNamespace, i.ToString());
break;
}
}
void ExportStreamContent(XmlWriter writer, PdfStream s) {
if (s is not PRStream prs || writer is NullXmlWriter) {
return;
}
var key = _exportPath[_exportPath.Count - 1];
writer.WriteStartElement(Constants.ContentPrefix, "stream", Constants.ContentNamespace);
byte[] bs;
bool isRaw = false;
try {
bs = PdfReader.GetStreamBytes(prs);
}
catch (IOException) {
bs = PdfReader.GetStreamBytesRaw(prs);
isRaw = true;
writer.WriteAttributeString(Constants.Content.Raw, Constants.Boolean.True);
}
if (isRaw == false) {
if (key == "Contents" || key == "ToUnicode" || PdfName.XOBJECT.Equals(s.GetAsName(PdfName.TYPE)) && PdfName.FORM.Equals(s.GetAsName(PdfName.SUBTYPE))) {
var sb = StringBuilderCache.Acquire();
byte b;
int l = bs.Length;
int p1 = 0, p2 = 0;
for (int i = 0; i < l; i++) {
b = bs[i];
if (b == 0x0A || b == 0x0D || i + 1 == l) {
p2 = i;
if (i > 2 && bs[i - 2] == 'T' && (bs[i - 1] == 'J' || bs[i - 1] == 'j')) {
// is a text operation
ExportStreamTextContent(bs, sb, p1, p2);
sb.AppendLine();
}
else {
sb.Append(Encoding.ASCII.GetString(bs, p1, p2 - p1));
sb.AppendLine();
}
if (b == 0x0D && i + 1 < l && bs[i + 1] == 0x0A) {
i++;
}
p1 = i + 1;
}
}
if (sb.Length == 0) {
if (l > 2 && bs[l - 2] == 'T' && (bs[l - 1] == 'J' || bs[l - 1] == 'j')) {
ExportStreamTextContent(bs, sb, 0, l);
}
else {
sb.Append(Encoding.Default.GetString(bs, p1, p2));
}
}
writer.WriteCData(StringBuilderCache.GetStringAndRelease(sb));
}
else if (key == "Metadata") {
using (var ms = new MemoryStream(bs))
using (var sr = new StreamReader(ms, true)) {
// strip byte-order-marks
writer.WriteCData(sr.ReadToEnd().Replace("\uFFFE", "").Replace("\uFEFF", ""));
}
}
else {
ExportRawStreamContent(writer, bs);
}
}
else {
//_streamID++;
//string fileName = String.Concat (_options.ExtractImagePath, (_options.ExtractImagePath.EndsWith ("\\") ? String.Empty : "\\"), (_activePage != 0 ? _activePage.ToString () + "-" : String.Empty), _streamID, ".bin");
//using (FileStream f = new FileStream (fileName, FileMode.Create)) {
// f.Write (bs, 0, bs.Length);
//}
//writer.WriteAttributeString ("path", Path.GetFileName (fileName));
ExportRawStreamContent(writer, bs);
}
writer.WriteEndElement();
}
void ExportRawStreamContent(XmlWriter writer, byte[] bs) {
writer.WriteAttributeString(Constants.Content.Length, bs.Length.ToText());
if (_options.ExportBinaryStream == 0) {
writer.WriteBinHex(bs, 0, bs.Length);
}
else {
writer.WriteBinHex(bs, 0, bs.Length < _options.ExportBinaryStream ? bs.Length : _options.ExportBinaryStream);
if (bs.Length > _options.ExportBinaryStream) {
writer.WriteString("....");
}
}
}
static void ExportStreamTextContent(byte[] bs, StringBuilder sb, int p1, int p2) {
bool inText = false;
bool escape = false;
char ch;
int beginTextPosition = 0;
for (int c = p1; c < p2; c++) {
ch = (char)bs[c];
switch (ch) {
case '\\':
if (escape) {
escape = false;
sb.Append(ch);
}
else {
escape = true;
}
continue;
case '(':
if (escape) {
sb.Append(ch);
escape = false;
}
else {
inText = true;
beginTextPosition = c;
sb.Append(ch);
}
continue;
case ')':
if (escape) {
escape = false;
}
else {
inText = false;
//sb.Append (Encoding.BigEndianUnicode.GetString (buffer, beginTextPosition + 1, c - 1 - beginTextPosition));
}
sb.Append(ch);
continue;
default:
if (escape) {
escape = false;
}
if (inText == false) {
sb.Append(ch);
}
else {
sb.Append(((byte)ch).ToString("X2"));
}
break;
}
}
}
internal void ExtractPage(PdfReader reader, XmlWriter writer, params int[] pageNumbers) {
var p = new ExportProcessor(this, writer, _options);
foreach (var pageNum in pageNumbers) {
ExtractPage(pageNum, reader, writer, p);
}
}
void ExtractPage(int pageNum, PdfReader reader, XmlWriter writer, ExportProcessor exportProcessor) {
writer.WriteStartElement(Constants.Content.Page);
_exportPath.Add(Constants.Content.Page);
writer.WriteAttributeString(Constants.Content.PageNumber, pageNum.ToText());
writer.WriteAttributeString(Constants.Content.ResourceID, reader.GetPageOrigRef(pageNum).ToString());
_imageExporter?.ExtractPageImages(reader, pageNum);
if (_options.ExtractPageDictionary) {
ExportPdfDictionary(writer, reader.GetPageNRelease(pageNum));
}
if (_options.ExportContentOperators || _options.ExportDecodedText) {
if (_options.ExportContentOperators) {
writer.WriteStartElement(Constants.Content.Operators);
}
exportProcessor.Reset();
exportProcessor.ProcessContent(reader.GetPageContent(pageNum), reader.GetPageNRelease(pageNum).GetAsDict(PdfName.RESOURCES));
if (_options.ExportContentOperators) {
exportProcessor.End();
writer.WriteEndElement();
}
}
//if (_imageExporter != null && _imageExporter.InfoList.Count > 0) {
// writer.WriteStartElement ("images", Constants.ContentNamespace);
// foreach (var item in _imageExporter.InfoList) {
// writer.WriteStartElement ("image", Constants.ContentNamespace);
// writer.WriteAttributeString (Constants.Content.ResourceID, Constants.ContentNamespace, item.PdfRef.ToString ());
// writer.WriteAttributeString ("位置", String.Concat (item.X, ", ", item.Y, ", ", item.Z));
// writer.WriteEndElement ();
// }
// writer.WriteEndElement ();
//}
writer.WriteEndElement();
_exportPath.RemoveAt(_exportPath.Count - 1);
}
sealed class ExportProcessor : PdfContentStreamProcessor
{
readonly PdfContentExport _export;
readonly XmlWriter _writer;
readonly bool _writeOperators;
readonly List<TextInfo> _textContainer;
readonly List<NameValuePair> _operands = new List<NameValuePair>();
int _writerLevel;
float _textWidth;
public ExportProcessor(PdfContentExport export, XmlWriter writer, ExporterOptions options) {
_export = export;
_writer = writer;
_writeOperators = options.ExportContentOperators;
_textContainer = options.ExportDecodedText ? new List<TextInfo>() : null;
PopulateOperators();
RegisterContentOperator("TJ", new AccumulatedShowTextArray());
}
public void End() {
if (_writeOperators == false) {
return;
}
while (_writerLevel > 0) {
_writer.WriteEndElement();
_writerLevel--;
}
}
internal List<TextInfo> TextInfoList => _textContainer;
protected override void DisplayPdfString(PdfString str) {
var gs = CurrentGraphicState;
var font = gs.Font;
float totalWidth = 0;
foreach (var c in font.DecodeText(str)) {
float w = font.GetWidth(c) / 1000.0f;
float wordSpacing = c == ' ' ? gs.WordSpacing : 0f;
totalWidth += (w * gs.FontSize + gs.CharacterSpacing + wordSpacing) * gs.HorizontalScaling;
}
_textWidth = totalWidth;
}
protected override void InvokeOperator(PdfLiteral oper, List<PdfObject> operands) {
base.InvokeOperator(oper, operands);
string o, fn, t = null;
bool open = false;
bool hasDescriptiveOperands = false;
o = oper.ToString();
PdfPageCommand.GetFriendlyCommandName(o, out fn);
_operands.Clear();
switch (o) {
case "BDC":
case "BMC":
case "BT":
case "BX":
case "q":
open = true;
_writerLevel++;
goto default;
case "EMC":
case "ET":
case "EX":
case "Q":
if (_writerLevel > 0 && _writeOperators) {
_writer.WriteEndElement();
_writerLevel--;
}
return;
case "TJ":
var array = (PdfArray)operands[0];
using (var ms = new MemoryStream(array.Length)) {
foreach (PdfObject item in array.ArrayList) {
if (item.Type == PdfObject.STRING) {
ms.Write((item as PdfString).GetBytes(), 0, item.Length);
}
}
t = CurrentGraphicState.Font.DecodeText(new PdfString(ms.ToArray()));
}
AddTextInfo(t);
_operands.Add(new NameValuePair(Constants.Content.OperandNames.Text, t));
goto default;
case "Tj":
case "'":
case "\"":
t = CurrentGraphicState.Font.DecodeText(operands[0] as PdfString);
AddTextInfo(t);
_operands.Add(new NameValuePair(Constants.Content.OperandNames.Text, t));
goto default;
case "Tf":
_operands.Add(new NameValuePair(Constants.Font.ThisName, CurrentGraphicState.Font.FontName));
_operands.Add(new NameValuePair(Constants.Content.OperandNames.ResourceName, operands[0].ToString()));
_operands.Add(new NameValuePair(Constants.Content.OperandNames.Size, operands[1].ToString()));
hasDescriptiveOperands = true;
goto default;
case "cm":
case "Tm":
_operands.Add(new NameValuePair(Constants.Content.Operands, GetOperandsTextValue(operands)));
hasDescriptiveOperands = true;
goto default;
case "BI":
//case "EI":
goto default;
default:
if (_writeOperators == false) {
return;
}
_writer.WriteStartElement(fn ?? "未知操作符");
_writer.WriteAttributeString("name", o);
foreach (var item in _operands) {
_writer.WriteAttributeString(item.Key, PdfHelper.GetValidXmlString(item.Value));
}
break;
}
if (hasDescriptiveOperands == false) {
if (operands.Count > 0) {
// 删除操作符
operands.RemoveAt(operands.Count - 1);
}
if (operands.Count > 0) {
_writer.WriteStartElement(Constants.Content.Operands);
_export.ExportArrayContent(operands, _writer, true);
_writer.WriteEndElement();
if (o == "BI") {
_writer.WriteStartElement(Constants.Content.Raw);
_export.ExportRawStreamContent(_writer, (operands[0] as PdfImageData).RawBytes);
_writer.WriteEndElement();
}
}
}
if (open == false && _writerLevel >= 0) {
_writer.WriteEndElement();
}
}
static string GetOperandsTextValue(List<PdfObject> operands) {
operands.RemoveAt(operands.Count - 1);
return String.Join(" ", operands.ConvertAll((po) => po.Type == PdfObject.NUMBER ? ((PdfNumber)po).DoubleValue.ToText() : null).ToArray());
}
void AddTextInfo(string t) {
if (_textContainer != null && t != null) {
_textContainer.Add(new TextInfo() {
Text = t,
Size = CurrentGraphicState.FontSize * TextMatrix[Matrix.I11],
Region = new Bound(TextMatrix[Matrix.I31], TextMatrix[Matrix.I32], TextMatrix[Matrix.I31] + _textWidth, 0),
// note 不要设置该属性,否则可能在文档具有大量字体时占用过高的内存
//Font = CurrentGraphicState.Font
});
}
}
}
}
}