List of usage examples for com.itextpdf.text.pdf PdfName CENTERWINDOW
PdfName CENTERWINDOW
To view the source code for com.itextpdf.text.pdf PdfName CENTERWINDOW.
Click Source Link
From source file:org.h819.commons.file.MyPDFUtils.java
/** * pdf //from w w w . j a v a 2 s . c o m * * @param srcPdfFile ? * @param descPdfFile * @param userPassword ?? ,null ? * @param ownerPassword ?? ,null ? * @param allowHideMenuBar ???? * @param allowHideToolbar ?? * @param allowPrinting ??? * @param allowCopy ??? * @throws java.io.IOException * @throws DocumentException */ public static void encryptPdf(File srcPdfFile, File descPdfFile, String userPassword, String ownerPassword, boolean allowHideMenuBar, boolean allowHideToolbar, boolean allowPrinting, boolean allowCopy) throws IOException { if (srcPdfFile == null || !srcPdfFile.exists()) throw new IOException("src pdf file '" + srcPdfFile + "' does not exist."); byte[] userByte = null; byte[] ownerByte = null; /** User password. */ if (userPassword != null) userByte = userPassword.getBytes(); /** Owner password. */ if (ownerPassword != null) ownerByte = ownerPassword.getBytes(); try { PdfReader reader = getPdfReader(srcPdfFile); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(descPdfFile.getAbsoluteFile())); // // ???? PdfWriter.setViewerPreferences addViewerPreference // ? api ??? addViewerPreference ? // ???"|"()?, ? PdfWriter.HideWindowUI // ???, ?? // ( addViewerPreference ) stamper.getWriter().addViewerPreference(PdfName.CENTERWINDOW, PdfBoolean.PDFTRUE); stamper.getWriter().addViewerPreference(PdfName.DISPLAYDOCTITLE, PdfBoolean.PDFTRUE); stamper.getWriter().addViewerPreference(PdfName.FITWINDOW, PdfBoolean.PDFTRUE); // writer.addViewerPreference(PdfName.HIDEWINDOWUI, // PdfBoolean.PDFTRUE); if (!allowHideToolbar) stamper.getWriter().addViewerPreference(PdfName.HIDETOOLBAR, PdfBoolean.PDFTRUE); if (!allowHideMenuBar) stamper.getWriter().addViewerPreference(PdfName.HIDEMENUBAR, PdfBoolean.PDFTRUE); // allow if (allowPrinting && allowCopy) stamper.setEncryption(userByte, ownerByte, PdfWriter.ALLOW_PRINTING | PdfWriter.ALLOW_COPY, true); else if (allowPrinting) stamper.setEncryption(userByte, ownerByte, PdfWriter.ALLOW_PRINTING, true); else if (allowCopy) stamper.setEncryption(userByte, ownerByte, PdfWriter.ALLOW_COPY, true); else stamper.setEncryption(userByte, ownerByte, 0, true); stamper.close(); reader.close(); logger.info(srcPdfFile.getAbsoluteFile() + " encrypt finished."); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:org.h819.commons.file.MyPDFUtilss.java
/** * pdf //from w ww . ja v a2 s .c o m * * @param srcPdfFile ? * @param descPdfFile * @param userPassword ?? ,null ? * @param ownerPassword ?? ,null ? * @param allow_HideMenubar ???? * @param allow_HideToolbar ?? * @param allow_printing ??? * @param allow_copy ??? * @throws java.io.IOException * @throws DocumentException */ public static void encryptPdf(File srcPdfFile, File descPdfFile, String userPassword, String ownerPassword, boolean allow_HideMenubar, boolean allow_HideToolbar, boolean allow_printing, boolean allow_copy) throws IOException { if (srcPdfFile == null || !srcPdfFile.exists()) throw new IOException("src pdf file '" + srcPdfFile + "' does not exsit."); byte[] USER = null; byte[] OWNER = null; /** User password. */ if (userPassword != null) USER = userPassword.getBytes(); /** Owner password. */ if (ownerPassword != null) OWNER = ownerPassword.getBytes(); try { PdfReader reader = getPdfReader(srcPdfFile); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(descPdfFile.getAbsoluteFile())); // // ???? PdfWriter.setViewerPreferences addViewerPreference // ? api ??? addViewerPreference ? // ???"|"()?, ? PdfWriter.HideWindowUI // ???, ?? // ( addViewerPreference ) stamper.getWriter().addViewerPreference(PdfName.CENTERWINDOW, PdfBoolean.PDFTRUE); stamper.getWriter().addViewerPreference(PdfName.DISPLAYDOCTITLE, PdfBoolean.PDFTRUE); stamper.getWriter().addViewerPreference(PdfName.FITWINDOW, PdfBoolean.PDFTRUE); // writer.addViewerPreference(PdfName.HIDEWINDOWUI, // PdfBoolean.PDFTRUE); if (!allow_HideToolbar) stamper.getWriter().addViewerPreference(PdfName.HIDETOOLBAR, PdfBoolean.PDFTRUE); if (!allow_HideMenubar) stamper.getWriter().addViewerPreference(PdfName.HIDEMENUBAR, PdfBoolean.PDFTRUE); // allow if (allow_printing && allow_copy) stamper.setEncryption(USER, OWNER, PdfWriter.ALLOW_PRINTING | PdfWriter.ALLOW_COPY, true); else if (allow_printing) stamper.setEncryption(USER, OWNER, PdfWriter.ALLOW_PRINTING, true); else if (allow_copy) stamper.setEncryption(USER, OWNER, PdfWriter.ALLOW_COPY, true); else stamper.setEncryption(USER, OWNER, 0, true); stamper.close(); logger.info(srcPdfFile.getAbsoluteFile() + " encrypt finished."); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }