Skip to content

Commit 9c9fa54

Browse files
committed
add flags
1 parent 8dc9190 commit 9c9fa54

File tree

5 files changed

+93
-21
lines changed

5 files changed

+93
-21
lines changed

src/foundation/src/PDFsharp/src/PdfSharp/Pdf.IO/PdfReader.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,12 +278,17 @@ PdfDocument OpenFromStream(Stream stream, string? password, PdfDocumentOpenMode
278278

279279
var lexer = new Lexer(stream, _logger);
280280
_document = new PdfDocument(lexer);
281-
_document.Options.EnableReferenceCompaction = _options.EnableReferenceCompaction;
281+
282282
_document.Options.EnableReferenceRenumbering = _options.EnableReferenceRenumbering;
283+
_document.Options.EnableReferenceCompaction = _options.EnableReferenceCompaction;
283284
_document.Options.EnableImplicitTransparencyGroup = _options.EnableImplicitTransparencyGroup;
284285
_document.Options.EnableImplicitMetadata = _options.EnableImplicitMetadata;
285286
_document.Options.EnableWriterCommentInTrailer = _options.EnableWriterCommentInTrailer;
286287
_document.Options.EnableLfLineEndings = _options.EnableLfLineEndings;
288+
_document.Options.EnableOwnBinaryHeader = _options.EnableOwnBinaryHeader;
289+
_document.Options.EnableLineBreakInArrayObjects = _options.EnableLineBreakInArrayObjects;
290+
_document.Options.DisablePagesAndCatalogAtEnd = _options.DisablePagesAndCatalogAtEnd;
291+
287292
_document._state |= DocumentState.Imported;
288293
_document._openMode = openMode;
289294

src/foundation/src/PDFsharp/src/PdfSharp/Pdf.IO/PdfReaderOptions.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,18 +99,48 @@ public class PdfReaderOptions
9999

100100
public ReaderProblemDelegate? ReaderProblemCallback { get; set; }
101101

102+
/// <summary>
103+
/// Set to <see langword="false"/> to enable greater compatibility
104+
/// </summary>
102105
public bool EnableReferenceRenumbering { get; set; } = true;
103106

107+
/// <summary>
108+
/// Set to <see langword="false"/> to enable greater compatibility
109+
/// </summary>
104110
public bool EnableReferenceCompaction { get; set; } = true;
105111

112+
/// <summary>
113+
/// Set to <see langword="false"/> to enable greater compatibility
114+
/// </summary>
106115
public bool EnableImplicitTransparencyGroup { get; set; } = true;
107116

117+
/// <summary>
118+
/// Set to <see langword="false"/> to enable greater compatibility
119+
/// </summary>
108120
public bool EnableImplicitMetadata { get; set; } = true;
109121

122+
/// <summary>
123+
/// Set to <see langword="false"/> to enable greater compatibility
124+
/// </summary>
110125
public bool EnableWriterCommentInTrailer { get; set; } = true;
111126

127+
/// <summary>
128+
/// Set to <see langword="false"/> to enable greater compatibility
129+
/// </summary>
112130
public bool EnableLfLineEndings { get; set; } = true;
113131

132+
/// <summary>
133+
/// Set to <see langword="false"/> to enable greater compatibility
134+
/// </summary>
135+
public bool EnableOwnBinaryHeader { get; set; } = true;
136+
137+
/// <summary>
138+
/// Set to <see langword="false"/> to enable greater compatibility
139+
/// </summary>
140+
public bool EnableLineBreakInArrayObjects { get; set; } = true;
141+
142+
public bool DisablePagesAndCatalogAtEnd { get; set; } = true;
143+
114144
// Testing only
115145

116146
//public bool UseOldCode { get; set; } = false;

src/foundation/src/PDFsharp/src/PdfSharp/Pdf.IO/PdfWriter.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,8 @@ public void WriteBeginObject(PdfObject obj)
357357
if (obj is PdfArray)
358358
{
359359
WriteRaw("[");
360-
//WriteRaw(_document.Options.LineEnding);
360+
if(_document.Options.EnableLineBreakInArrayObjects)
361+
WriteRaw(_document.Options.LineEnding);
361362
}
362363
else if (obj is PdfDictionary)
363364
{
@@ -408,7 +409,8 @@ public void WriteEndObject()
408409
{
409410
if (indirect)
410411
{
411-
//WriteRaw(_document.Options.LineEnding);
412+
if (_document.Options.EnableLineBreakInArrayObjects)
413+
WriteRaw(_document.Options.LineEnding);
412414
WriteRaw("]");
413415
WriteRaw(_document.Options.LineEnding);
414416
_lastCat = CharCat.NewLine;
@@ -559,8 +561,10 @@ public void WriteFileHeader(PdfDocument document)
559561
int version = document._version;
560562
WriteRaw(Invariant($"%PDF-{version / 10}.{version % 10}"));
561563
WriteRaw(document.Options.LineEnding);
562-
//WriteRaw("%\xD3\xF4\xCC\xE1");
563-
WriteRaw("%\xE2\xE3\xCF\xD3");
564+
if(document.Options.EnableOwnBinaryHeader)
565+
WriteRaw("%\xD3\xF4\xCC\xE1");
566+
else
567+
WriteRaw("%\xE2\xE3\xCF\xD3");
564568
WriteRaw(document.Options.LineEnding);
565569

566570
if (Layout == PdfWriterLayout.Verbose)

src/foundation/src/PDFsharp/src/PdfSharp/Pdf/PdfDocument.cs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -360,23 +360,26 @@ async Task DoSaveAsync(PdfWriter writer)
360360

361361
writer.WriteFileHeader(this);
362362
var irefs = IrefTable.AllReferences.ToList();
363-
irefs.Sort((a, b) =>
363+
if (!Options.DisablePagesAndCatalogAtEnd)
364364
{
365-
int getPrio(PdfReference r) => r.Value switch
365+
irefs.Sort((a, b) =>
366366
{
367-
PdfPages => 1,
368-
PdfCatalog => 2,
369-
_ => 0,
370-
};
371-
var cmp = getPrio(a).CompareTo(getPrio(b));
372-
if (cmp != 0)
367+
int getPrio(PdfReference r) => r.Value switch
368+
{
369+
PdfPages => 1,
370+
PdfCatalog => 2,
371+
_ => 0,
372+
};
373+
var cmp = getPrio(a).CompareTo(getPrio(b));
374+
if (cmp != 0)
375+
return cmp;
376+
cmp = a.GenerationNumber.CompareTo(b.GenerationNumber);
377+
if (cmp != 0)
378+
return cmp;
379+
cmp = a.ObjectNumber.CompareTo(b.ObjectNumber);
373380
return cmp;
374-
cmp = a.GenerationNumber.CompareTo(b.GenerationNumber);
375-
if (cmp != 0)
376-
return cmp;
377-
cmp = a.ObjectNumber.CompareTo(b.ObjectNumber);
378-
return cmp;
379-
});
381+
});
382+
}
380383
int count = irefs.Count;
381384
foreach(var iref in irefs)
382385
{

src/foundation/src/PDFsharp/src/PdfSharp/Pdf/PdfDocumentOptions.cs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,48 @@ public PdfUseFlateDecoderForJpegImages UseFlateDecoderForJpegImages
8484
}
8585
PdfUseFlateDecoderForJpegImages _useFlateDecoderForJpegImages = PdfUseFlateDecoderForJpegImages.Never;
8686

87-
public bool EnableReferenceCompaction { get; set; } = true;
88-
87+
/// <summary>
88+
/// Set to <see langword="false"/> to enable greater compatibility
89+
/// </summary>
8990
public bool EnableReferenceRenumbering { get; set; } = true;
9091

92+
/// <summary>
93+
/// Set to <see langword="false"/> to enable greater compatibility
94+
/// </summary>
95+
public bool EnableReferenceCompaction { get; set; } = true;
96+
97+
/// <summary>
98+
/// Set to <see langword="false"/> to enable greater compatibility
99+
/// </summary>
91100
public bool EnableImplicitTransparencyGroup { get; set; } = true;
92101

102+
/// <summary>
103+
/// Set to <see langword="false"/> to enable greater compatibility
104+
/// </summary>
93105
public bool EnableImplicitMetadata { get; set; } = true;
94106

107+
/// <summary>
108+
/// Set to <see langword="false"/> to enable greater compatibility
109+
/// </summary>
95110
public bool EnableWriterCommentInTrailer { get; set; } = true;
96111

112+
/// <summary>
113+
/// Set to <see langword="false"/> to enable greater compatibility
114+
/// </summary>
97115
public bool EnableLfLineEndings { get; set; } = true;
98116

117+
/// <summary>
118+
/// Set to <see langword="false"/> to enable greater compatibility
119+
/// </summary>
120+
public bool EnableOwnBinaryHeader { get; set; } = true;
121+
122+
/// <summary>
123+
/// Set to <see langword="false"/> to enable greater compatibility
124+
/// </summary>
125+
public bool EnableLineBreakInArrayObjects { get; set; } = true;
126+
127+
public bool DisablePagesAndCatalogAtEnd { get; set; } = true;
128+
99129
public string LineEnding => EnableLfLineEndings ? "\n" : "\r\n";
100130

101131
public byte[] LineEndingBytes => Encoding.ASCII.GetBytes(LineEnding);

0 commit comments

Comments
 (0)