List of usage examples for org.apache.pdfbox.pdmodel.common PDStream createInputStream
public COSInputStream createInputStream() throws IOException
From source file:net.padaf.preflight.helpers.MetadataValidationHelper.java
License:Apache License
/** * Return the xpacket from the dictionary's stream */// ww w . j a v a 2 s . co m public static byte[] getXpacket(COSDocument cdocument) throws IOException, XpacketParsingException { COSObject catalog = cdocument.getCatalog(); COSBase cb = catalog.getDictionaryObject(COSName.METADATA); if (cb == null) { // missing Metadata Key in catalog ValidationError error = new ValidationError(ValidationConstants.ERROR_METADATA_FORMAT, "Missing Metadata Key in catalog"); throw new XpacketParsingException("Failed while retrieving xpacket", error); } // no filter key COSDictionary metadataDictionnary = COSUtils.getAsDictionary(cb, cdocument); if (metadataDictionnary.getItem(COSName.FILTER) != null) { // should not be defined ValidationError error = new ValidationError(ValidationConstants.ERROR_SYNTAX_STREAM_INVALID_FILTER, "Filter specified in metadata dictionnary"); throw new XpacketParsingException("Failed while retrieving xpacket", error); } PDStream stream = PDStream.createFromCOS(metadataDictionnary); ByteArrayOutputStream bos = new ByteArrayOutputStream(); InputStream is = stream.createInputStream(); IOUtils.copy(is, bos); is.close(); bos.close(); return bos.toByteArray(); }
From source file:org.apache.fop.render.pdf.pdfbox.FOPPDFMultiByteFont.java
License:Apache License
private InputStream readFontFile(PDFont font) throws IOException { PDFontDescriptor fd = font.getFontDescriptor(); if (font instanceof PDType0Font) { PDCIDFont cidFont = ((PDType0Font) font).getDescendantFont(); fd = cidFont.getFontDescriptor(); }/*ww w. j a va 2 s . c om*/ PDStream ff = fd.getFontFile3(); if (ff == null) { ff = fd.getFontFile2(); if (ff == null) { ff = fd.getFontFile(); } } if (ff == null) { throw new IOException(font.getName() + " no fontfile"); } InputStream is = ff.createInputStream(); return new ByteArrayInputStream(IOUtils.toByteArray(is)); }
From source file:org.apache.fop.render.pdf.pdfbox.FOPPDFSingleByteFont.java
License:Apache License
private void loadFontFile(FontContainer font) throws IOException { PDStream ff = readFontFile(font.font); mergeFontFile(ff.createInputStream(), font); if (font.font instanceof PDTrueTypeFont) { TrueTypeFont ttfont = ((PDTrueTypeFont) font.font).getTrueTypeFont(); CmapSubtable[] cmapList = ttfont.getCmap().getCmaps(); for (CmapSubtable c : cmapList) { MergeTTFonts.Cmap tempCmap = getNewCmap(c.getPlatformId(), c.getPlatformEncodingId()); for (int i = 0; i < 256 * 256; i++) { int gid = c.getGlyphId(i); if (gid != 0) { tempCmap.glyphIdToCharacterCode.put(i, gid); }/*from www . j ava 2s.co m*/ } } FOPPDFMultiByteFont.mergeMaxp(ttfont, ((MergeTTFonts) mergeFonts).maxp); } }
From source file:pdf.PdfBuilder.java
public void build(Stammdaten sta) { try (InputStream is = getClass().getResourceAsStream("sta.pdf"); //get pdfobject PDDocument pdfTemplate = PDDocument.load(is)) { PDDocumentCatalog docCatalog = pdfTemplate.getDocumentCatalog(); PDAcroForm acroForm = docCatalog.getAcroForm(); //Set Data /*//from w ww .j a v a 2s . c o m acroForm.getField(PLZ).setValue(sta.getPostleitzahl()); acroForm.getField(SITZ).setValue(sta.getSitz()); acroForm.getField(STAAT).setValue(sta.getStaat()); acroForm.getField(ISO).setValue(sta.getISO()); acroForm.getField(BERUF).setValue(sta.getBeruf()); acroForm.getField(GEBURTSDATUM).setValue(sta.getGeburtsdatum()); acroForm.getField(KREDITNEHMER1).setValue(sta.getKreditnehmer()); */ acroForm.getField(STAAT).setValue("test"); // Generate new File // TODO: change uuid to timestamp File saveFile = new File("STA" + UUID.randomUUID() + ".pdf"); if (saveFile.createNewFile()) { System.out.println("File is created!"); } else { System.out.println("File already exists."); } // Save edited file pdfTemplate.save(saveFile); PDStream ps = new PDStream(pdfTemplate); InputStream finalPDF = ps.createInputStream(); ; } catch (IOException | COSVisitorException ex) { Logger.getLogger(PdfBuilder.class.getName()).log(Level.SEVERE, null, ex); } }