Craig
Craig

Reputation: 36846

iTextSharp modify pdf properties

I have a PDF I am trying to open up and alter slightly (just changing the ViewerPreferences) but can't seem to work out the exact usage of iTextSharp. The file that gets saved at the end is corrupt. Any ideas?

        PdfReader reader = new PdfReader(@"C:\4803.pdf");

        using (var stream = new MemoryStream())
        {
            PdfStamper stamper = new PdfStamper(reader, stream);
            stamper.ViewerPreferences = PdfWriter.AllowPrinting | PdfWriter.PrintScalingNone;

            stream.Position = 0;
            byte[] output = LoadFromStream(stream); // Convert it to a byte array
            SaveToFile(output, @"C:\4803_out.pdf"); // Save it to a file

            stamper.Close();
        }

Upvotes: 1

Views: 1955

Answers (1)

mkl
mkl

Reputation: 96029

Close the PdfStamper before converting the MemoryStream to a byte array and saving it. The way you do it, the pdf is not yet completed in the stream.

PS: To prevent the closing of the stamper from also closing the stream, use

stamper.Writer.CloseStream = false

Upvotes: 3

Related Questions