1

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();
        }
4

1 回答 1

3

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
于 2012-10-26T04:39:08.240 回答