public static void encrypt(PDFDocument doc, OutputStream os, String newUserPassword, String newOwnerPassword, int permissions, int encryption) throws ApplicationException, DocumentException, IOException { byte[] user = newUserPassword==null?null:newUserPassword.getBytes(); byte[] owner = newOwnerPassword==null?null:newOwnerPassword.getBytes(); PdfReader pr = doc.getPdfReader(); List bookmarks = SimpleBookmark.getBookmark(pr); int n = pr.getNumberOfPages(); Document document = new Document(pr.getPageSizeWithRotation(1)); PdfCopy writer = new PdfCopy(document, os); if(encryption!=ENCRYPT_NONE)writer.setEncryption(user, owner, permissions, encryption); document.open(); PdfImportedPage page; for (int i = 1; i <= n; i++) { page = writer.getImportedPage(pr, i); writer.addPage(page); } PRAcroForm form = pr.getAcroForm(); if (form != null)writer.copyAcroForm(pr); if (bookmarks!=null)writer.setOutlines(bookmarks); document.close(); }
private int addArchivePartToPDF(final PdfCopy copy, final ArchivePart archivePart) { try { return addArchivePartToPDF0(copy, archivePart); } catch (final Exception e) { throw new PrintingQueueAggregationException(archivePart.getPrintJobLine().getC_Printing_Queue_ID(), e); } }
public void print(final OutputStream bos) throws Exception { final I_C_Print_Job_Instructions print_Job_Instructions = printPackage.getC_Print_Job_Instructions(); final Document document = new Document(); final PdfCopy copy = new PdfCopy(document, bos); document.open(); final PrintablePDF printable = createPrintable(); if (printable == null) { return; } try { for (final I_C_Print_PackageInfo printPackageInfo : printingDAO.retrievePrintPackageInfos(printPackage)) { final byte[] pdf = print(printPackageInfo, printable); final PdfReader reader = new PdfReader(pdf); for (int page = 0; page < reader.getNumberOfPages();) { copy.addPage(copy.getImportedPage(reader, ++page)); } copy.freeReader(reader); reader.close(); } document.close(); print_Job_Instructions.setErrorMsg(null); print_Job_Instructions.setStatus(X_C_Print_Job_Instructions.STATUS_Done); InterfaceWrapperHelper.save(print_Job_Instructions); } catch (Exception e) { print_Job_Instructions.setErrorMsg(e.getLocalizedMessage()); print_Job_Instructions.setStatus(X_C_Print_Job_Instructions.STATUS_Error); InterfaceWrapperHelper.save(print_Job_Instructions); throw new AdempiereException(e.getLocalizedMessage()); } }
/** * Generate pdf for sending response using itext * * @param reportFileFullName * @return * @throws IOException * @throws DocumentException * @throws BadPdfFormatException */ protected ByteArrayOutputStream generatePdfOutStream(String reportFileFullName) throws IOException, DocumentException, BadPdfFormatException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); // we create a reader for a certain document PdfReader reader = new PdfReader(reportFileFullName); reader.consolidateNamedDestinations(); // step 1: creation of a document-object Document document = new Document(reader.getPageSizeWithRotation(1)); // step 2: we create a writer that listens to the document PdfCopy writer = new PdfCopy(document, baos); // step 3: we open the document document.open(); // we retrieve the total number of pages int n = reader.getNumberOfPages(); // step 4: we add content PdfImportedPage page; for (int i = 0; i < n;) { ++i; page = writer.getImportedPage(reader, i); writer.addPage(page); } writer.freeReader(reader); // step 5: we close the document document.close(); return baos; }
/** * 添加水印、页眉、页脚 * @param fileName 源文件路径 * @param savepath 目标文件路径 * @param waterMarkName 文字水印 * @param pageHeade 页眉 * @param foot 页脚 * @return */ public static int addFooterAndWater(String fileName, String savepath, String waterMarkName, String pageHeade, String foot) { // 文档总页数 int num = 0; Document document = new Document(); try { PdfReader reader = new PdfReader(fileName); //BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED); BaseFont base = BaseFont.createFont(BaseFont.COURIER, "utf-8", BaseFont.EMBEDDED); num = reader.getNumberOfPages(); PdfCopy copy = new PdfCopy(document, new FileOutputStream(savepath)); document.open(); for (int i = 0; i < num;) { PdfImportedPage page = copy.getImportedPage(reader, ++i); PageStamp stamp = copy.createPageStamp(page); Font f = new Font(base); // 添加页脚,左侧文字,右侧页码 ColumnText.showTextAligned(stamp.getUnderContent(), Element.ALIGN_RIGHT, new Phrase(String.format("第 %d 页/共 %d 页", i, num), f), 550f, 28, 0); ColumnText.showTextAligned(stamp.getUnderContent(), Element.ALIGN_LEFT, new Phrase(foot, f), 50f, 28, 0); // 添加页眉 (文字页眉,居中) ColumnText.showTextAligned(stamp.getUnderContent(), Element.ALIGN_CENTER, new Phrase(pageHeade, f), 150f, 800, 0); // 页眉添加logo (图片页眉,居右) Image img = Image.getInstance("F:\\Tools\\pdf2swf工具\\resource/watermark.png");//"template/logo.png");// 选择图片 img.setAlignment(1); img.scaleAbsolute(436 / 5, 96 / 5);// 控制图片大小 img.setAbsolutePosition(450f, 800);// 控制图片位置 stamp.getUnderContent().addImage(img); // 添加水印 PdfContentByte under = stamp.getUnderContent(); under.beginText(); under.setColorFill(Color.LIGHT_GRAY); // 字符越长,字体越小,设置字体 int fontSize = getFontSize(waterMarkName); under.setFontAndSize(base, fontSize); // 设置水印文字字体倾斜 开始 float pageWidth = reader.getPageSize(i).getWidth(); float pageHeight = reader.getPageSize(i).getHeight(); // 水印文字成60度角倾斜,且页面居中展示 //under.showTextAligned(Element.ALIGN_CENTER, waterMarkName, pageWidth / 2, pageHeight / 2, 60); img.setAlignment(1); img.scaleAbsolute(636 / 5, 126 / 5);// 控制图片大小 img.setAbsolutePosition(pageWidth / 2, pageHeight / 2);// 控制图片位置 img.setRotation(60); stamp.getUnderContent().addImage(img); // 字体设置结束 under.endText(); stamp.alterContents(); copy.addPage(page); } } catch (Exception e) { e.printStackTrace(); return -1; } finally { if (null != document) { document.close(); } } System.out.println("pdf totalpages:" + num); return num; }