问题
I Have Installed iText7 trail Version Packages to convert html file into pdf. I have succefully converted html to pdf using proper code like they(iText Software) mentioned.But still i am not clear to set the Password for created pdf. After converting html file to pdf,the pdf file should be protected with password.so please anyone help me what is the code in c# to encrypt the pdf file while converting from html.
回答1:
You didn't share any code (which is actually a requirement when you post a question on Stack Overflow), but I assume that you are creating a PdfWriter
somewhere in the process. If not, check out the different variations to create a PDF from HTML. Internally, the PDF writing process is done by a PdfWriter
instance, so if you don't have a PdfWriter
instance in your code, you'll have to use a method that reaches somewhat deeper into the lower-level functionality.
When you create the PdfWriter
instance, you can define WriterProperties
. This is explained in Chapter 7 of the Building Blocks tutorial. You have to create a PdfWriter
instance that accepts a destination (the path to the PDF that you are creating), but also a WriterProperties
instance:
byte[] user = "abc".getBytes();
byte[] owner = "xyz".getBytes();
PdfDocument pdf = new PdfDocument(new PdfWriter(dest,
new WriterProperties().setStandardEncryption(user, owner,
EncryptionConstants.ALLOW_PRINTING | EncryptionConstants.ALLOW_ASSEMBLY,
EncryptionConstants.ENCRYPTION_AES_256)));
In this case, we use AES 256 encryption (the only encryption algorithm that will be allowed in ISO-32000-2) using a user and an owner password. We allow printing and assembly of the document.
来源:https://stackoverflow.com/questions/44512926/need-itext7-html-to-pdf-encryption-code-in-c-sharp