com.itextpdf.text.Paragraph Java Examples

The following examples show how to use com.itextpdf.text.Paragraph. 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: DynamicFooter.java    From testarea-itext5 with GNU Affero General Public License v3.0 11 votes vote down vote up
public PdfPTable generateFooter() {
    try {
        BaseFont baseFont = BaseFont.createFont(/*"resources/ARIAL.TTF"*/ "c:/Windows/Fonts/arial.ttf", BaseFont.IDENTITY_H, true);
        footerTable = new PdfPTable(1);
        footerTable.setTotalWidth(440);
        footerTable.setLockedWidth(true);

        Font pageNumberFont = new Font(baseFont, 9, Font.BOLD);
        Paragraph pageNumberP = new Paragraph(titleIndex+"-"+ pageNumber, pageNumberFont);
        PdfPCell pageNumberCell = new PdfPCell(pageNumberP);
        pageNumberCell.setBorder(0);
        pageNumberCell.setPaddingTop(20);
        footerTable.addCell(pageNumberCell);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return footerTable;
}
 
Example #2
Source File: PDFMigrationReportWriter.java    From bonita-studio with GNU General Public License v2.0 7 votes vote down vote up
private void createLegend(Paragraph mainPara) throws BadElementException,
		MalformedURLException, IOException {
	mainPara.add(new Chunk("      ",legendFont));
	Image im = getImageForStatus(IStatus.OK);
	mainPara.add(new Chunk(im, 0, 0, false));
	mainPara.add(new Chunk(" ",legendFont));
	mainPara.add(new Chunk(Messages.noActionRequired,legendFont));
	mainPara.add(new Chunk("   ",legendFont));
	im = getImageForStatus(IStatus.WARNING);
	mainPara.add(new Chunk(im, 0, 0, false));
	mainPara.add(new Chunk(" ",legendFont));
	mainPara.add(new Chunk(Messages.reviewRequired,legendFont));
	mainPara.add(new Chunk("   ",legendFont));
	im = getImageForStatus(IStatus.ERROR);
	mainPara.add(new Chunk(im, 0, 0, false));
	mainPara.add(new Chunk(" ",legendFont));
	mainPara.add(new Chunk(Messages.actionRequired,legendFont));
}
 
Example #3
Source File: PageHeaderFooterEvent.java    From ureport with Apache License 2.0 7 votes vote down vote up
private PdfPCell buildPdfPCell(HeaderFooter phf,String text,int type){
	PdfPCell cell=new PdfPCell();
	cell.setPadding(0);
	cell.setBorder(Rectangle.NO_BORDER);
	Font font=FontBuilder.getFont(phf.getFontFamily(), phf.getFontSize(), phf.isBold(), phf.isItalic(),phf.isUnderline());
	String fontColor=phf.getForecolor();
	if(StringUtils.isNotEmpty(fontColor)){
		String[] color=fontColor.split(",");
		font.setColor(Integer.valueOf(color[0]), Integer.valueOf(color[1]), Integer.valueOf(color[2]));			
	}
	Paragraph graph=new Paragraph(text,font);
	cell.setPhrase(graph);
	switch(type){
	case 1:
		cell.setHorizontalAlignment(Element.ALIGN_LEFT);
		break;
	case 2:
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		break;
	case 3:
		cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
		break;
	}
	cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
	return cell;
}
 
Example #4
Source File: AbstractPdfReportBuilder.java    From bdf3 with Apache License 2.0 6 votes vote down vote up
protected Paragraph createReportTitle(ReportTitle reportTitle) {
	Paragraph paragraph = new Paragraph();
	paragraph.setAlignment(Element.ALIGN_CENTER);
	if (reportTitle != null && reportTitle.isShowTitle()) {
		TextChunk titleChunk = new TextChunk();
		titleChunk.setText(reportTitle.getTitle());
		titleChunk.setFontSize(reportTitle.getStyle().getFontSize());
		titleChunk.setFontColor(reportTitle.getStyle().getFontColor());
		paragraph.add(createChunk(titleChunk));
		paragraph.add(Chunk.NEWLINE);
		paragraph.add(Chunk.NEWLINE);
	}
	return paragraph;
}
 
Example #5
Source File: ImportPageWithoutFreeSpace.java    From testarea-itext5 with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * This method creates a PDF with a single styled paragraph.
 */
static byte[] createSimpleTextPdf() throws DocumentException
{
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    Document document = new Document();
    PdfWriter.getInstance(document, baos);
    document.open();

    Paragraph paragraph = new Paragraph();
    paragraph.add(new Phrase("Beware: ", new Font(FontFamily.HELVETICA, 12, Font.BOLDITALIC)));
    paragraph.add(new Phrase("The implementation of ", new Font(FontFamily.HELVETICA, 12, Font.ITALIC)));
    paragraph.add(new Phrase("MarginFinder", new Font(FontFamily.COURIER, 12, Font.ITALIC)));
    paragraph.add(new Phrase(" is far from optimal. It is not even correct as it includes all curve control points which is too much. Furthermore it ignores stuff like line width or wedge types. It actually merely is a proof-of-concept.", new Font(FontFamily.HELVETICA, 12, Font.ITALIC)));
    document.add(paragraph);

    document.close();

    return baos.toByteArray();
}
 
Example #6
Source File: VeryDenseMerging.java    From testarea-itext5 with GNU Affero General Public License v3.0 6 votes vote down vote up
static byte[] createSimpleTextPdf(String paragraphFormat, int paragraphCount) throws DocumentException
{
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    Document document = new Document();
    PdfWriter.getInstance(document, baos);
    document.open();
    for (int i = 0; i < paragraphCount; i++)
    {
        Paragraph paragraph = new Paragraph();
        paragraph.add(String.format(paragraphFormat, i));
        document.add(paragraph);
    }
    document.close();

    return baos.toByteArray();
}
 
Example #7
Source File: DenseMerging.java    From testarea-itext5 with GNU Affero General Public License v3.0 6 votes vote down vote up
static byte[] createSimpleTextPdf(String paragraphFormat, int paragraphCount) throws DocumentException
{
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    Document document = new Document();
    PdfWriter.getInstance(document, baos);
    document.open();
    for (int i = 0; i < paragraphCount; i++)
    {
        Paragraph paragraph = new Paragraph();
        paragraph.add(String.format(paragraphFormat, i));
        document.add(paragraph);
    }
    document.close();

    return baos.toByteArray();
}
 
Example #8
Source File: CreateAndAppendDoc.java    From testarea-itext5 with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * <a href="http://stackoverflow.com/questions/29001852/how-to-create-a-pdf-and-you-then-merge-another-pdf-to-the-same-document-using-it">
 * how to create a PDF and you then merge another pdf to the same document using itext
 * </a>
 * <p>
 * Testing the OP's method with <code>paginate</code> set to <code>true</code>
 * </p>
 */
@Test
public void testAppendPDFsPaginate() throws IOException, DocumentException
{
    try (
            InputStream testA4Stream = getClass().getResourceAsStream("testA4.pdf");
            InputStream fromStream = getClass().getResourceAsStream("from.pdf");
            InputStream prefaceStream = getClass().getResourceAsStream("preface.pdf");
            InputStream type3Stream = getClass().getResourceAsStream("Test_Type3_Problem.pdf");
            FileOutputStream output = new FileOutputStream(new File(RESULT_FOLDER, "appendPdfsPaginate.pdf"));
        )
    {
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, output);
        document.open();
        document.add(new Paragraph("Some content to start with"));
        appendPDFs(Arrays.asList(testA4Stream, fromStream, prefaceStream, type3Stream), writer, document, null, true);
        document.close();
    }
}
 
