简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM