Package com.itextpdf.text

Examples of com.itextpdf.text.Document


    @Ignore("validity of test needs to be resolved")
    @Test
    public void testGetLink() throws Exception {
        File testFile = TestResourceUtils.getResourceAsTempFile(this, "getLinkTest1.pdf");
        PdfReader currentReader = new PdfReader(testFile.getAbsolutePath());
        Document document = new Document(PageSize.A4, 0, 0, 0, 0);
        PdfWriter writer = PdfWriter.getInstance(document, new
                ByteArrayOutputStream());
        document.open();
        document.newPage();
        List links = currentReader.getLinks(1);
        PdfAnnotation.PdfImportedLink link =
            (PdfAnnotation.PdfImportedLink) links.get(0);
        writer.addAnnotation(link.createAnnotation(writer));
        document.close();
    }
View Full Code Here


    File stamp;
    File multiPageStamp;
    File out;
   
    private void createTempFile(String filename, String[] pageContents) throws Exception{
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(filename));
        document.open();
       
        for (int i = 0; i < pageContents.length; i++) {
            if (i != 0)
                document.newPage();

            String content = pageContents[i];
            Chunk contentChunk = new Chunk(content);
            document.add(contentChunk);
        }
       
       
        document.close();
    }
View Full Code Here

    public void tearDown() throws Exception {
        cleanTempFiles();
    }

    public void mergeAndStampPdf(boolean resetStampEachPage, File[] in, File out, File stamp) throws Exception {
        Document document = new Document();
       
        PdfCopy writer = new PdfSmartCopy(document, new FileOutputStream(out));
       
        document.open();
       
        int stampPageNum = 1;

        PdfReader stampReader = new PdfReader(stamp.getPath());
        for (int inNum = 0; inNum < in.length; inNum++){
            // create a reader for the input document
            PdfReader documentReader = new PdfReader(in[inNum].getPath());
           
            for (int pageNum = 1; pageNum <= documentReader.getNumberOfPages(); pageNum++){
           
                // import a page from the main file
                PdfImportedPage mainPage = writer.getImportedPage(documentReader, pageNum);
       
                // make a stamp from the page and get under content...
                PdfCopy.PageStamp pageStamp = writer.createPageStamp(mainPage);
        
                // import a page from a file with the stamp...
                if (resetStampEachPage)
                    stampReader = new PdfReader(stamp.getPath());
                PdfImportedPage stampPage = writer.getImportedPage(stampReader, stampPageNum++);
       
                // add the stamp template, update stamp, and add the page
                pageStamp.getOverContent().addTemplate(stampPage, 0, 0);
                pageStamp.alterContents();
                writer.addPage(mainPage);
               
                if (stampPageNum > stampReader.getNumberOfPages())
                    stampPageNum = 1;
            }
        }       
       
        writer.close();
        document.close();
    }
View Full Code Here

public class MetaDataTest {

    @Test
    public void testProducer() throws Exception {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Document document = new Document();

        PdfWriter.getInstance(document, baos);
        document.open();
        document.add(new Paragraph("Hello World"));
        document.close();
       
        PdfReader r = new PdfReader(baos.toByteArray());

        assertEquals("Producer", r.getInfo().get("Producer"), Document.getVersion());
    }
View Full Code Here

    }
   
    private static byte[] createPdfWithArrayText(String directContentTj) throws Exception{
        final ByteArrayOutputStream byteStream = new ByteArrayOutputStream();

        final Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, byteStream);
        document.setPageSize(PageSize.LETTER);

        document.open();

        PdfContentByte cb = writer.getDirectContent();

        BaseFont font = BaseFont.createFont();
       
        cb.transform(AffineTransform.getTranslateInstance(100, 500));
        cb.beginText();
        cb.setFontAndSize(font, 12);

        cb.getInternalBuffer().append(directContentTj + "\n");
       
        cb.endText();
       
        document.close();

        final byte[] pdfBytes = byteStream.toByteArray();

        return pdfBytes;