Example #9
Source File: CreateAndAppendDoc.java    From testarea-itext5 with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * <a href="http://stackoverflow.com/questions/29001852/how-to-create-a-pdf-and-you-then-merge-another-pdf-to-the-same-document-using-it">
 * how to create a PDF and you then merge another pdf to the same document using itext
 * </a>
 * <p>
 * Testing the OP's method with <code>paginate</code> set to <code>false</code>
 * </p>
 */
@Test
public void testAppendPDFs() throws IOException, DocumentException
{
    try (
            InputStream testA4Stream = getClass().getResourceAsStream("testA4.pdf");
            InputStream fromStream = getClass().getResourceAsStream("from.pdf");
            InputStream prefaceStream = getClass().getResourceAsStream("preface.pdf");
            InputStream type3Stream = getClass().getResourceAsStream("Test_Type3_Problem.pdf");
            FileOutputStream output = new FileOutputStream(new File(RESULT_FOLDER, "appendPdfs.pdf"));
        )
    {
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, output);
        document.open();
        document.add(new Paragraph("Some content to start with"));
        appendPDFs(Arrays.asList(testA4Stream, fromStream, prefaceStream, type3Stream), writer, document, null, false);
        document.close();
    }
}
 
Example #10
Source File: ChangeMargins.java    From testarea-itext5 with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * <a href="http://stackoverflow.com/questions/38057241/itextpdf-different-margin-on-specific-page">
 * itextpdf different margin on specific page
 * </a>
 * <p>
 * This test shows how to set different margins to separate pages.
 * </p> 
 */
