List of usage examples for com.itextpdf.text.pdf PdfWriter getStructureTreeRoot
public PdfStructureTreeRoot getStructureTreeRoot()
From source file:org.orbisgis.core_export.GeoSpatialPDF.java
License:Open Source License
/** * This method is used to georeference the pdf. * Note : The CRS is not yet supported.//from w ww. ja va 2 s .com * * @param writer * @param mt * @throws IOException * @throws DocumentException */ public void georefPdf(PdfWriter writer, MapTransform mt) throws IOException, DocumentException { PdfStructureTreeRoot tree = writer.getStructureTreeRoot(); //the part of the document where maps are displayed float mapWidth = width; float mapHeight = height; float mapLX = 0; float mapLY = 0; //ViewPort Dictionary PdfDictionary viewPortDict = new PdfDictionary(); viewPortDict.put(PdfName.TYPE, new PdfName("Viewport")); viewPortDict.put(PdfName.BBOX, new PdfRectangle(mapLX, mapLY, mapLX + mapWidth, mapLY + mapHeight)); viewPortDict.put(PdfName.NAME, new PdfString("Layers")); //Measure dictionary PdfDictionary measureDict = new PdfDictionary(); measureDict.put(PdfName.TYPE, new PdfName("Measure")); measureDict.put(PdfName.SUBTYPE, new PdfName("GEO")); //Bounds PdfArray bounds = new PdfArray(); bounds.add(new PdfNumber(0)); bounds.add(new PdfNumber(0)); bounds.add(new PdfNumber(1)); bounds.add(new PdfNumber(0)); bounds.add(new PdfNumber(1)); bounds.add(new PdfNumber(1)); bounds.add(new PdfNumber(0)); bounds.add(new PdfNumber(1)); measureDict.put(new PdfName("Bounds"), bounds); //GPTS Envelope adjustedBbox = mt.getAdjustedExtent(); if (!adjustedBbox.isNull()) { //ly lx ly ux uy ux uy lx PdfArray gptsTable = new PdfArray(); gptsTable.add(new PdfNumber(((Double) adjustedBbox.getMinY()).toString())); gptsTable.add(new PdfNumber(((Double) adjustedBbox.getMinX()).toString())); gptsTable.add(new PdfNumber(((Double) adjustedBbox.getMinY()).toString())); gptsTable.add(new PdfNumber(((Double) adjustedBbox.getMaxX()).toString())); gptsTable.add(new PdfNumber(((Double) adjustedBbox.getMaxY()).toString())); gptsTable.add(new PdfNumber(((Double) adjustedBbox.getMaxX()).toString())); gptsTable.add(new PdfNumber(((Double) adjustedBbox.getMaxY()).toString())); gptsTable.add(new PdfNumber(((Double) adjustedBbox.getMinX()).toString())); measureDict.put(new PdfName("GPTS"), gptsTable); //The CRS will be added when the mapcontext will support it. // //GCS Geospatial Coordinate system // PdfDictionary gcsDict = new PdfDictionary(); // if (context.getBbox().getCrs() != null) { // // if (context.getBbox().getCrs().getType() != null) { // gcsDict.put(PdfName.TYPE, new PdfName(context.getBbox().getCrs().getType())); // } else { // LOGGER.warn("No type of crs : the pdf cannot be georeferenced"); // return; // } // // if (context.getBbox().getCrs().getEpsg() != 0) { // gcsDict.put(new PdfName("EPSG"), new PdfNumber(context.getBbox().getCrs().getEpsg())); // } else { // LOGGER.warn("No epsg : the pdf cannot be georeferenced"); // return; // } // // // } else { // LOGGER.warn("No crs : the pdf cannot be georeferenced"); // // } // // measureDict.put(new PdfName("GCS"), gcsDict); } else { LOGGER.warn("Envelope of bbox null : the pdf cannot be georeferenced"); } //PDU : array of units PdfArray pdu = new PdfArray(); pdu.add(new PdfName("KM")); pdu.add(new PdfName("SQKM")); pdu.add(new PdfName("DEG")); measureDict.put(new PdfName("PDU"), pdu); //LPTS PdfArray lptsTable = new PdfArray(); lptsTable.add(new PdfNumber(0)); lptsTable.add(new PdfNumber(0)); lptsTable.add(new PdfNumber(1)); lptsTable.add(new PdfNumber(0)); lptsTable.add(new PdfNumber(1)); lptsTable.add(new PdfNumber(1)); lptsTable.add(new PdfNumber(0)); lptsTable.add(new PdfNumber(1)); measureDict.put(new PdfName("LPTS"), lptsTable); viewPortDict.put(new PdfName("Measure"), measureDict); PdfStructureElement top = new PdfStructureElement(tree, new PdfName("VP")); top.putAll(viewPortDict); }
From source file:pdfpruefbericht.PdfPruefbericht.java
License:Open Source License
public static void main(String[] args) throws IOException, DocumentException, SAXException, ParserConfigurationException, URISyntaxException { String RESULT = args[1];/*from w w w. j av a 2s . c o m*/ String RESOURCE = args[0]; ContentParser cp; float compression; try { compression = Float.parseFloat(args[2]); } catch (NumberFormatException e) { System.out.println("Kompresion konnte nicht geparsed werden ... : " + e.getMessage()); compression = 0.5f; } try { Document document = new Document(PageSize.A4); document.setMargins(36, 72, 36, 144); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT)); writer.setTagged(); document.open(); PdfStructureTreeRoot root = writer.getStructureTreeRoot(); root.mapRole(new PdfName("Report"), PdfName.ROOT); root.mapRole(new PdfName("Norm"), PdfName.SECT); root.mapRole(new PdfName("Test"), PdfName.SEPARATION); root.mapRole(new PdfName("Comment"), PdfName.CO); root.mapRole(new PdfName("img"), PdfName.PI); root.mapRole(new PdfName("log"), PdfName.L); root.mapRole(new PdfName("Setup"), PdfName.SETTINGS); root.mapRole(new PdfName("device"), PdfName.D); root.mapRole(new PdfName("parameter"), PdfName.P); PdfStructureElement top = new PdfStructureElement(root, new PdfName("Report")); SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); List<PdfStructureElement> elements = new ArrayList<>(); cp = new ContentParser(document, writer, elements); parser.parse(new InputSource(new FileInputStream(RESOURCE)), new StructureParser(top, elements)); parser.parse(new InputSource(new FileInputStream(RESOURCE)), cp); PdfBuilder pdf = new PdfBuilder(document, writer, cp.getReport(), compression); pdf.build(); document.close(); } catch (IOException | DocumentException | SAXException | ParserConfigurationException e) { System.out.println(e.getMessage()); } //watermark watermark = new watermark(); }