Package org.pdfbox.pdmodel

Examples of org.pdfbox.pdmodel.PDDocument


      boolean writePDFInfo) throws IOException, TransformerException {

    if (databasee != null)
      bibtexEntries = databasee.resolveForStrings(bibtexEntries, false);

    PDDocument document = null;

    try {
      document = PDDocument.load(file.getAbsoluteFile());
      if (document.isEncrypted()) {
        throw new EncryptionNotSupportedException(
            "Error: Cannot add metadata to encrypted document.");
      }

      if (writePDFInfo && bibtexEntries.size() == 1) {
        writeDocumentInformation(document, bibtexEntries
            .iterator().next(), null);
        writeDublinCore(document, bibtexEntries, null);
      }

      PDDocumentCatalog catalog = document.getDocumentCatalog();
      PDMetadata metaRaw = catalog.getMetadata();

      XMPMetadata meta;
      if (metaRaw != null) {
        meta = new XMPMetadata(XMLUtil.parse(metaRaw
            .createInputStream()));
      } else {
        meta = new XMPMetadata();
      }
      meta.addXMLNSMapping(XMPSchemaBibtex.NAMESPACE,
          XMPSchemaBibtex.class);

      // Remove all current Bibtex-schemas
      List schemas = meta
          .getSchemasByNamespaceURI(XMPSchemaBibtex.NAMESPACE);
      Iterator it = schemas.iterator();
      while (it.hasNext()) {
        XMPSchemaBibtex bib = (XMPSchemaBibtex) it.next();
        bib.getElement().getParentNode().removeChild(bib.getElement());
      }

      it = bibtexEntries.iterator();
      while (it.hasNext()) {
        BibtexEntry e = (BibtexEntry) it.next();
        XMPSchemaBibtex bibtex = new XMPSchemaBibtex(meta);
        meta.addSchema(bibtex);
        bibtex.setBibtexEntry(e, null);
      }

      // Save to stream and then input that stream to the PDF
      ByteArrayOutputStream os = new ByteArrayOutputStream();
      meta.save(os);
      ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
      PDMetadata metadataStream = new PDMetadata(document, is, false);
      catalog.setMetadata(metadataStream);

      // Save
      try {
        document.save(file.getAbsolutePath());
      } catch (COSVisitorException e) {
        throw new TransformerException("Could not write XMP-metadata: "
            + e.getLocalizedMessage());
      }

    } finally {
      if (document != null) {
        document.close();
      }
    }
  }
View Full Code Here


    public void testExportSingleSimplePageAsPDF() throws Exception
    {
        URL url = new URL("http://localhost:8080/xwiki/bin/export/Main/WebHome?format=pdf");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        InputStream is = connection.getInputStream();
        PDDocument pdd = PDDocument.load(is);
        PDFTextStripper stripper = new PDFTextStripper();
        String text = stripper.getText(pdd);
        pdd.close();
        is.close();

        assertTrue("Invalid content", text.contains("Welcome to your wiki"));
    }