@Test
public void testChangingMargins() throws IOException, DocumentException
{
    StringBuilder builder = new StringBuilder("test");
    for (int i = 0; i < 100; i++)
        builder.append(" test");
    String test = builder.toString();
    
    try (   OutputStream pdfStream = new FileOutputStream(new File(RESULT_FOLDER, "ChangingMargins.pdf")))
    {
        Document pdfDocument = new Document(PageSize.A4.rotate(), 0, 0, 0, 0);
        PdfWriter.getInstance(pdfDocument, pdfStream);
        pdfDocument.open();

        for (int m = 0; m < pdfDocument.getPageSize().getWidth() / 2 && m < pdfDocument.getPageSize().getHeight() / 2; m += 100)
        {
            // pdfDocument.setMargins(m, m, 100, 100);
            pdfDocument.setMargins(m, m, m, m);
            pdfDocument.newPage();
            pdfDocument.add(new Paragraph(test));
        }

        pdfDocument.close();
    }
}
 
Example #11
Source File: ColorParagraphBackground.java    From testarea-itext5 with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void testParagraphBackgroundEventListener() throws DocumentException, FileNotFoundException
{
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(new File(RESULT_FOLDER, "document-with-paragraph-backgrounds.pdf")));
    ParagraphBackground border = new ParagraphBackground();
    writer.setPageEvent(border);
    document.open();
    document.add(new Paragraph("Hello,"));
    document.add(new Paragraph("In this document, we'll add several paragraphs that will trigger page events. As long as the event isn't activated, nothing special happens, but let's make the event active and see what happens:"));
    border.setActive(true);
    document.add(new Paragraph("This paragraph now has a background. Isn't that fantastic? By changing the event, we can even draw a border, change the line width of the border and many other things. Now let's deactivate the event."));
    border.setActive(false);
    document.add(new Paragraph("This paragraph no longer has a background."));
    document.close();
}
 
Example #12
Source File: SimpleRedactionTest.java    From testarea-itext5 with GNU Affero General Public License v3.0 6 votes vote down vote up
static byte[] createSimpleTextPdf() throws DocumentException
{
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    Document document = new Document();
    PdfWriter.getInstance(document, baos);
    document.open();
    for (int i = 1; i < 20; i++)
    {
        Paragraph paragraph = new Paragraph();
        for (int j = 0; j < i; j++)
            paragraph.add("Hello World! ");
        document.add(paragraph);
    }
    document.close();

    return baos.toByteArray();
}
 
Example #13
Source File: PDF2TextExample.java    From tutorials with MIT License 6 votes vote down vote up
private static void generatePDFFromTxt(String filename) throws IOException, DocumentException {
	Document pdfDoc = new Document(PageSize.A4);
	PdfWriter.getInstance(pdfDoc, new FileOutputStream("src/output/txt.pdf"))
			.setPdfVersion(PdfWriter.PDF_VERSION_1_7);
	pdfDoc.open();
	
	Font myfont = new Font();
	myfont.setStyle(Font.NORMAL);
	myfont.setSize(11);
	pdfDoc.add(new Paragraph("\n"));
	
	BufferedReader br = new BufferedReader(new FileReader(filename));
	String strLine;
	while ((strLine = br.readLine()) != null) {
		Paragraph para = new Paragraph(strLine + "\n", myfont);
		para.setAlignment(Element.ALIGN_JUSTIFIED);
		pdfDoc.add(para);
	}
	
	pdfDoc.close();
	br.close();
}
 
Example #14
Source File: NormalSubTitle.java    From xiaoyaoji with GNU General Public License v3.0 5 votes vote down vote up
public NormalSubTitle(Paragraph parent, String title, int index, float vMargin) {

        setFont(font);
        Paragraph titlePara = new Paragraph(title, font);
        titlePara.setSpacingAfter(vMargin);
        titlePara.setSpacingBefore(vMargin);
        add(titlePara);

        if (parent != null) {
            parent.add(index, this);
        }
    }
 
Example #15
Source File: NormalTitle.java    From xiaoyaoji with GNU General Public License v3.0 5 votes vote down vote up
public NormalTitle(String title, int number) {
    super(number);
    Paragraph titlePara = new Paragraph(title, font);
    titlePara.setSpacingAfter(DEFAULT_MARGIN);
    titlePara.setSpacingBefore(DEFAULT_MARGIN);
    setTitle(titlePara);
    setTriggerNewPage(false);
}
 
Example #16
Source File: NormalSubTitle.java    From xiaoyaoji with GNU General Public License v3.0 5 votes vote down vote up
public NormalSubTitle(Paragraph parent, String title, int index) {

        setFont(font);
        Paragraph titlePara = new Paragraph(title, font);
        titlePara.setSpacingAfter(DEFAULT_MARGIN);
        titlePara.setSpacingBefore(DEFAULT_MARGIN);
        add(titlePara);

        if (parent != null) {
            parent.add(index, this);
        }
    }
 
Example #17
Source File: Helper.java    From mobikul-standalone-pos with MIT License 5 votes vote down vote up
Paragraph getParagraph(String name, Font font, int alignment) {
    Paragraph paragraph = new Paragraph(name, font);
    if (alignment == PDF_ALIGNMENT_RIGHT)
        paragraph.setAlignment(Element.ALIGN_RIGHT);
    else if (alignment == PDF_ALIGNMENT_CENTER)
        paragraph.setAlignment(Element.ALIGN_CENTER);
    else
        paragraph.setAlignment(Element.ALIGN_LEFT);
    return paragraph;
}
 
Example #18
Source File: PdfReportPageNumber.java    From bdf3 with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a header to every page
 * @see com.itextpdf.text.pdf.PdfPageEventHelper#onEndPage(
 *      com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)
 */
public void onEndPage(PdfWriter writer, Document document) {
	PdfPTable table = new PdfPTable(3);
	try {
		table.setWidths(new int[]{40,5,10});
		table.setTotalWidth(100);
		table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
		table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
		Font font=new Font(chineseFont,8);
		font.setColor(new BaseColor(55,55,55));
		Paragraph paragraph=new Paragraph("第   "+writer.getPageNumber()+" 页   共",font);
		paragraph.setAlignment(Element.ALIGN_RIGHT);
		table.addCell(paragraph);
		Image img=Image.getInstance(total);
		img.scaleAbsolute(28, 28);
		PdfPCell cell = new PdfPCell(img);
		cell.setBorder(Rectangle.NO_BORDER);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		table.addCell(cell);
		PdfPCell c = new PdfPCell(new Paragraph("页",font));
		c.setHorizontalAlignment(Element.ALIGN_LEFT);
		c.setBorder(Rectangle.NO_BORDER);
		table.addCell(c);
		float center=(document.getPageSize().getWidth())/2-120/2;
		table.writeSelectedRows(0, -1,center,30, writer.getDirectContent());
	}
	catch(DocumentException de) {
		throw new ExceptionConverter(de);
	}
}
 
Example #19
Source File: PdfReportBuilder.java    From bdf3 with Apache License 2.0 5 votes vote down vote up
public Document createDocument(ReportTitle reportTitle, OutputStream out) throws Exception {
	Document doc = new Document();
	PdfWriter writer = PdfWriter.getInstance(doc, out);
	if (reportTitle.isShowPageNo()) {
		PdfReportPageNumber event = new PdfReportPageNumber(chineseFont);
		writer.setPageEvent(event);
	}
	doc.open();
	Paragraph paragraph = this.createReportTitle(reportTitle);
	doc.add(paragraph);
	return doc;
}
 
Example #20
Source File: PdfTable.java    From smartcoins-wallet with MIT License 5 votes vote down vote up
PdfPTable addforCell(String subject, String detail) {
    PdfPTable table = new PdfPTable(2); // 2 columns.
    PdfPCell cell1 = new PdfPCell(new Paragraph(subject));
    PdfPCell cell2 = new PdfPCell(new Paragraph(detail));
    table.addCell(cell1);
    table.addCell(cell2);
    table.completeRow();
    return table;
}
 
Example #21
Source File: ThemeImpl.java    From ganttproject with GNU General Public License v3.0 5 votes vote down vote up
private void addEmptyRow(PdfPTable table, float height) {
  PdfPCell emptyCell = new PdfPCell(new Paragraph("  ", getSansRegular(height)));
  emptyCell.setBorderWidth(0);
  for (int i = 0; i < table.getNumberOfColumns(); i++) {
    table.addCell(emptyCell);
  }
}
 
Example #22
Source File: PDFMigrationReportWriter.java    From bonita-studio with GNU General Public License v2.0 5 votes vote down vote up
private void addTitlePage(Document document)
		throws DocumentException {
	Paragraph preface = new Paragraph();
	// Lets write a big header
	preface.add(new Paragraph(report.getName(), catFont));
	//addEmptyLine(preface, 1);
	// Will create: Report generated by: _name, _date
	preface.add(new Paragraph(new SimpleDateFormat("dd MMM yyyy",new Locale(Platform.getNL())).format(new Date()), smallBold));
	document.add(preface);

}
 
Example #23
Source File: PdfExportDialogFragment.java    From geopaparazzi with GNU General Public License v3.0 5 votes vote down vote up
private void addKeyValueToTableRow(PdfPTable table, String key, String value) {
    PdfPCell keyCell = new PdfPCell(new Paragraph(key));
    keyCell.setHorizontalAlignment(Element.ALIGN_CENTER);
    keyCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    keyCell.setPadding(10);
    table.addCell(keyCell);

    PdfPCell valueCell = new PdfPCell(new Phrase(value));
    valueCell.setHorizontalAlignment(Element.ALIGN_CENTER);
    valueCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    valueCell.setPadding(10);
    table.addCell(valueCell);
}
 
Example #24
Source File: Database.java    From GetIntoClub with GNU General Public License v3.0 5 votes vote down vote up
private void createPdf() throws FileNotFoundException, DocumentException {

        File docsFolder = new File(Environment.getExternalStorageDirectory() + "/Documents");
        if (!docsFolder.exists()) {
            docsFolder.mkdir();
            Log.i(TAG, "Created a new directory for PDF");
        }

        pdfFile = new File(docsFolder.getAbsolutePath(), "MyCV.pdf");
        OutputStream output = new FileOutputStream(pdfFile);
        Document document = new Document();
        PdfWriter.getInstance(document, output);
        document.open();
        Font bold = new Font(Font.FontFamily.HELVETICA, 20, Font.BOLD);
        Font bold1 = new Font(Font.FontFamily.TIMES_ROMAN, 28, Font.BOLDITALIC);
        Font regular = new Font(Font.FontFamily.HELVETICA, 18, Font.ITALIC);

        document.add(new Paragraph("CURRICULUM VITAE" + "\n\n", bold1));
        for (int i = 0; i < 12; i++) {
            document.add(new Paragraph(head[i], bold));
            document.add(new Paragraph(val[i] + "\n", regular));

        }
        document.close();
        Toast.makeText(Database.this, "MyCV.pdf created in Documents.", Toast.LENGTH_SHORT).show();
        finish();
        ContactDetail.name = "";
        ContactDetail.branch = "";
        ContactDetail.email = "";
        ContactDetail.mobile = "";
        SkillDetail.skill = "";
        SkillDetail.interset = "";
        SkillDetail.achievments = "";
        QuestionDetail.q1 = "";
        QuestionDetail.q2 = "";
        QuestionDetail.q3 = "";
        QuestionDetail.q4 = "";
        //        previewPdf();

    }
 
Example #25
Source File: StampHeader.java    From testarea-itext5 with GNU Affero General Public License v3.0 5 votes vote down vote up
byte[] createSampleDocument() throws IOException, DocumentException
{
	try (	ByteArrayOutputStream baos = new ByteArrayOutputStream()	)
	{
		Document doc = new Document(new RectangleReadOnly(842,595));
		PdfWriter.getInstance(doc, baos);
		doc.open();
		doc.add(new Paragraph("Test Page 1"));
		doc.newPage();
		doc.add(new Paragraph("Test Page 2"));
		doc.close();
		return baos.toByteArray();
	}
}
 
Example #26
Source File: ImportPageWithoutFreeSpace.java    From testarea-itext5 with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * <a href="http://stackoverflow.com/questions/31980979/itext-importing-styled-text-and-informations-from-an-existing-pdf">
 * iText: Importing styled Text and informations from an existing PDF
 * </a>
 * <p>
 * This method demonstrates how to import merely the region of a PDF page with
 * actual content. The main necessity is to call {@link #cropPdf(PdfReader)}
 * for the reader in question which restricts the media boxes of the pages to
 * the bounding box of the existing content.
 * </p>
 */
@Test
public void testImportPages() throws DocumentException, IOException
{
    byte[] docText = createSimpleTextPdf();
    Files.write(new File(RESULT_FOLDER, "textOnly.pdf").toPath(), docText);
    byte[] docGraphics = createSimpleCircleGraphicsPdf();
    Files.write(new File(RESULT_FOLDER, "graphicsOnly.pdf").toPath(), docGraphics);

    PdfReader readerText = new PdfReader(docText);
    cropPdf(readerText);
    PdfReader readerGraphics = new PdfReader(docGraphics);
    cropPdf(readerGraphics);
    try (   FileOutputStream fos = new FileOutputStream(new File(RESULT_FOLDER, "importPages.pdf")))
    {
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, fos);
        document.open();
        document.add(new Paragraph("Let's import 'textOnly.pdf'", new Font(FontFamily.HELVETICA, 12, Font.BOLD)));
        document.add(Image.getInstance(writer.getImportedPage(readerText, 1)));
        document.add(new Paragraph("and now 'graphicsOnly.pdf'", new Font(FontFamily.HELVETICA, 12, Font.BOLD)));
        document.add(Image.getInstance(writer.getImportedPage(readerGraphics, 1)));
        document.add(new Paragraph("That's all, folks!", new Font(FontFamily.HELVETICA, 12, Font.BOLD)));

        document.close();
    }
    finally
    {
        readerText.close();
        readerGraphics.close();
    }
}
 
