Java Code Examples for com.lowagie.text.pdf.PdfWriter#getInstance()

The following examples show how to use com.lowagie.text.pdf.PdfWriter#getInstance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FormSignatureTest.java    From itext2 with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Generates an Acroform with a Signature
 */
@Test
public void main() throws Exception {

	// step 1: creation of a document-object
	Document document = new Document(PageSize.A4);

	// step 2:
	PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("signature.pdf"));

	// step 3: we open the document
	document.open();

	// step 4:
	PdfAcroForm acroForm = writer.getAcroForm();
	document.add(new Paragraph("Hello World"));
	acroForm.addSignature("mysig", 73, 705, 149, 759);

	// step 5: we close the document
	document.close();
}
 
Example 2
Source File: AbsolutePositionsTest.java    From itext2 with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Adds an Image at an absolute position.
 */
@Test
public void main() throws Exception {

	// step 1: creation of a document-object
	Document document = new Document();

	// step 2:
	// we create a writer that listens to the document
	// and directs a PDF-stream to a file

	PdfWriter.getInstance(document, PdfTestBase.getOutputStream("absolutepositions.pdf"));

	// step 3: we open the document
	document.open();

	// step 4: we add content
	Image png = Image.getInstance(PdfTestBase.RESOURCES_DIR + "hitchcock.png");
	png.setAbsolutePosition(171, 250);
	document.add(png);
	png.setAbsolutePosition(342, 500);
	document.add(png);

	// step 5: we close the document
	document.close();
}
 
Example 3
Source File: OpenApplicationTest.java    From itext2 with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Creates a document with Named Actions.
 * 
 * @param args The file to open
 */
public void main(String... args) throws Exception {

	// step 1: creation of a document-object
	Document document = new Document(PageSize.A4, 50, 50, 50, 50);

	// step 2: we create a writer that listens to the document
	PdfWriter.getInstance(document, PdfTestBase.getOutputStream("OpenApplication.pdf"));
	// step 3: we open the document
	document.open();
	// step 4: we add some content
	String application = args[0];
	Paragraph p = new Paragraph(new Chunk("Click to open " + application).setAction(
			new PdfAction(application, null, null, null)));
	document.add(p);

	// step 5: we close the document
	document.close();

}
 
Example 4
Source File: Prd3820IT.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
protected TestPdfLogicalPageDrawable createDrawableForTest( final MasterReport report,
    final LogicalPageBox logicalPageBox ) throws DocumentException {
  Document document = new Document();
  PdfWriter writer = PdfWriter.getInstance( document, new NullOutputStream() );
  writer.setLinearPageMode();
  writer.open();

  document.setPageSize( new com.lowagie.text.Rectangle( 700, 500 ) );
  document.setMargins( 10, 10, 10, 10 );
  document.open();

  PdfOutputProcessorMetaData metaData =
      new PdfOutputProcessorMetaData( new ITextFontStorage( BaseFontModule.getFontRegistry() ) );
  metaData.initialize( report.getConfiguration() );
  final Graphics2D graphics = new PdfGraphics2D( writer.getDirectContent(), 700, 500, metaData );

  TestPdfLogicalPageDrawable pdf =
      new TestPdfLogicalPageDrawable( writer, new LFUMap<ResourceKey, Image>( 10 ), '5', graphics );
  pdf.init( logicalPageBox, metaData, report.getResourceManager(), logicalPageBox.getPageGrid().getPage( 0, 0 ) );
  return pdf;
}
 
Example 5
Source File: Prd5321IT.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
protected TestPdfLogicalPageDrawable createDrawableForTest( final MasterReport report,
    final LogicalPageBox logicalPageBox ) throws DocumentException {
  Document document = new Document();
  PdfWriter writer = PdfWriter.getInstance( document, new NullOutputStream() );
  writer.setLinearPageMode();
  writer.open();

  document.setPageSize( new com.lowagie.text.Rectangle( 700, 500 ) );
  document.setMargins( 10, 10, 10, 10 );
  document.open();

  PdfOutputProcessorMetaData metaData =
      new PdfOutputProcessorMetaData( new ITextFontStorage( BaseFontModule.getFontRegistry() ) );
  metaData.initialize( report.getConfiguration() );
  final Graphics2D graphics = new PdfGraphics2D( writer.getDirectContent(), 700, 500, metaData );

  TestPdfLogicalPageDrawable pdf =
      new TestPdfLogicalPageDrawable( writer, new LFUMap<ResourceKey, Image>( 10 ), '5', graphics );
  pdf.init( logicalPageBox, metaData, report.getResourceManager(), logicalPageBox.getPageGrid().getPage( 0, 0 ) );
  return pdf;
}
 
Example 6
Source File: TrueTypeTest.java    From itext2 with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Using a True Type Font.
 */
@Test
public void main() throws Exception {


	// step 1: creation of a document-object
	Document document = new Document();

	// step 2:
	// we create a writer that listens to the document
	// and directs a PDF-stream to a file
	PdfWriter.getInstance(document,PdfTestBase.getOutputStream("truetype.pdf"));

	// step 3: we open the document
	document.open();

	String f = new File(PdfTestBase.RESOURCES_DIR + "liberation-fonts-ttf/LiberationMono-Regular.ttf").getAbsolutePath();
	// step 4: we add content to the document
	BaseFont bfComic = BaseFont.createFont(f, BaseFont.CP1252,	BaseFont.NOT_EMBEDDED);
	Font font = new Font(bfComic, 12);
	String text1 = "This is the quite popular Liberation Mono.";
	document.add(new Paragraph(text1, font));

	// step 5: we close the document
	document.close();
}
 
Example 7
Source File: NewPageTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Creates a PDF document with different pages.
 */
@Test
public void main() throws Exception {

	// step 1: creation of a document-object
	Document document = new Document();

	// step 2:
	// we create a writer that listens to the document
	// and directs a PDF-stream to a file

	PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("NewPage.pdf"));

	// step 3: we open the document
	document.open();

	// step 4:
	document.add(new Paragraph("This is the first page."));
	document.newPage();
	document.add(new Paragraph("This is a new page"));
	document.newPage();
	document.newPage();
	document.add(new Paragraph(
			"We invoked new page twice, yet there was no blank page added. Between the second page and this one. This is normal behaviour."));
	document.newPage();
	writer.setPageEmpty(false);
	document.newPage();
	document.add(new Paragraph("We told the writer the page wasn't empty."));
	document.newPage();
	document.add(Chunk.NEWLINE);
	document.newPage();
	document.add(new Paragraph("You can also add something invisible if you want a blank page."));
	document.add(Chunk.NEXTPAGE);
	document.add(new Paragraph("Using Chunk.NEXTPAGE also jumps to the next page"));

	// step 5: we close the document
	document.close();
}
 
Example 8
Source File: StandardType1FontsTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Generates a PDF file with the 14 standard Type 1 Fonts
 * 
 */
@Test
public void main() throws Exception {

	// step 1: creation of a document-object
	Document document = new Document();
	// step 2:
	// we create a writer that listens to the document
	PdfWriter.getInstance(document, PdfTestBase.getOutputStream("StandardType1Fonts.pdf"));

	// step 3: we open the document
	document.open();
	// step 4:

	// the 14 standard fonts in PDF: do not use this Font constructor!
	// this is for demonstration purposes only, use FontFactory!
	Font[] fonts = new Font[14];
	fonts[0] = new Font(Font.COURIER, Font.DEFAULTSIZE, Font.NORMAL);
	fonts[1] = new Font(Font.COURIER, Font.DEFAULTSIZE, Font.ITALIC);
	fonts[2] = new Font(Font.COURIER, Font.DEFAULTSIZE, Font.BOLD);
	fonts[3] = new Font(Font.COURIER, Font.DEFAULTSIZE, Font.BOLD | Font.ITALIC);
	fonts[4] = new Font(Font.HELVETICA, Font.DEFAULTSIZE, Font.NORMAL);
	fonts[5] = new Font(Font.HELVETICA, Font.DEFAULTSIZE, Font.ITALIC);
	fonts[6] = new Font(Font.HELVETICA, Font.DEFAULTSIZE, Font.BOLD);
	fonts[7] = new Font(Font.HELVETICA, Font.DEFAULTSIZE, Font.BOLDITALIC);
	fonts[8] = new Font(Font.TIMES_ROMAN, Font.DEFAULTSIZE, Font.NORMAL);
	fonts[9] = new Font(Font.TIMES_ROMAN, Font.DEFAULTSIZE, Font.ITALIC);
	fonts[10] = new Font(Font.TIMES_ROMAN, Font.DEFAULTSIZE, Font.BOLD);
	fonts[11] = new Font(Font.TIMES_ROMAN, Font.DEFAULTSIZE, Font.BOLDITALIC);
	fonts[12] = new Font(Font.SYMBOL);
	fonts[13] = new Font(Font.ZAPFDINGBATS);
	// add the content
	for (int i = 0; i < 14; i++) {
		document.add(new Paragraph("quick brown fox jumps over the lazy dog", fonts[i]));
	}

	// step 5: we close the document
	document.close();
}
 
Example 9
Source File: ScalingTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Scaling an image.
 */
@Test
public void main() throws Exception {

	// step 1: creation of a document-object
	Document document = new Document();

	// step 2:
	// we create a writer that listens to the document
	// and directs a PDF-stream to a file

	PdfWriter.getInstance(document, PdfTestBase.getOutputStream("scaling.pdf"));

	// step 3: we open the document
	document.open();

	// step 4: we add content
	Image jpg1 = Image.getInstance(PdfTestBase.RESOURCES_DIR + "otsoe.jpg");
	jpg1.scaleAbsolute(160, 120);
	document.add(new Paragraph("scaleAbsolute(160, 120)"));
	document.add(jpg1);
	Image jpg2 = Image.getInstance(PdfTestBase.RESOURCES_DIR + "otsoe.jpg");
	jpg2.scalePercent(50);
	document.add(new Paragraph("scalePercent(50)"));
	document.add(jpg2);
	Image jpg3 = Image.getInstance(PdfTestBase.RESOURCES_DIR + "otsoe.jpg");
	jpg3.scaleAbsolute(320, 120);
	document.add(new Paragraph("scaleAbsolute(320, 120)"));
	document.add(jpg3);
	Image jpg4 = Image.getInstance(PdfTestBase.RESOURCES_DIR + "otsoe.jpg");
	jpg4.scalePercent(100, 50);
	document.add(new Paragraph("scalePercent(100, 50)"));
	document.add(jpg4);

	// step 5: we close the document
	document.close();
}
 
Example 10
Source File: PdfEventHandler.java    From unitime with Apache License 2.0 5 votes vote down vote up
/**
   * Initialize Pdf footer
   * @param document
   * @param outputStream
   * @return PdfWriter
   */
  public static PdfWriter initFooter(Document document, OutputStream outputStream) 
  		throws DocumentException, IOException {
  	
PdfWriter iWriter = PdfWriter.getInstance(document, outputStream);
iWriter.setPageEvent(new PdfEventHandler());
  	
return iWriter;
  }
 
Example 11
Source File: PageLabelsTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Demonstrates some PageLabel functionality.
 * 
 */
@Test
public void main() throws Exception {

	// step 1: creation of a document-object
	Document document = new Document();
	// step 2:
	PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("PageLabels.pdf"));
	// step 3:
	writer.setViewerPreferences(PdfWriter.PageModeUseThumbs);
	document.open();
	// step 4:
	// we add some content
	for (int k = 1; k <= 10; ++k) {
		document.add(new Paragraph(
				"This document has the logical page numbers: i,ii,iii,iv,1,2,3,A-8,A-9,A-10\nReal page " + k));
		document.newPage();
	}
	PdfPageLabels pageLabels = new PdfPageLabels();
	pageLabels.addPageLabel(1, PdfPageLabels.LOWERCASE_ROMAN_NUMERALS);
	pageLabels.addPageLabel(5, PdfPageLabels.DECIMAL_ARABIC_NUMERALS);
	pageLabels.addPageLabel(8, PdfPageLabels.DECIMAL_ARABIC_NUMERALS, "A-", 8);
	writer.setPageLabels(pageLabels);

	// step 5: we close the document
	document.close();
}
 
Example 12
Source File: MultiColumnSimpleTest.java    From itext2 with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * An example using MultiColumnText with irregular columns.
 */
@Test
public void main() throws Exception {
	Document document = new Document();
	OutputStream out = PdfTestBase.getOutputStream("multicolumnsimple.pdf");
	PdfWriter.getInstance(document, out);
	document.open();

	MultiColumnText mct = new MultiColumnText();

	// set up 3 even columns with 10pt space between
	mct.addRegularColumns(document.left(), document.right(), 10f, 3);

	// Write some iText poems
	for (int i = 0; i < 30; i++) {
		mct.addElement(new Paragraph(String.valueOf(i + 1)));
		mct.addElement(newPara(randomWord(noun), Element.ALIGN_CENTER, Font.BOLDITALIC));
		for (int j = 0; j < 4; j++) {
			mct.addElement(newPara(poemLine(), Element.ALIGN_LEFT, Font.NORMAL));
		}
		mct.addElement(newPara(randomWord(adverb), Element.ALIGN_LEFT, Font.NORMAL));
		mct.addElement(newPara("\n\n", Element.ALIGN_LEFT, Font.NORMAL));
	}
	document.add(mct);
	document.close();

}
 
Example 13
Source File: PurchaseOrderQuotePdf.java    From kfs with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Invokes the createPOQuotePDF method to create a purchase order quote pdf document and saves it into a file
 * which name and location are specified in the pdfParameters.
 *
 * @param po                         The PurchaseOrderDocument to be used to generate the pdf.
 * @param poqv                       The PurchaseOrderVendorQuote to be used to generate the pdf.
 * @param pdfFileLocation            The location to save the pdf file.
 * @param pdfFilename                The name for the pdf file.
 * @param campusName                 The campus name to be used to generate the pdf.
 * @param contractManagerCampusCode  The contract manager campus code to be used to generate the pdf.
 * @param logoImage                  The logo image file name to be used to generate the pdf.
 * @param environment                The current environment used (e.g. DEV if it is a development environment).
 */
public void savePOQuotePDF(PurchaseOrderDocument po, PurchaseOrderVendorQuote poqv,PurchaseOrderParameters transmitParameters , String environment) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("savePOQuotePDF() started for po number " + po.getPurapDocumentIdentifier());
    }

    try {
        PurchaseOrderTransmitParameters orderTransmitParameters = (PurchaseOrderTransmitParameters)transmitParameters;
        CampusParameter deliveryCampus = orderTransmitParameters.getCampusParameter();
            if (deliveryCampus == null) {
                throw new RuntimeException(" delivery campus is null");
            }
            String campusName = deliveryCampus.getCampus().getName();
            if (campusName == null) {

                throw new RuntimeException("Campus Information is missing - campusName: " + campusName);
            }
        Document doc = this.getDocument(9, 9, 70, 36);
        PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(orderTransmitParameters.getPdfFileLocation() + orderTransmitParameters.getPdfFileName()));
        this.createPOQuotePdf(po, poqv,campusName, orderTransmitParameters.getContractManagerCampusCode(), orderTransmitParameters.getLogoImage(), doc, writer, environment);
    }
    catch (DocumentException de) {
        LOG.error(de.getMessage(), de);
        throw new PurError("Document Exception when trying to save a Purchase Order Quote PDF", de);
    }
    catch (FileNotFoundException f) {
        LOG.error(f.getMessage(), f);
        throw new PurError("FileNotFound Exception when trying to save a Purchase Order Quote PDF", f);
    }
}
 
Example 14
Source File: ComplexTextTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Adding text at absolute positions.
 * 
 * @param args
 *            no arguments needed
 */
@Test
public void main() throws Exception {


	// step 1: creation of a document-object
	Document document = new Document();

	// step 2: creation of the writer
	PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("complextext.pdf"));

	// step 3: we open the document
	document.open();

	// step 4: we grab the ContentByte and do some stuff with it
	PdfContentByte cb = writer.getDirectContent();

	// first we draw some lines to be able to visualize the text alignment
	// functions
	cb.setLineWidth(0f);
	cb.moveTo(250, 500);
	cb.lineTo(250, 800);
	cb.moveTo(50, 700);
	cb.lineTo(400, 700);
	cb.moveTo(50, 650);
	cb.lineTo(400, 650);
	cb.moveTo(50, 600);
	cb.lineTo(400, 600);
	cb.stroke();

	File font = new File (PdfTestBase.RESOURCES_DIR + "/liberation-fonts-ttf/LiberationSans-Regular.ttf");
	// we construct a font
	BaseFont bf = BaseFont.createFont(font.getAbsolutePath(), BaseFont.IDENTITY_H, true);
	Font ft = new Font(bf, 12);
	// This is the text:
	String text = "\u0623\u0648\u0631\u0648\u0628\u0627, \u0628\u0631\u0645\u062c\u064a\u0627\u062a \u0627\u0644\u062d\u0627\u0633\u0648\u0628 + \u0627\u0646\u062a\u0631\u0646\u064a\u062a :";
	Phrase center = new Phrase(text + " Center", ft);
	ColumnText
			.showTextAligned(cb, PdfContentByte.ALIGN_CENTER, center, 250, 700, 0, PdfWriter.RUN_DIRECTION_RTL, 0);
	ColumnText.showTextAligned(cb, PdfContentByte.ALIGN_RIGHT, new Phrase(text + " Right", ft), 250, 650, 20,
			PdfWriter.RUN_DIRECTION_RTL, 0);
	ColumnText.showTextAligned(cb, PdfContentByte.ALIGN_LEFT, new Phrase("Some text Left aligned", ft), 250, 600,
			20);
	float size = ColumnText.getWidth(center, PdfWriter.RUN_DIRECTION_RTL, 0);
	cb.setRGBColorStroke(255, 0, 0);
	cb.rectangle(250 - size / 2, 690, size, 30);
	cb.stroke();

	// step 5: we close the document
	document.close();
}
 
Example 15
Source File: AutomaticTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Automatic grouping and nesting.
 */
@Test
public void main() throws Exception {
	// step 1
	Document document = new Document(PageSize.A4, 50, 50, 50, 50);
	// step 2
	PdfWriter writer = PdfWriter.getInstance(document,
			PdfTestBase.getOutputStream("automatic.pdf"));
	writer.setPdfVersion(PdfWriter.VERSION_1_5);
	writer.setViewerPreferences(PdfWriter.PageModeUseOC);
	// step 3
	document.open();
	// step 4
	PdfContentByte cb = writer.getDirectContent();
	Phrase explanation = new Phrase("Automatic layer grouping and nesting",
			new Font(Font.HELVETICA, 20, Font.BOLD, Color.red));
	ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, explanation, 50,
			650, 0);
	PdfLayer l12 = new PdfLayer("Layer nesting", writer);
	PdfLayer l1 = new PdfLayer("Layer 1", writer);
	PdfLayer l2 = new PdfLayer("Layer 2", writer);
	PdfLayer title = PdfLayer.createTitle("Layer grouping", writer);
	PdfLayer l3 = new PdfLayer("Layer 3", writer);
	PdfLayer l4 = new PdfLayer("Layer 4", writer);
	l12.addChild(l1);
	l12.addChild(l2);
	title.addChild(l3);
	title.addChild(l4);
	Phrase p1 = new Phrase("Text in layer 1");
	Phrase p2 = new Phrase("Text in layer 2");
	Phrase p3 = new Phrase("Text in layer 3");
	Phrase p4 = new Phrase("Text in layer 4");
	cb.beginLayer(l1);
	ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p1, 50, 600, 0);
	cb.endLayer();
	cb.beginLayer(l2);
	ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p2, 50, 550, 0);
	cb.endLayer();
	cb.beginLayer(l3);
	ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p3, 50, 500, 0);
	cb.endLayer();
	cb.beginLayer(l4);
	ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p4, 50, 450, 0);
	cb.endLayer();
	cb.sanityCheck();

	// step 5
	document.close();
}
 
Example 16
Source File: PdfDocumentWriter.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void open() throws DocumentException {
  this.document = new Document();
  // pageSize, marginLeft, marginRight, marginTop, marginBottom));

  writer = PdfWriter.getInstance( document, out );
  writer.setLinearPageMode();

  version = getVersion();
  writer.setPdfVersion( version );
  writer.setViewerPreferences( getViewerPreferences() );

  final String encrypt =
      config.getConfigProperty( "org.pentaho.reporting.engine.classic.core.modules.output.pageable.pdf.Encryption" );

  if ( encrypt != null ) {
    if ( encrypt.equals( PdfPageableModule.SECURITY_ENCRYPTION_128BIT ) == true
        || encrypt.equals( PdfPageableModule.SECURITY_ENCRYPTION_40BIT ) == true ) {
      final String userpassword =
          config
              .getConfigProperty( "org.pentaho.reporting.engine.classic.core.modules.output.pageable.pdf.UserPassword" );
      final String ownerpassword =
          config
              .getConfigProperty( "org.pentaho.reporting.engine.classic.core.modules.output.pageable.pdf.OwnerPassword" );
      // Log.debug ("UserPassword: " + userpassword + " - OwnerPassword: " + ownerpassword);
      final byte[] userpasswordbytes = DocWriter.getISOBytes( userpassword );
      byte[] ownerpasswordbytes = DocWriter.getISOBytes( ownerpassword );
      if ( ownerpasswordbytes == null ) {
        ownerpasswordbytes = PdfDocumentWriter.PDF_PASSWORD_PAD;
      }
      final int encryptionType;
      if ( encrypt.equals( PdfPageableModule.SECURITY_ENCRYPTION_128BIT ) ) {
        encryptionType = PdfWriter.STANDARD_ENCRYPTION_128;
      } else {
        encryptionType = PdfWriter.STANDARD_ENCRYPTION_40;
      }
      writer.setEncryption( userpasswordbytes, ownerpasswordbytes, getPermissions(), encryptionType );
    }
  }

  /**
   * MetaData can be set when the writer is registered to the document.
   */
  final String title =
      config.getConfigProperty( "org.pentaho.reporting.engine.classic.core.modules.output.pageable.pdf.Title", config
          .getConfigProperty( "org.pentaho.reporting.engine.classic.core.metadata.Title" ) );
  final String subject =
      config.getConfigProperty( "org.pentaho.reporting.engine.classic.core.modules.output.pageable.pdf.Description",
          config.getConfigProperty( "org.pentaho.reporting.engine.classic.core.metadata.Description" ) );
  final String author =
      config.getConfigProperty( "org.pentaho.reporting.engine.classic.core.modules.output.pageable.pdf.Author",
          config.getConfigProperty( "org.pentaho.reporting.engine.classic.core.metadata.Author" ) );
  final String keyWords =
      config.getConfigProperty( "org.pentaho.reporting.engine.classic.core.modules.output.pageable.pdf.Keywords",
          config.getConfigProperty( "org.pentaho.reporting.engine.classic.core.metadata.Keywords" ) );

  if ( title != null ) {
    document.addTitle( title );
  }
  if ( author != null ) {
    document.addAuthor( author );
  }
  if ( keyWords != null ) {
    document.addKeywords( keyWords );
  }
  if ( keyWords != null ) {
    document.addSubject( subject );
  }

  document.addCreator( PdfDocumentWriter.CREATOR );
  document.addCreationDate();

  // getDocument().open();
  awaitOpenDocument = true;
}
 
Example 17
Source File: PaperightPdfConverter.java    From website with GNU Affero General Public License v3.0 4 votes vote down vote up
private String createA5(String originalPdfFile) throws IOException, DocumentException {
	logger.debug("Started create A5 file from " + originalPdfFile);
	String filename = FilenameUtils.getBaseName(originalPdfFile) + "_a5." + FilenameUtils.getExtension(originalPdfFile);
	String tempFilename =  FilenameUtils.concat(System.getProperty("java.io.tmpdir"), filename + ".tmp");
	filename = FilenameUtils.concat(getPdfFileFolder(), filename);
	File tempFile = new File(tempFilename);
	//float scale = 0.72906403940886699507389162561576f;
	PdfReader reader = new PdfReader(originalPdfFile);
	
	//new RectangleReadOnly(425.16f, 606.96f)
	Document doc = new Document(PageSize.A5, 0, 0, 0, 0);
	PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(tempFile));
	doc.open();
	for (int i = 1; i <= reader.getNumberOfPages(); i++) {
		doc.newPage();
		PdfContentByte cb = writer.getDirectContent();
		PdfImportedPage page = writer.getImportedPage(reader, i); // page #1
		
		float documentWidth = doc.getPageSize().getWidth();
		float documentHeight = doc.getPageSize().getHeight();
		if (i > 1) {
			documentHeight = documentHeight - 65f;
		}
		//float documentHeight = doc.getPageSize().getHeight() - 65f;

		float pageWidth = page.getWidth();
		float pageHeight = page.getHeight();
		
		float widthScale = documentWidth / pageWidth;
		float heightScale = documentHeight / pageHeight;
		float scale = Math.min(widthScale, heightScale);
		
		//float offsetX = 50f;
		float offsetX = (documentWidth - (pageWidth * scale)) / 2;
		float offsetY = 0f;
		if (i > 1) {
			offsetY = 65f;
		}
		//float offsetY = ((documentHeight) - (pageHeight * scale)) / 2;
		
		cb.addTemplate(page, scale, 0, 0, scale, offsetX, offsetY);
		//cb.addTemplate(page, scale, 0, 0, scale, 50f, 100f);
	}
	doc.close();
	//FileUtils.moveFile(tempFile, new File(filename));
	overlayLicenceArea(tempFilename, filename, PageLayout.A5);
	logger.debug("Deleting temporary file " + tempFile.getAbsolutePath());
	FileUtils.deleteQuietly(tempFile);
	logger.debug("Completed create A5 file from " + originalPdfFile);
	return filename;
}
 
Example 18
Source File: FormTextFieldTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Generates an Acroform with a TextField
 */
@Test
public void main() throws Exception {

	// step 1: creation of a document-object
	Document document = new Document(PageSize.A4);

	// step 2:
	PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("textfield.pdf"));

	// step 3: we open the document
	document.open();

	// step 4:
	BaseFont helv = BaseFont.createFont("Helvetica", "winansi", false);
	PdfContentByte cb = writer.getDirectContent();
	cb.moveTo(0, 0);
	String text = "Some start text";
	float fontSize = 12;
	Color textColor = new GrayColor(0f);
	PdfFormField field = PdfFormField.createTextField(writer, false, false, 0);
	field.setWidget(new Rectangle(171, 750, 342, 769), PdfAnnotation.HIGHLIGHT_INVERT);
	field.setFlags(PdfAnnotation.FLAGS_PRINT);
	field.setFieldName("ATextField");
	field.setValueAsString(text);
	field.setDefaultValueAsString(text);
	field.setBorderStyle(new PdfBorderDictionary(2, PdfBorderDictionary.STYLE_SOLID));
	field.setPage();
	PdfAppearance tp = cb.createAppearance(171, 19);
	PdfAppearance da = (PdfAppearance) tp.getDuplicate();
	da.setFontAndSize(helv, fontSize);
	da.setColorFill(textColor);
	field.setDefaultAppearanceString(da);
	tp.beginVariableText();
	tp.saveState();
	tp.rectangle(2, 2, 167, 15);
	tp.clip();
	tp.newPath();
	tp.beginText();
	tp.setFontAndSize(helv, fontSize);
	tp.setColorFill(textColor);
	tp.setTextMatrix(4, 5);
	tp.showText(text);
	tp.endText();
	tp.restoreState();
	tp.endVariableText();
	field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, tp);
	writer.addAnnotation(field);

	// step 5: we close the document
	document.close();
}
 
Example 19
Source File: LayersTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
    * Layer radio group and zoom.
    */
@Test
public void main() throws Exception {
       	// step 1
           Document document = new Document(PageSize.A4, 50, 50, 50, 50);
           // step 2
           PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream( "layers.pdf"));
           writer.setPdfVersion(PdfWriter.VERSION_1_5);
           writer.setViewerPreferences(PdfWriter.PageModeUseOC);
           // step 3
           document.open();
           // step 4
           PdfContentByte cb = writer.getDirectContent();
           Phrase explanation = new Phrase("Layer radio group and zoom", new Font(Font.HELVETICA, 20, Font.BOLD, Color.red));
           ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, explanation, 50, 650, 0);
           PdfLayer title = PdfLayer.createTitle("Layer radio group", writer);
           PdfLayer l1 = new PdfLayer("Layer 1", writer);
           PdfLayer l2 = new PdfLayer("Layer 2", writer);
           PdfLayer l3 = new PdfLayer("Layer 3", writer);
           PdfLayer l4 = new PdfLayer("Layer 4", writer);
           title.addChild(l1);
           title.addChild(l2);
           title.addChild(l3);
           l4.setZoom(2, -1);
           l4.setOnPanel(false);
           l4.setPrint("Print", true);
           l2.setOn(false);
           l3.setOn(false);
           ArrayList<PdfLayer> radio = new ArrayList<PdfLayer>();
           radio.add(l1);
           radio.add(l2);
           radio.add(l3);
           writer.addOCGRadioGroup(radio);
           Phrase p1 = new Phrase("Text in layer 1");
           Phrase p2 = new Phrase("Text in layer 2");
           Phrase p3 = new Phrase("Text in layer 3");
           Phrase p4 = new Phrase("Text in layer 4");
           cb.beginLayer(l1);
           ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p1, 50, 600, 0);
           cb.endLayer();
           cb.beginLayer(l2);
           ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p2, 50, 550, 0);
           cb.endLayer();
           cb.beginLayer(l3);
           ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p3, 50, 500, 0);
           cb.endLayer();
           cb.beginLayer(l4);
           ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, p4, 50, 450, 0);
           cb.endLayer();
           ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("<< Zoom here!", new Font(Font.COURIER, 12, Font.NORMAL, Color.blue)), 150, 450, 0);
           // step 5
           document.close();

   }
 