View Full Code Here

    bos.close();   
  }

  private String extractTextFromPdf(VFSLeaf leaf) throws IOException, DocumentAccessException {
    if (log.isDebug()) log.debug("readContent from pdf starts...");
    PDDocument document = null;
    BufferedInputStream bis = null;
    try {
      bis = new BufferedInputStream(leaf.getInputStream());     
      document = PDDocument.load(bis);
      if (document.isEncrypted()) {
        throw new DocumentAccessException("PDF is encrypted. Can not read content file=" + leaf.getName());
      }     
      if (log.isDebug()) log.debug("readContent PDDocument loaded");
      PDFTextStripper stripper = new PDFTextStripper();
      return stripper.getText(document);
    } finally {
      if (document != null) {
        document.close();
      }
      if (bis != null) {
        bis.close();
      }
    }
View Full Code Here

     *
     * @throws IOException If there is an error writing the data.
     */
    public PDDocument createPDFFromText( Reader text ) throws IOException
    {
        PDDocument doc = null;
        try
        {
           
            int margin = 40;
            float height = font.getFontDescriptor().getFontBoundingBox().getHeight()/1000;
           
            //calculate font height and increase by 5 percent.
            height = height*fontSize*1.05f;
            doc = new PDDocument();
            BufferedReader data = new BufferedReader( text );
            String nextLine = null;
            PDPage page = new PDPage();
            PDPageContentStream contentStream = null;
            float y = -1;
            float maxStringLength = page.getMediaBox().getWidth() - 2*margin;
            while( (nextLine = data.readLine()) != null )
            {
               
                String[] lineWords = nextLine.trim().split( " " );
                int lineIndex = 0;
                while( lineIndex < lineWords.length )
                {  
                    StringBuffer nextLineToDraw = new StringBuffer();
                    float lengthIfUsingNextWord = 0;
                    do
                    {
                        nextLineToDraw.append( lineWords[lineIndex] );
                        nextLineToDraw.append( " " );
                        lineIndex++;
                        if( lineIndex < lineWords.length )
                        {
                            String lineWithNextWord = nextLineToDraw.toString() + lineWords[lineIndex];
                            lengthIfUsingNextWord =
                                (font.getStringWidth( lineWithNextWord )/1000) * fontSize;
                        }
                    }
                    while( lineIndex < lineWords.length &&
                           lengthIfUsingNextWord < maxStringLength );
                    if( y < margin )
                    {
                        page = new PDPage();
                        doc.addPage( page );
                        if( contentStream != null )
                        {
                            contentStream.endText();
                            contentStream.close();
                        }
                        contentStream = new PDPageContentStream(doc, page);
                        contentStream.setFont( font, fontSize );
                        contentStream.beginText();
                        y = page.getMediaBox().getHeight() - margin + height;
                        contentStream.moveTextPositionByAmount(
                            margin, y );
                       
                    }
                    //System.out.println( "Drawing string at " + x + "," + y );
                   
                    if( contentStream == null )
                    {
                        throw new IOException( "Error:Expected non-null content stream." );
                    }
                    contentStream.moveTextPositionByAmount( 0, -height);
                    y -= height;
                    contentStream.drawString( nextLineToDraw.toString() );
                }
               
               
            } 
            if( contentStream != null )
            {
                contentStream.endText();
                contentStream.close();
            }
        }
        catch( IOException io )
        {
            if( doc != null )
            {
                doc.close();
            }
            throw io;
        }
        return doc;
    }
View Full Code Here

     * @throws IOException If there is an error with the PDF.
     */
    public static void main(String[] args) throws IOException
    {
        TextToPDF app = new TextToPDF();
        PDDocument doc = null;
        try
        {
            if( args.length < 2 )
            {
                app.usage();
            }
            else
            {
                for( int i=0; i<args.length-2; i++ )
                {
                    if( args[i].equals( "-standardFont" ))
                    {
                        i++;
                        app.setFont( PDType1Font.getStandardFont( args[i] ));
                    }
                    else if( args[i].equals( "-ttf" ))
                    {
                        i++;
                        PDTrueTypeFont font = PDTrueTypeFont.loadTTF( doc, new File( args[i]));
                        app.setFont( font );
                    }
                    else if( args[i].equals( "-fontSize" ))
                    {
                        i++;
                        app.setFontSize( Integer.parseInt( args[i] ) );
                    }
                    else
                    {
                        throw new IOException( "Unknown argument:" + args[i] );
                    }
                }
                doc = app.createPDFFromText( new FileReader( args[args.length-1] ) );
                doc.save( args[args.length-2] );
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            if( doc != null )
            {
                doc.close();
            }
        }
    }
View Full Code Here

        {
            usage();
        }
        else
        {
            PDDocument document = null;
            try
            {
                document = PDDocument.load( args[0] );
                if( document.isEncrypted() )
                {
                    try
                    {
                        document.decrypt( "" );
                    }
                    catch( InvalidPasswordException e )
                    {
                        System.err.println( "Error: Document is encrypted with a password." );
                        System.exit( 1 );
                    }
                }
                PDFTextStripperByArea stripper = new PDFTextStripperByArea();
                stripper.setSortByPosition( true );
                Rectangle rect = new Rectangle( 10, 280, 275, 60 );
                stripper.addRegion( "class1", rect );
                List allPages = document.getDocumentCatalog().getAllPages();
                PDPage firstPage = (PDPage)allPages.get( 0 );
                stripper.extractRegions( firstPage );
                System.out.println( "Text in the area:" + rect );
                System.out.println( stripper.getTextForRegion( "class1" ) );
               
            }
            finally
            {
                if( document != null )
                {
                    document.close();
                }
            }
        }
    }
View Full Code Here

        {
            usage();
        }
        else
        {
            PDDocument document = null;
            try
            {
                document = PDDocument.load( args[0] );
                if( document.isEncrypted() )
                {
                    System.err.println( "Error: Encrypted documents are not supported for this example." );
                    System.exit( 1 );
                }
                List allPages = document.getDocumentCatalog().getAllPages();
                for( int i=0; i<allPages.size(); i++ )
                {
                    PDPage page = (PDPage)allPages.get( i );
                    PDFStreamParser parser = new PDFStreamParser(page.getContents());
                    parser.parse();
                    List tokens = parser.getTokens();
                    List newTokens = new ArrayList();
                    for( int j=0; j<tokens.size(); j++)
                    {
                        Object token = tokens.get( j );
                        if( token instanceof PDFOperator )
                        {
                            PDFOperator op = (PDFOperator)token;
                            if( op.getOperation().equals( "TJ") || op.getOperation().equals( "Tj" ))
                            {
                                //remove the one argument to this operator
                                newTokens.remove( newTokens.size() -1 );
                                continue;
                            }
                        }
                        newTokens.add( token );
                       
                    }
                    PDStream newContents = new PDStream( document );
                    ContentStreamWriter writer = new ContentStreamWriter( newContents.createOutputStream() );
                    writer.writeTokens( newTokens );
                    newContents.addCompression();
                    page.setContents( newContents );
                }
                document.save( args[1] );
            }
            finally
            {
                if( document != null )
                {
                    document.close();
                }
            }
        }
    }
