List of usage examples for com.itextpdf.text.pdf PdfReader getPageRotation
int getPageRotation(final PdfDictionary page)
From source file:Separator.java
License:Apache License
public static void crop(String inFileName, String outFileName, int split, boolean vert) throws Exception { PdfReader reader = new PdfReader(new File(inFileName).getAbsolutePath()); String PDFName = outFileName.substring(outFileName.lastIndexOf("Folder\\") + 7, outFileName.indexOf(".pdf")); File fn = new File(scanFolder + PDFName + "\\" + PDFName + "_split_" + split + ".pdf"); fn.getParentFile().mkdirs();/* w w w .j ava2 s .c o m*/ int count = reader.getNumberOfPages(); Document doc = new Document(); PdfCopy copy = new PdfCopy(doc, new FileOutputStream(fn.getAbsolutePath())); doc.open(); if (vert) { for (int i = 1; i <= count; i++) { reader.getPageN(i).put(PdfName.CROPBOX, new PdfRectangle(PageSize.LETTER)); copy.addPage(copy.getImportedPage(reader, i)); } } else { if (!doubSided) { for (int j = 1; j <= count; j++) { reader.getPageN(j).put(PdfName.CROPBOX, new PdfRectangle((new Rectangle(0, 180, 792, 792)))); PdfDictionary pageDict; int rot = reader.getPageRotation(j); pageDict = reader.getPageN(j); pageDict.put(PdfName.ROTATE, new PdfNumber(rot + 90)); copy.addPage(copy.getImportedPage(reader, j)); } } else { for (int j = 1; j <= count; j++) { reader.getPageN(j).put(PdfName.CROPBOX, new PdfRectangle(new Rectangle(0, 180, 792, 792))); PdfDictionary pageDict; pageDict = reader.getPageN(j); if (j % 2 == 0) { // even pageDict.put(PdfName.ROTATE, new PdfNumber(270)); } else { // odd pageDict.put(PdfName.ROTATE, new PdfNumber(90)); } copy.addPage(copy.getImportedPage(reader, j)); } } } doc.close(); }
From source file:at.laborg.briss.CropManager.java
License:Open Source License
private static void cropMultipliedFile(File source, CropJob cropJob) throws FileNotFoundException, DocumentException, IOException { PdfReader reader = new PdfReader(source.getAbsolutePath()); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(cropJob.getDestinationFile())); stamper.setMoreInfo(cropJob.getSourceMetaInfo()); PdfDictionary pageDict;/*from w w w . j a va 2 s .c om*/ int newPageNumber = 1; for (int origPageNumber = 1; origPageNumber <= cropJob.getSourcePageCount(); origPageNumber++) { SingleCluster cluster = cropJob.getClusterCollection().getSingleCluster(origPageNumber); // if no crop was selected do nothing if (cluster.getRatiosList().size() == 0) { newPageNumber++; continue; } for (Float[] ratios : cluster.getRatiosList()) { pageDict = reader.getPageN(newPageNumber); List<Rectangle> boxes = new ArrayList<Rectangle>(); boxes.add(reader.getBoxSize(newPageNumber, "media")); boxes.add(reader.getBoxSize(newPageNumber, "crop")); int rotation = reader.getPageRotation(newPageNumber); Rectangle scaledBox = calculateScaledRectangle(boxes, ratios, rotation); PdfArray scaleBoxArray = new PdfArray(); scaleBoxArray.add(new PdfNumber(scaledBox.getLeft())); scaleBoxArray.add(new PdfNumber(scaledBox.getBottom())); scaleBoxArray.add(new PdfNumber(scaledBox.getRight())); scaleBoxArray.add(new PdfNumber(scaledBox.getTop())); pageDict.put(PdfName.CROPBOX, scaleBoxArray); pageDict.put(PdfName.MEDIABOX, scaleBoxArray); // increment the pagenumber newPageNumber++; } int[] range = new int[2]; range[0] = newPageNumber - 1; range[1] = cropJob.getSourcePageCount() + (newPageNumber - origPageNumber); SimpleBookmark.shiftPageNumbers(cropJob.getSourceBookmarks(), cluster.getRatiosList().size() - 1, range); } stamper.setOutlines(cropJob.getSourceBookmarks()); stamper.close(); reader.close(); }
From source file:at.laborg.briss.utils.DocumentCropper.java
License:Open Source License
private static void cropMultipliedFile(final CropDefinition cropDefinition, final File multipliedDocument, final PdfMetaInformation pdfMetaInformation) throws DocumentException, IOException { PdfReader reader = new PdfReader(multipliedDocument.getAbsolutePath()); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(cropDefinition.getDestinationFile())); stamper.setMoreInfo(pdfMetaInformation.getSourceMetaInfo()); PdfDictionary pageDict;/* w w w . j a v a 2s. com*/ int newPageNumber = 1; for (int sourcePageNumber = 1; sourcePageNumber <= pdfMetaInformation .getSourcePageCount(); sourcePageNumber++) { List<Float[]> rectangleList = cropDefinition.getRectanglesForPage(sourcePageNumber); // if no crop was selected do nothing if (rectangleList.isEmpty()) { newPageNumber++; continue; } for (Float[] ratios : rectangleList) { pageDict = reader.getPageN(newPageNumber); List<Rectangle> boxes = new ArrayList<Rectangle>(); boxes.add(reader.getBoxSize(newPageNumber, "media")); boxes.add(reader.getBoxSize(newPageNumber, "crop")); int rotation = reader.getPageRotation(newPageNumber); Rectangle scaledBox = RectangleHandler.calculateScaledRectangle(boxes, ratios, rotation); PdfArray scaleBoxArray = createScaledBoxArray(scaledBox); pageDict.put(PdfName.CROPBOX, scaleBoxArray); pageDict.put(PdfName.MEDIABOX, scaleBoxArray); // increment the pagenumber newPageNumber++; } int[] range = new int[2]; range[0] = newPageNumber - 1; range[1] = pdfMetaInformation.getSourcePageCount() + (newPageNumber - sourcePageNumber); SimpleBookmark.shiftPageNumbers(pdfMetaInformation.getSourceBookmarks(), rectangleList.size() - 1, range); } stamper.setOutlines(pdfMetaInformation.getSourceBookmarks()); stamper.close(); reader.close(); }
From source file:de.aidger.utils.pdf.BalanceReportConverter.java
License:Open Source License
/** * Places the created document onto the template for this report. * //from ww w . java 2 s.c om * @param file * The file to which this report will be saved. * @param preTemplateFile * The report to be used. */ private boolean applyTemplate(File file, File preTemplateFile) { FileOutputStream outStream = null; FileInputStream inStream = null; PdfContentByte contentByte = null; PdfReader reader = null, templateReader = null; try { /* * Use the template located in the configuration path first, if it * exists. */ File template = new File( Runtime.getInstance().getConfigPath() + "/templates/BalanceReportTemplate.pdf"); URL templateURL = null; if (template.exists()) { templateURL = template.toURI().toURL(); } else { templateURL = getClass().getResource("/de/aidger/res/pdf/BalanceReportTemplate.pdf"); } if (templateURL == null) { throw new FileNotFoundException(_("The report template could not be loaded.") + " " + _("Please make sure that a fitting template exists in the template folder.")); } Document document = new Document(PageSize.A4.rotate()); outStream = new FileOutputStream(file.getPath()); inStream = new FileInputStream(preTemplateFile); writer = PdfWriter.getInstance(document, outStream); document.open(); contentByte = writer.getDirectContent(); reader = new PdfReader(inStream); templateReader = new PdfReader(templateURL); /* * Add the template pdf to the document and place the finished * report on top of it. */ for (int i = 1; i <= reader.getNumberOfPages(); i++) { document.newPage(); PdfImportedPage page = writer.getImportedPage(templateReader, 1); int rotation = templateReader.getPageRotation(1); if (rotation == 90 || rotation == 270) { //landscape mode contentByte.addTemplate(page, 0, -1f, 1f, 0, 0, reader.getPageSizeWithRotation(1).getHeight()); } else { //portrait mode contentByte.addTemplate(page, 1f, 0, 0, 1f, 0, 0); } page = writer.getImportedPage(reader, i); rotation = reader.getPageRotation(i); if (rotation == 90 || rotation == 270) { //landscape mode contentByte.addTemplate(page, 0, -1f, 1f, 0, 0, reader.getPageSizeWithRotation(1).getHeight()); } else { //portrait mode contentByte.addTemplate(page, 1f, 0, 0, 1f, 0, 0); } } document.close(); return true; } catch (FileNotFoundException e) { if (e.getMessage() != null) { UI.displayError(e.getMessage()); } } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return false; }
From source file:de.aidger.utils.pdf.BudgetReportConverter.java
License:Open Source License
/** * Places the created document onto the template for this report. * /*ww w . j av a 2 s .co m*/ * @param file * The file to which this report will be saved. * @param preTemplateFile * The report to be used. */ private boolean applyTemplate(File file, File preTemplateFile) { FileOutputStream outStream = null; FileInputStream inStream = null; PdfContentByte contentByte = null; PdfReader reader = null, templateReader = null; try { /* * Use the template located in the configuration path first, if it * exists. */ File template = new File(Runtime.getInstance().getConfigPath() + "/templates/BudgetReportTemplate.pdf"); URL templateURL = null; if (template.exists()) { templateURL = template.toURI().toURL(); } else { templateURL = getClass().getResource("/de/aidger/res/pdf/BudgetReportTemplate.pdf"); } if (templateURL == null) { throw new FileNotFoundException(_("The report template could not be loaded.") + " " + _("Please make sure that a fitting template exists in the template folder.")); } Document document = new Document(PageSize.A4); outStream = new FileOutputStream(file.getPath()); inStream = new FileInputStream(preTemplateFile); writer = PdfWriter.getInstance(document, outStream); document.open(); contentByte = writer.getDirectContent(); reader = new PdfReader(inStream); templateReader = new PdfReader(templateURL); /* * Add the template pdf to the document and place the finished * report on top of it. */ for (int i = 1; i <= reader.getNumberOfPages(); i++) { document.newPage(); PdfImportedPage page = writer.getImportedPage(templateReader, 1); int rotation = templateReader.getPageRotation(1); if (rotation == 90 || rotation == 270) { //landscape mode contentByte.addTemplate(page, 0, -1f, 1f, 0, 0, reader.getPageSizeWithRotation(1).getHeight()); } else { //portrait mode contentByte.addTemplate(page, 1f, 0, 0, 1f, 0, 0); } page = writer.getImportedPage(reader, i); rotation = reader.getPageRotation(i); if (rotation == 90 || rotation == 270) { //landscape mode contentByte.addTemplate(page, 0, -1f, 1f, 0, 0, reader.getPageSizeWithRotation(1).getHeight()); } else { //portrait mode contentByte.addTemplate(page, 1f, 0, 0, 1f, 0, 0); } } document.close(); return true; } catch (FileNotFoundException e) { if (e.getMessage() != null) { UI.displayError(e.getMessage()); } } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return false; }
From source file:de.aidger.utils.pdf.ControllingConverter.java
License:Open Source License
/** * Places the created document onto the template for this report. * /* ww w.j a v a 2 s .c om*/ * @param file * The file to which this report will be saved. * @param preTemplateFile * The report to be used. */ private boolean applyTemplate(File file, File preTemplateFile) { FileOutputStream outStream = null; FileInputStream inStream = null; PdfContentByte contentByte = null; PdfReader reader = null, templateReader = null; try { /* * Use the template located in the configuration path first, if it * exists. */ File template = new File(Runtime.getInstance().getConfigPath() + "/templates/ControllingTemplate.pdf"); URL templateURL = null; if (template.exists()) { templateURL = template.toURI().toURL(); } else { templateURL = getClass().getResource("/de/aidger/res/pdf/ControllingTemplate.pdf"); } if (templateURL == null) { throw new FileNotFoundException(_("The report template could not be loaded.") + " " + _("Please make sure that a fitting template exists in the template folder.")); } Document document = new Document(PageSize.A4); outStream = new FileOutputStream(file.getPath()); inStream = new FileInputStream(preTemplateFile); writer = PdfWriter.getInstance(document, outStream); document.open(); contentByte = writer.getDirectContent(); reader = new PdfReader(inStream); templateReader = new PdfReader(templateURL); /* * Add the template pdf to the document and place the finished * report on top of it. */ for (int i = 1; i <= reader.getNumberOfPages(); i++) { document.newPage(); PdfImportedPage page = writer.getImportedPage(templateReader, 1); int rotation = templateReader.getPageRotation(1); if (rotation == 90 || rotation == 270) { //landscape mode contentByte.addTemplate(page, 0, -1f, 1f, 0, 0, reader.getPageSizeWithRotation(1).getHeight()); } else { //portrait mode contentByte.addTemplate(page, 1f, 0, 0, 1f, 0, 0); } page = writer.getImportedPage(reader, i); rotation = reader.getPageRotation(i); if (rotation == 90 || rotation == 270) { //landscape mode contentByte.addTemplate(page, 0, -1f, 1f, 0, 0, reader.getPageSizeWithRotation(1).getHeight()); } else { //portrait mode contentByte.addTemplate(page, 1f, 0, 0, 1f, 0, 0); } } document.close(); return true; } catch (FileNotFoundException e) { if (e.getMessage() != null) { UI.displayError(e.getMessage()); } } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return false; }
From source file:de.aidger.utils.pdf.ProtocolConverter.java
License:Open Source License
/** * Places the created document onto the template for this report. * /*from w w w . j av a 2s . com*/ * @param file * The file to which this report will be saved. * @param preTemplateFile * The report to be used. */ private boolean applyTemplate(File file, File preTemplateFile) { FileOutputStream outStream = null; FileInputStream inStream = null; PdfContentByte contentByte = null; PdfReader reader = null, templateReader = null; try { /* * Use the template located in the configuration path first, if it * exists. */ File template = new File(Runtime.getInstance().getConfigPath() + "/templates/ProtocolTemplate.pdf"); URL templateURL = null; if (template.exists()) { templateURL = template.toURI().toURL(); } else { templateURL = getClass().getResource("/de/aidger/res/pdf/ProtocolTemplate.pdf"); } if (templateURL == null) { throw new FileNotFoundException(_("The report template could not be loaded.") + " " + _("Please make sure that a fitting template exists in the template folder.")); } Document document = new Document(PageSize.A4.rotate()); outStream = new FileOutputStream(file.getPath()); inStream = new FileInputStream(preTemplateFile); writer = PdfWriter.getInstance(document, outStream); document.open(); contentByte = writer.getDirectContent(); reader = new PdfReader(inStream); templateReader = new PdfReader(templateURL); /* * Add the template pdf to the document and place the finished * report on top of it. */ for (int i = 1; i <= reader.getNumberOfPages(); i++) { document.newPage(); PdfImportedPage page = writer.getImportedPage(templateReader, 1); int rotation = templateReader.getPageRotation(1); if (rotation == 90 || rotation == 270) { //landscape mode contentByte.addTemplate(page, 0, -1f, 1f, 0, 0, reader.getPageSizeWithRotation(1).getHeight()); } else { //portrait mode contentByte.addTemplate(page, 1f, 0, 0, 1f, 0, 0); } page = writer.getImportedPage(reader, i); rotation = reader.getPageRotation(i); if (rotation == 90 || rotation == 270) { //landscape mode contentByte.addTemplate(page, 0, -1f, 1f, 0, 0, reader.getPageSizeWithRotation(1).getHeight()); } else { //portrait mode contentByte.addTemplate(page, 1f, 0, 0, 1f, 0, 0); } } document.close(); return true; } catch (FileNotFoundException e) { if (e.getMessage() != null) { UI.displayError(e.getMessage()); } } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return false; }
From source file:de.gbv.marginalia.Marginalia.java
License:Open Source License
/** * Inspect a PDF file and write the info to a writer * @param writer Writer to a text file// w w w . ja v a 2s. c o m * @param filename Path to the PDF file * @throws IOException */ public static void inspect(PrintWriter writer, String filename) throws IOException, SAXException { // writer.println(filename); writer.flush(); PdfReader reader = new PdfReader(filename); ContentHandler xmlhandler = new SimpleXMLWriter(writer); xmlhandler.startDocument(); SimpleXMLCreator xml = new SimpleXMLCreator(xmlhandler, Annotation.namespaces, true); /* writer.println("Number of pages: "+reader.getNumberOfPages()); Rectangle mediabox = reader.getPageSize(1); writer.print("Size of page 1: ["); writer.print(mediabox.getLeft()); writer.print(','); writer.print(mediabox.getBottom()); writer.print(','); writer.print(mediabox.getRight()); writer.print(','); writer.print(mediabox.getTop()); writer.println("]"); writer.print("Rotation of page 1: "); writer.println(reader.getPageRotation(1)); writer.print("Page size with rotation of page 1: "); writer.println(reader.getPageSizeWithRotation(1)); writer.println(); writer.flush(); */ List<Annotation> annots = new LinkedList<Annotation>(); xml.startElement("annots"); // TODO: The following elements may be added: // - optionally write <f href="Document.pdf"/> // - optionally write <ids original="ID" modified="ID" /> xml.startElement("m", "pages"); for (int pageNum = 1; pageNum <= reader.getNumberOfPages(); pageNum++) { PdfDictionary pageDic = reader.getPageN(pageNum); Map<String, String> attr = new HashMap<String, String>(); attr.put("number", "" + pageNum); attr.put("rotate", "" + reader.getPageRotation(pageNum)); Rectangle mediabox = reader.getPageSize(pageNum); attr.put("left", "" + mediabox.getLeft()); attr.put("bottom", "" + mediabox.getBottom()); attr.put("right", "" + mediabox.getRight()); attr.put("top", "" + mediabox.getTop()); xml.contentElement("m", "page", "", attr); PdfArray rawannots = pageDic.getAsArray(PdfName.ANNOTS); if (rawannots == null || rawannots.isEmpty()) { // writer.println("page "+pageNum+" contains no annotations"); continue; } // writer.println("page "+pageNum+" has "+rawannots.size()+" annotations"); for (int i = 0; i < rawannots.size(); i++) { PdfObject obj = rawannots.getDirectObject(i); if (!obj.isDictionary()) continue; Annotation a = new Annotation((PdfDictionary) obj, pageNum); annots.add(a); } /** // Now we have all highlight and similar annotations, we need // to find out what words are actually highlighted! PDF in fact // is a dump format to express documents. // For some hints see // http://stackoverflow.com/questions/4028240/extract-each-column-of-a-pdf-file // We could reuse code from LocationTextExtractionStrategy (TODO) // LocationTextExtractionStrategy extr = new LocationTextExtractionStrategy(); String fulltext = PdfTextExtractor.getTextFromPage(reader,pageNum);//,extr writer.println(fulltext); */ } xml.endElement(); for (Annotation a : annots) { a.serializeXML(xmlhandler); } // TODO: add page information (page size and orientation) xml.endAll(); }
From source file:EplanPrinter.PDFPrint.java
License:Open Source License
public String createPDF(String input, String output) throws FileNotFoundException, DocumentException, IOException { heightScalar = 0;// w w w. j a va2s.c o m //Addition. linh 07.21.2016 bypass owner password PdfReader.unethicalreading = true; PdfReader reader = new PdfReader(input); //Sets up the stamper, which is what adds all the content to the page. pds = new PdfStamper(reader, new FileOutputStream(output)); PdfImportedPage page; page = pds.getImportedPage(reader, 1); PdfContentByte bg; totalPages = reader.getNumberOfPages(); r = new Rectangle[totalPages]; rotation = new int[totalPages]; for (int x = 0; x < totalPages; x++) { r[x] = reader.getPageSizeWithRotation(x + 1); rotation[x] = reader.getPageRotation(x + 1); } mediabox = getDocumentMediaBox(reader, totalPages); //r = reader.getPageSizeWithRotation(1); //setRatio(); bg = pds.getUnderContent(1); heightScalar = 0; return ""; }
From source file:org.gmdev.pdftrick.engine.MergeFiles.java
License:Open Source License
/** * Materially multiple pdf files are written merged file on a disk * @param list//from www . j a v a 2 s . c o m * @param outputStream * @throws DocumentException * @throws IOException */ private void doMerge(List<StreamPwdContainer> list, OutputStream outputStream) throws DocumentException, IOException { HashMap<Integer, String> rotationFromPages = factory.getRotationFromPages(); Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, outputStream); document.open(); PdfContentByte cb = writer.getDirectContent(); int z = 0; for (StreamPwdContainer boom : list) { InputStream in = boom.getIn(); PdfReader reader = null; if (!boom.getPwd().equalsIgnoreCase("")) { reader = new PdfReader(in, boom.getPwd().getBytes()); } else { reader = new PdfReader(in); } for (int i = 1; i <= reader.getNumberOfPages(); i++) { z++; int rotation = reader.getPageRotation(i); //set size Rectangle pageSize_ = reader.getPageSize(i); Rectangle pageSize = null; if (rotation == 270 || rotation == 90) { pageSize = new Rectangle(pageSize_.getHeight(), pageSize_.getWidth()); } else { pageSize = pageSize_; } document.setPageSize(pageSize); writer.setCropBoxSize(pageSize); document.newPage(); // import the page from source pdf PdfImportedPage page = writer.getImportedPage(reader, i); // add the page to the destination pdf if (rotation == 270) { cb.addTemplate(page, 0, 1.0f, -1.0f, 0, reader.getPageSizeWithRotation(i).getWidth(), 0); rotationFromPages.put(z, "" + rotation); } else if (rotation == 180) { cb.addTemplate(page, -1f, 0, 0, -1f, 0, 0); rotationFromPages.put(z, "" + rotation); } else if (rotation == 90) { cb.addTemplate(page, 0, -1f, 1f, 0, 0, reader.getPageSizeWithRotation(i).getHeight()); rotationFromPages.put(z, "" + rotation); } else { cb.addTemplate(page, 1f, 0, 0, 1f, 0, 0); } } in.close(); } outputStream.flush(); document.close(); outputStream.close(); }