Created
November 15, 2013 14:10
-
-
Save frankgeerlings/7484879 to your computer and use it in GitHub Desktop.
Fix for iTextSharp created PDFs.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ExtendedApprovals | |
{ | |
/// <summary> | |
/// This compares a PDF document saved in a byte stream. | |
/// </summary> | |
/// <param name="bytes">A byte array that represents the contents of the PDF file to be verified</param> | |
public static void VerifyPdf(byte[] bytes) | |
{ | |
var actual = ScrubCreationDateInPdf(bytes); | |
var writer = new ApprovalTests.Writers.BinaryWriter(actual, "pdf"); | |
var namer = Approvals.GetDefaultNamer(); | |
var basename = String.Format(@"{0}\{1}", namer.SourcePath, namer.Name); | |
var received = Path.GetFullPath(writer.GetReceivedFilename(basename)); | |
received = writer.WriteReceivedFile(received); | |
Approvals.Verify(new ExistingFileWriter(received)); | |
} | |
private static byte[] ScrubCreationDateInPdf(byte[] actual) | |
{ | |
var stream = new MemoryStream(actual, 0, actual.Length, true); | |
ScrubInPlace(stream, "/CreationDate(", "/CreationDate(D:20110426104115-07'00')"); | |
ScrubInPlace(stream, "/ModDate(", "/ModDate(D:20110426104115-07'00')"); | |
ScrubInPlace(stream, "/ID [<", "/ID [<02f36b9088ef0614b00d885a1b5037cf><38ccb29766064dab2071f1ac6a9056fc>]>>"); | |
stream.Flush(); | |
return stream.ToArray(); | |
} | |
private static void ScrubInPlace(MemoryStream stream, string start, string replacement) | |
{ | |
var offset = PdfScrubber.Find(start, stream); | |
if (0L > offset) | |
return; | |
stream.Seek(offset, SeekOrigin.Begin); | |
var bytes = new ASCIIEncoding().GetBytes(replacement); | |
stream.Write(bytes, 0, bytes.Length); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment