Skip to content

PDF created with OwnerPassword cannot be read: SharpZipBaseException: Header checksum illegalΒ #470

@Myster

Description

@Myster

Hi,
I have a .NET website that will watermark the current username onto any PDF they download.
If I run the same PDF through twice it fails to read the PDF that the same code created.
Here is what I believe is the relevant code from the middleware which does this:

	pdfDoc = PdfReader.Open(fileStream, "__redacted__",PdfDocumentOpenMode.Modify);
	if (pdfDoc.Version < 14) // version 14 is the version at which transparent text for the watermark is supported.
		pdfDoc.Version = 14;
	
	pdfDoc.SecuritySettings.OwnerPassword = "__redacted__";
	pdfDoc.SecuritySettings.PermitAccessibilityExtractContent = true;
	pdfDoc.SecuritySettings.PermitFullQualityPrint = false;
	pdfDoc.SecuritySettings.PermitPrint = true;
	
	foreach (var page in pdfDoc.Pages)
	{
		double width = page.Width;
		double height = page.Height;
	
		// Create graphics object in append mode to overlay the watermark
		using var gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Append); //πŸ›‘ exception here

		// ...snip... apply watermark...
	}

	using var outputStream = new MemoryStream();
	pdfDoc.Save(outputStream, false);
	outputStream.Position = 0;
	context.Response.ContentType = "application/pdf";
	await outputStream.CopyToAsync(context.Response.Body);

If I try and upload the file created/downloaded this way I get the following exception:

SharpZipBaseException: Header checksum illegal
ICSharpCode.SharpZipLib.Zip.Compression.Inflater.DecodeHeader()
ICSharpCode.SharpZipLib.Zip.Compression.Inflater.Decode()
ICSharpCode.SharpZipLib.Zip.Compression.Inflater.Inflate(byte[] buffer, int offset, int count)
ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream.Read(byte[] buffer, int offset, int count)
PdfSharpCore.Pdf.Filters.FlateDecode.Decode(byte[] data, FilterParms parms)
PdfSharpCore.Pdf.Filters.Filter.Decode(byte[] data, PdfDictionary decodeParms)
PdfSharpCore.Pdf.Filters.Filtering.Decode(byte[] data, PdfItem filterItem, PdfItem decodeParms)
PdfSharpCore.Pdf.Advanced.PdfContent.Decode()
PdfSharpCore.Pdf.Advanced.PdfContent..ctor(PdfDictionary dict)
PdfSharpCore.Pdf.Advanced.PdfContents..ctor(PdfArray array)
PdfSharpCore.Pdf.PdfPage.get_Contents()
PdfSharpCore.Drawing.XGraphics..ctor(PdfPage page, XGraphicsPdfPageOptions options, XGraphicsUnit pageUnit, XPageDirection pageDirection)
PdfSharpCore.Drawing.XGraphics.FromPdfPage(PdfPage page, XGraphicsPdfPageOptions options)
DXP.Middleware.PdfWatermarkMiddleware.InvokeAsync(...

I'm sure I'm missing something, but not sure what :-)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions