コード例 #1
0
        private static PDEmbeddedFile getEmbeddedFile(PDComplexFileSpecification fileSpec)
        {
            PDEmbeddedFile embeddedFileUnicode = null;

            if (fileSpec != null)
            {
                embeddedFileUnicode = fileSpec.getEmbeddedFileUnicode() ?? fileSpec.getEmbeddedFileDos() ?? fileSpec.getEmbeddedFileMac() ?? fileSpec.getEmbeddedFileUnix() ?? fileSpec.getEmbeddedFile();
            }
            return(embeddedFileUnicode);
        }
        /**
         * Extracts a ZUGFeRD invoice from a PDF document represented by an input stream.
         * Errors are reported via exception handling.
         */
        public void extractLowLevel(InputStream pdfStream)
        {
            PDDocument doc = null;

            try
            {
                doc = PDDocument.load(pdfStream);
                // PDDocumentInformation info = doc.getDocumentInformation();
                PDDocumentNameDictionary names = new PDDocumentNameDictionary(
                    doc.getDocumentCatalog());
                PDEmbeddedFilesNameTreeNode etn;
                etn = names.getEmbeddedFiles();
                if (etn == null)
                {
                    doc.close();
                    return;
                }
                //Set efMap = etn.getNames().keySet();
                Object[] efMap = etn.getNames().keySet().toArray();
                //Map<String, COSObjectable> efMap = etn.getNames();
                // String filePath = "/tmp/";
                //for (String filename : efMap.keySet()) {
                foreach (String filename in efMap)
                {
                    ///**
                    //* currently (in the release candidate of version 1) only one
                    //* attached file with the name ZUGFeRD-invoice.xml is allowed
                    //* */
                    if (filename.Equals("ZUGFeRD-invoice.xml"))
                    { //$NON-NLS-1$
                        containsMeta = true;
                        PDComplexFileSpecification fileSpec     = (PDComplexFileSpecification)etn.getNames().get(filename);
                        PDEmbeddedFile             embeddedFile = fileSpec.getEmbeddedFile();
                        // String embeddedFilename = filePath + filename;
                        // File file = new File(filePath + filename);
                        // System.out.println("Writing " + embeddedFilename);
                        // ByteArrayOutputStream fileBytes=new
                        // ByteArrayOutputStream();
                        // FileOutputStream fos = new FileOutputStream(file);
                        rawXML = embeddedFile.getByteArray();
                        setMeta(Encoding.UTF8.GetString(rawXML));
                        // fos.write(embeddedFile.getByteArray());
                        // fos.close();
                    }
                }
            }
            catch (IOException e1)
            {
                throw e1;
            }
            finally
            {
                try
                {
                    if (doc != null)
                    {
                        doc.close();
                    }
                }
                catch (IOException e) { }
            }
        }