Example 20
Source File: ChineseJapaneseKoreanTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Using CJK fonts
 * @param args no arguments needed
 */
@Test

public void main() throws Exception {
       
       // step 1: creation of a document-object
       Document document = new Document();
           
           // step 2: creation of the writer
           PdfWriter.getInstance(document, PdfTestBase.getOutputStream("cjk.pdf"));
           
           // step 3: we open the document
           document.open();
           String chinese = "\u53d6\u6e96\u53d7\u4fdd\u4eba\u5728\u6211\u56fd\u7ecf\u6d4e\u7ed3\u6784"
           + "\u8fdb\u884c\u6218\u7565\u6027\u8c03\u6574\u7684"
           + "\u80CC\u666F\u4e0B\uff0c\u4FE1\u606f\u4ea7\u4e1a"
           + "\u5c06\u6210\u4E3A\u62c9\u52A8\u7ecf\u6d4e\u589e"
           + "\u957f\u7684\u65b0\u52A8\u529b\uff0c\u800c\u4f5c"
           + "\u4E3A\u4FE1\u606f\u6280\u672f\u6838\u5fc3\u4e4b"
           + "\u4e00\u7684\u8f6f\u4ef6\u4ea7\u4e1a\u5fc5\u7136"
           + "\u6210\u4E3A\u4e4a\u540e\u56fd\u5bb6\u4ea7\u4e1a"
           + "\u54d1\u5c55\u7684\u6218\u7565\u91cd\u70b9\uff0c"
           + "\u6211\u56fd\u7684\u8f6f\u4ef6\u4ea7\u4e1a\u5c06"
           + "\u4f1a\u5f97\u5230\u98de\u901f\u7684\u54d1\u5c55"
           + "\u3002\u540c\u65f6\u4F34\u968f\u7740\u6211\u56fd"
           + "\u52a0\u5165\u4e16\u8d38\u7ec4\u7ec7\uff0c\u8de8"
           + "\u56fd\u4f01\u4e1a\u4e5f\u5c06\u5927\u89c4\uc4a3"
           + "\u8fdb\u9a7b\u4e2d\u56fd\u8f6f\u4ef6\ucad0\u573a"
           + "\u3002\u56e0\u6b64\uff0c\u9ad8\u7d20\u8d28\u5f0c"
           + "\u4ef6\u4eba\u624d\u7684\u7ade\u4e89\ucac6\u5fc5"
           + "\uc8d5\uc7f7\ubca4\uc1d2\u3002\u800c\u4e4b\u6b64"
           + "\u7678\u5bf9\u5e94\u7684\u662f\u76ee\u524d\u6211"
           + "\u56fd\u5bf9\u5f0c\u4ef6\u4eba\u624d\u57f9\u517b"
           + "\u7684\u529b\u5ea6\u4e0d\u591f\uff0c\u6240\u80fd"
           + "\u63d0\u4f9b\u4e13\u4e1a\u4eba\u624d\u8fdc\u8fdc"
           + "\u6ee1\u8db3\u4e0d\u4e86\u5e02\u573a\u7684\u9700"
           + "\u6c42\u3002\u56e0\u6b64\u4ee5\u5e02\u573a\u9700"
           + "\u6c42\u4E3A\u7740\u773c\u70b9\uff0c\u6539\u9769"
           + "\u5f0c\u4ef6\u4eba\u624d\u57f9\u517b\u7684\u6a21"
           + "\u5f0f\uff0c\u52a0\u5927\u5f0c\u4ef6\u4eba\u624d"
           + "\u57f9\u517b\u7684\u529b\u5ea6\uff0c\u5b9e\u73b0"
           + "\u6211\u56fd\u5f0c\u4ef6\u4eba\u624d\u57f9\u517b"
           + "\u7684\u5d88\u8d8a\u5f0f\u54d1\u5c55\uff0c\u5df2"
           + "\u7ecf\u6210\u4E3A\u5f53\u524d\u6c0a\u7b49\u6559"
           + "\u80b2\u6539\u9769\u4e0e\u54d1\u5c55\u7684\u4e00"
           + "\u9879\u6781\u5176\u91cd\u8981\u800c\u4e14\u7678"
           + "\u5f53\u7d27\u8feb\u7684\u4efb\u52a1\u3002";
           
           // step 4: we add content to the document
           BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
           Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);
           Paragraph p = new Paragraph(chinese, FontChinese);
           document.add(p);
   
       
       // step 5: we close the document
       document.close();
}