Example #27
Source File: BinaryTransparency.java    From testarea-itext5 with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * <a href="http://stackoverflow.com/questions/39119776/itext-binary-transparency-bug">
 * iText binary transparency bug
 * </a>
 * <p>
 * Indeed, there is a bug in {@link Image#getInstance(Image, Color, boolean)},
 * the loop which determines whether to use a transparency array or a softmask
 * is erroneous and here falsely indicates a transparency array suffices.
 * </p>
 */
@Test
public void testBinaryTransparencyBug() throws IOException, DocumentException
{
    Document document = new Document();
    File file = new File(RESULT_FOLDER, "binary_transparency_bug.pdf");
    FileOutputStream outputStream = new FileOutputStream(file);
    PdfWriter writer = PdfWriter.getInstance(document, outputStream);
    document.open();

    addBackground(writer);
    document.add(new Paragraph("Binary transparency bug test case"));
    document.add(new Paragraph("OK: Visible image (opaque pixels are red, non opaque pixels are black)"));
    document.add(com.itextpdf.text.Image.getInstance(createBinaryTransparentAWTImage(Color.red,false,null), null));
    document.newPage();

    addBackground(writer);
    document.add(new Paragraph("Suspected bug: invisible image (both opaque an non opaque pixels have the same color)"));
    document.add(com.itextpdf.text.Image.getInstance(createBinaryTransparentAWTImage(Color.black,false,null), null));
    document.newPage();

    addBackground(writer);
    document.add(new Paragraph("Analysis: Aliasing makes the problem disappear, because this way the image is not binary transparent any more"));
    document.add(com.itextpdf.text.Image.getInstance(createBinaryTransparentAWTImage(Color.black,true,null), null));
    document.newPage();

    addBackground(writer);
    document.add(new Paragraph("Analysis: Setting the color of the transparent pixels to anything but black makes the problem go away, too"));
    document.add(com.itextpdf.text.Image.getInstance(createBinaryTransparentAWTImage(Color.black,false,Color.red), null));

    document.close();
}
 
Example #28
Source File: RootTitle.java    From xiaoyaoji with GNU General Public License v3.0 5 votes vote down vote up
public RootTitle(Document doc, String title) {
	Paragraph placeHolder = new Paragraph("\n");
	placeHolder.setSpacingAfter(doc.getPageSize().getHeight() * 0.1f);
	add(placeHolder);
	Paragraph p = new Paragraph(title, new Font(BaseInfo.cjkFont, 24f, Font.BOLD, BaseColor.BLACK));
	p.setAlignment(ALIGN_CENTER);
	p.setSpacingAfter(100);
	add(p);
}
 
Example #29
Source File: TableKeepTogether.java    From testarea-itext5 with GNU Affero General Public License v3.0 5 votes vote down vote up
private void printPage3(Document document, PdfContentByte canvas) throws DocumentException {
    int cols = 3;
    int rows = 15;

    PdfPTable table = new PdfPTable(cols);
    for (int row = 0; row < rows; row++) {
        for (int col = 0; col < cols; col++) {
            table.addCell(new Phrase("Cell " + row + ", " + col));
        }
    }
    table.setSpacingBefore(5);

    Rectangle docBounds = document.getPageSize();
    Rectangle upper = new Rectangle(docBounds.getLeft(20), docBounds.getTop(20) - 200, docBounds.getRight(20), docBounds.getTop(20));
    upper.setBackgroundColor(new BaseColor(23, 142, 255, 20));
    Rectangle lower = new Rectangle(docBounds.getLeft(20), docBounds.getBottom(20), docBounds.getRight(20), docBounds.getBottom(20) + 600);
    lower.setBackgroundColor(new BaseColor(255, 142, 23, 20));
    Rectangle[] rectangles = new Rectangle[] { upper, lower };

    for (Rectangle bounds : rectangles)
    {
        bounds.setBorder(Rectangle.BOX);
        bounds.setBorderColor(BaseColor.BLACK);
        bounds.setBorderWidth(1);

        canvas.rectangle(bounds);
    }

    rectangles = drawKeepTogether(new Paragraph("This table should keep together!"), canvas, rectangles);
    rectangles = drawKeepTogether(table, canvas, rectangles);
}
 
Example #30
Source File: LandscapePdf.java    From testarea-itext5 with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * <a href="https://stackoverflow.com/questions/47209312/i-have-a-trouble-with-lowagie-pdf-and-report-making-i-cant-include-headerfooter">
 * I have a trouble with lowagie pdf and Report Making. I cant include headerfooter on the first page
 * </a>
 * <p>
 * This example shows how to generate a PDF with page level
 * settings (page size, page margins) customized already on
 * the first page.
 * </p>
 */
@Test
public void testCreateLandscapeDocument() throws FileNotFoundException, DocumentException {
    Document document = new Document();

    PdfWriter.getInstance(document, new FileOutputStream(new File(RESULT_FOLDER, "landscape.pdf")));

    document.setPageSize(PageSize.A4.rotate());
    document.setMargins(60, 30, 30, 30);
    document.open();
    document.add(new Paragraph("Test string for a landscape PDF."));
    document.close();
}