View Full Code Here

    }   
   
    private static byte[] createPdfWithArrayText(String text1, String text2, int spaceInPoints) throws Exception{
        final ByteArrayOutputStream byteStream = new ByteArrayOutputStream();

        final Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, byteStream);
        document.setPageSize(PageSize.LETTER);

        document.open();

        PdfContentByte cb = writer.getDirectContent();

        BaseFont font = BaseFont.createFont();
       
       
        cb.beginText();
        cb.setFontAndSize(font, 12);

        cb.getInternalBuffer().append("[(" + text1 + ")" + (-spaceInPoints) + "(" + text2 + ")]TJ\n");
       
        cb.endText();
       
        document.close();

        final byte[] pdfBytes = byteStream.toByteArray();

        return pdfBytes;
View Full Code Here

   
    private static byte[] createPdfWithRotatedText(String text1, String text2, float rotation, boolean moveTextToNextLine, float moveTextDelta) throws Exception {

        final ByteArrayOutputStream byteStream = new ByteArrayOutputStream();

        final Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, byteStream);
        document.setPageSize(PageSize.LETTER);

        document.open();

        PdfContentByte cb = writer.getDirectContent();

        BaseFont font = BaseFont.createFont();

        float x = document.getPageSize().getWidth()/2;
        float y = document.getPageSize().getHeight()/2;
       
        cb.transform(AffineTransform.getTranslateInstance(x, y));

        cb.moveTo(-10, 0);
        cb.lineTo(10, 0);
        cb.moveTo(0, -10);
        cb.lineTo(0, 10);
        cb.stroke();
       
        cb.beginText();
        cb.setFontAndSize(font, 12);
        cb.transform(AffineTransform.getRotateInstance(rotation/180f*Math.PI));
        cb.showText(text1);
        if (moveTextToNextLine)
            cb.moveText(0, moveTextDelta);
        else
            cb.transform(AffineTransform.getTranslateInstance(moveTextDelta, 0));
        cb.showText(text2);
        cb.endText();

        document.close();

        final byte[] pdfBytes = byteStream.toByteArray();

        return pdfBytes;
    }
View Full Code Here

    private Document document;
    private PdfWriter writer;
   
    private void initializeDocument() throws DocumentException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        document = new Document();
        writer = PdfWriter.getInstance(document, baos);
        document.open();       
    }
View Full Code Here

    private static final String TITLE = "1.\u00a0Paragraph 1";

    @Test
    public void testNoBreakSpace() throws Exception {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Document document = new Document();

        PdfWriter writer = PdfWriter.getInstance(document, baos);

        document.open();
        writer.setPageEvent(new PdfPageEventHelper() {
            public void onParagraph(PdfWriter writer, Document document, float position) {
                PdfContentByte cb = writer.getDirectContent();
                PdfDestination destination = new PdfDestination(PdfDestination.FITH, position);
                new PdfOutline(cb.getRootOutline(), destination, TITLE);
            }           
        });
        document.add(new Paragraph("Hello World"));
        document.close();
       
        // read bookmark back
        PdfReader r = new PdfReader(baos.toByteArray());

        List bookmarks = SimpleBookmark.getBookmark(r);
View Full Code Here

        collection = Arrays.toList(sources);
      } else if (sources instanceof Collection) {
        collection = (Collection<Object>) sources;
      }

      Document document = new Document();
      ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
      PdfCopy writer = new PdfCopy(document, outputStream);

      document.open();

      for (Object source : collection) {
        PdfReader reader = IText.read(source);

        int pageCount = reader.getNumberOfPages();
        for (int i = 1; i <= pageCount; i++) {
          PdfImportedPage page = writer.getImportedPage(reader, i);
          writer.addPage(page);
        }
        reader.close();
      }

      document.close();
      writer.close();

      return new DocumentOutput(new ByteArrayInputStream(outputStream.toByteArray()));
    } catch (IOException e) {
      throw new PDFException(e);
View Full Code Here

TOP

Related Classes of com.itextpdf.text.Document

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.