【问题标题】:Outputstream - create non editable pdf fileOutputstream - 创建不可编辑的 pdf 文件
【发布时间】:2013-08-23 10:09:15
【问题描述】:

以下代码用于编写pdf文件;

BaseFont bf = BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
FontSelector fs = new FontSelector();
fs.addFont(new Font(bf));                
String fileName = filePath3 + "//DEVIATION_REPORT.pdf";
OutputStream file = new FileOutputStream(new File(fileName));
Font smallFont = new Font(Font.FontFamily.COURIER, 6, Font.NORMAL);
Font headerFont = new Font(Font.FontFamily.HELVETICA, 7, Font.BOLD);
Font tabFont = new Font(Font.FontFamily.HELVETICA, 5, Font.NORMAL);
Font rusFont = new Font(bf, 5);                
Font blueFont = new Font(bf, 5);
blueFont.setColor(BaseColor.BLUE);                
Font redFont = new Font(bf, 5);
redFont.setColor(BaseColor.RED);

String comType;
if (compType == 2)
   comType = "Character";
else 
   comType = "Word";

Document doc = new Document();
PdfWriter.getInstance(doc, file);                
doc.open();            

Image image1 = Image.getInstance(cmdpath + "ScRp.jpg");
image1.setAlignment(Element.ALIGN_LEFT);

Paragraph prg = new Paragraph("Compare", smallFont);
prg.setAlignment(Element.ALIGN_RIGHT);
doc.add(image1);
doc.add(prg);
doc.add(new Paragraph("__________________________________________________"));
doc.add(new Paragraph("Passed Report"));
doc.add(new Paragraph(" "));

PdfPTable table = new PdfPTable(2);
PdfPCell cell1 = new PdfPCell(new Paragraph("No", headerFont));
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
cell1.setPaddingBottom(5);                
PdfPCell cell2 = new PdfPCell(new Paragraph("Details", headerFont));
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
cell2.setPaddingBottom(5);             
table.addCell(cell1);
table.addCell(cell2);
doc.add(table);
doc.newPage();
doc.close();
file.close();

如果我想让 pdf 文件不可编辑,我该怎么做。我试过用

 PdfEncryptor.encrypt(  
 new PdfReader(fileName),  
 new FileOutputStream("HelloWorldProtected.pdf"),  
 null,  
 "StrongPassword".getBytes(),  
 PdfWriter.AllowPrinting,  
 PdfWriter.STRENGTH128BITS);  

但是编译器在这里停止。我等了超过 15 分钟,但什么也没发生。

我什至制作了 file.setreadOnly(),但它并没有用户在编辑后保存它。它仍然允许用户编辑。用户可以另存为文档并替换为不可编辑的文档。

有没有其他方法可以使文件不可编辑。请指教。

【问题讨论】:

    标签: java pdf outputstream fileoutputstream readonly


    【解决方案1】:

    如果您的项目很好,您可能希望使用iText 进行此类工作。我现在看不到任何其他选择。我个人使用过 iText,它非常强大。让我知道这是否有帮助。

    编辑:

    1. Encryption settings
    2. http://viralpatel.net/blogs/password-protect-pdf-itext-java/
    3. http://www.jarfinder.com/index.php/java/info/com.lowagie.text.pdf.interfaces.PdfEncryptionSettings
    4. 尝试在 google 和 SO 上搜索更多内容

    编辑: 15 分钟的漫长等待。问题可能是文件路径上的文件写入权限。

    【讨论】:

    • 感谢您在只读部分分享您的工作代码。
    • 正如我的问题所提到的......当我这样做时,PdfWriter rptWritter = PdfWriter.getInstance(doc, file); rptWritter.setEncryption(USER_PASS.getBytes(), OWNER_PASS.getBytes(), PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128); 调试器在这里停了很长时间。即使我等了 15 分钟也没有回应。
    • 15 分钟的漫长等待。问题可能是文件路径上的文件写入权限。
    • 抱歉忘记更新了..漫长的等待是因为 jar 文件。一旦我添加了所需的 jar 文件。它工作正常。有一个问题,用户仍然可以保存为 pdf。知道如何阻止这种情况吗?
    • 你能不能更清楚“用户仍然可以保存为 pdf”我做了。nt让你明白。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多