View Full Code Here

            usage();
            System.exit(1);
        }
        else
        {
            PDDocument overlay = null;
            PDDocument pdf = null;

            try
            {
                overlay = getDocument( args[0] );
                pdf = getDocument( args[1] );
                Overlay overlayer = new Overlay();
                overlayer.overlay( overlay, pdf );
                writeDocument( pdf, args[2] );
            }
            finally
            {
                if( overlay != null )
                {
                    overlay.close();
                }
                if( pdf != null )
                {
                    pdf.close();
                }
            }
        }
    }
View Full Code Here

    private static PDDocument getDocument( String filename ) throws IOException
    {
        FileInputStream input = null;
        PDFParser parser = null;
        PDDocument result = null;
        try
        {
            input = new FileInputStream( filename );
            parser = new PDFParser( input );
            parser.parse();
View Full Code Here

        setter.setField( args );
    }

    private void setField( String[] args ) throws IOException, COSVisitorException
    {
        PDDocument pdf = null;
        try
        {
            if( args.length != 3 )
            {
                usage();
            }
            else
            {
                SetField example = new SetField();

                pdf = PDDocument.load( args[0] );
                example.setField( pdf, args[1], args[2] );
                pdf.save( args[0] );
            }
        }
        finally
        {
            if( pdf != null )
            {
                pdf.close();
            }
        }
    }
View Full Code Here

TOP

Related Classes of org.pdfbox.pdmodel.PDDocument

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.