Package com.itextpdf.text

Examples of com.itextpdf.text.Document


      PdfReader reader = IText.read(source);
      int pageCount = reader.getNumberOfPages();
      List<InputStream> list = new LinkedList<InputStream>();

      Document document = null;
      ByteArrayOutputStream outputStream = null;
      PdfCopy writer = null;
      int iSize = size.intValue();
      for (int i = 1; i <= pageCount; i++) {
        if ((document == null) || ((i % iSize) == 0)) {
          if (document != null) {
            document.close();
            writer.close();
            list.add(new ByteArrayInputStream(outputStream.toByteArray()));
          }
          document = new Document(reader.getPageSizeWithRotation(1));
          outputStream = new ByteArrayOutputStream();
          writer = new PdfCopy(document, outputStream);
        }
        PdfImportedPage page = writer.getImportedPage(reader, i);
        writer.addPage(page);
      }

      if (document != null) {
        document.close();
        writer.close();
        list.add(new ByteArrayInputStream(outputStream.toByteArray()));
      }

      reader.close();
View Full Code Here


        end = Integer.valueOf(0);
      }

      PdfReader reader = IText.read(source);

      Document document = new Document(reader.getPageSizeWithRotation(1));
      ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
      PdfCopy writer = new PdfCopy(document, outputStream);
      int pageCount = reader.getNumberOfPages();

      if (start.intValue() > pageCount) {
        throw new DocumentException("Start page is greater than page count");
      }

      int endPage = end.intValue();
      if ((endPage < 1) || (endPage > pageCount)) {
        endPage = pageCount;
      }

      document.open();

      for (int i = start.intValue(); i <= endPage; i++) {
        PdfImportedPage page = writer.getImportedPage(reader, i);
        writer.addPage(page);
      }

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

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

        throw new DocumentException("Invalid HTML source type");
      }

      Charset cCharset = IText.getCharset(charset);

      Document document = new Document();
      ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
      PdfWriter writer = PdfWriter.getInstance(document, outputStream);

      if (margin != null) {
        float f = this.getMargin(margin);
        writer.setMargins(f, f, f, f);
      } else {
        writer.setMargins(this.getMargin(marginLeft), this.getMargin(marginRight), this.getMargin(marginTop), this.getMargin(marginBottom));
      }

      document.open();

      XMLWorkerHelper workerHelper = XMLWorkerHelper.getInstance();

      HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
      htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());

      if (Conditions.isNotEmpty(imagesRootPath)) {
        htmlContext.setImageProvider(new AbstractImageProvider() {

          @Override
          public String getImageRootPath() {

            return imagesRootPath;

          }

        });
      }

      HtmlPipeline htmlPipeline = new HtmlPipeline(htmlContext, new PdfWriterPipeline(document, writer));

      CSSResolver cssResolver = workerHelper.getDefaultCssResolver(true);

      Pipeline<?> pipeline = new CssResolverPipeline(cssResolver, htmlPipeline);

      XMLWorker worker = new XMLWorker(pipeline, true);
      XMLParser parser = new XMLParser(worker);

      if (source instanceof InputStream) {
        parser.parse((InputStream) source, cCharset);
      } else if (source instanceof Reader) {
        parser.parse((Reader) source);
      } else {
        StringReader reader = new StringReader((String) source);
        parser.parse(reader);
      }

      document.close();

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

  private final PNGPDFTransformerHelper writerHelper;

  public PDFBoletoWriter(final Rectangle rectangle) {
    bytes = new ByteArrayOutputStream();
    document = new Document(rectangle);

    try {
      writer = PdfWriter.getInstance(document, bytes);

      fonteSimples = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
View Full Code Here

    try {
      Assert.notNull(source, "source");
      Assert.greaterOrEqual(start, 1, "start");

      PdfReader reader = new PdfReader(source);
      Document document = new Document(reader.getPageSizeWithRotation(1));
      InputOutputStream outputStream = new InputOutputStream();
      PdfCopy writer = new PdfCopy(document, outputStream);
      int pageCount = reader.getNumberOfPages();

      Assert.lessOrEqual(start, pageCount, "start");

      int endPage = end;
      if (endPage > pageCount) {
        endPage = pageCount;
      }

      document.open();

      for (int i = start; i <= endPage; i++) {
        PdfImportedPage page = writer.getImportedPage(reader, i);
        writer.addPage(page);
      }

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

      return outputStream.getInputStream();
    } catch (IOException e) {
View Full Code Here

  public InputStream merge(final InputStream[] sources) {
    try {
      Assert.notNull(sources, "sources");
      Assert.notEmpty(sources, "sources");

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

      document.open();

      for (InputStream source : sources) {
        PdfReader reader = new PdfReader(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 outputStream.getInputStream();
    } catch (IOException e) {
      throw new PDFException(e);
View Full Code Here

      PdfReader reader = new PdfReader(source);
      int pageCount = reader.getNumberOfPages();
      List<InputStream> list = new LinkedList<InputStream>();

      Document document = null;
      InputOutputStream outputStream = null;
      PdfCopy writer = null;
      for (int i = 1; i <= pageCount; i++) {
        if ((document == null) || ((i % size) == 0)) {
          if (document != null) {
            document.close();
            writer.close();
            list.add(outputStream.getInputStream());
          }
          document = new Document(reader.getPageSizeWithRotation(1));
          outputStream = new InputOutputStream();
          writer = new PdfCopy(document, outputStream);
        }
        PdfImportedPage page = writer.getImportedPage(reader, i);
        writer.addPage(page);
      }

      if (document != null) {
        document.close();
        writer.close();
        list.add(outputStream.getInputStream());
      }

      reader.close();
View Full Code Here

  //生成新的pdf文档
  public PdfGenerator newPdf(String pdf)
      throws DocumentException, MalformedURLException, IOException {
    // 横版A4
    document = new Document(PageSize.A4.rotate(), 50, 50, 50, 50);
    //
    writer = PdfWriter.getInstance(document, new FileOutputStream(pdf));
    // //1,设置此PDF文件的权限,只有写上的是答应的。这里只答应打印,读取和保存,不答应修改等。
    // int intPermissions =
    // PdfWriter.ALLOW_PRINTING|PdfWriter.ALLOW_SCREENREADERS;
View Full Code Here

    super( tempdir );
  }

  @Override
  protected Document createDocument( File file ) throws Exception {
    Document document = new Document( PageSize.A4, 48.0f, 36.0f, 16.0f, 36.0f );
    PdfWriter.getInstance( document, new FileOutputStream( file ) );
    return document;
  }
View Full Code Here

import java.io.IOException;

public class ImageToPdfConverter {

  public static byte[] convert(byte[] data) throws IOException, DocumentException {
    Document document = new Document();
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    PdfWriter.getInstance(document, byteArrayOutputStream);
    document.open();
    Image image = Image.getInstance(data);
    float maxWidth = document.getPageSize().getWidth() - 75;
    float maxHeight = document.getPageSize().getHeight();

    if (image.getWidth() > maxWidth || image.getHeight() > maxHeight)
      image.scaleToFit(maxWidth, maxHeight);

    document.add(image);
    document.close();
    return byteArrayOutputStream.toByteArray();
  }
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.