org.apache.pdfbox.pdmodel.font.PDType1Font Java Examples

The following examples show how to use org.apache.pdfbox.pdmodel.font.PDType1Font. 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: JoinPages.java    From testarea-pdfbox2 with Apache License 2.0 7 votes vote down vote up
/**
 * @see #testJoinSmallAndBig()
 */
PDDocument prepareSmallPdf() throws IOException {
    PDDocument document = new PDDocument();
    PDPage page = new PDPage(new PDRectangle(72, 72));
    document.addPage(page);
    PDPageContentStream contentStream = new PDPageContentStream(document, page);
    contentStream.setNonStrokingColor(Color.YELLOW);
    contentStream.addRect(0, 0, 72, 72);
    contentStream.fill();
    contentStream.setNonStrokingColor(Color.BLACK);
    PDFont font = PDType1Font.HELVETICA;
    contentStream.beginText();
    contentStream.setFont(font, 18);
    contentStream.newLineAtOffset(2, 54);
    contentStream.showText("small");
    contentStream.newLineAtOffset(0, -48);
    contentStream.showText("page");
    contentStream.endText();
    contentStream.close();
    return document;
}
 
Example #2
Source File: PDTextAppearanceHandler.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
private void drawNewParagraph(PDAnnotationText annotation, final PDAppearanceContentStream contentStream)
        throws IOException
{
    adjustRectAndBBox(annotation, 13, 20);

    contentStream.setMiterLimit(4);
    contentStream.setLineJoinStyle(0);
    contentStream.setLineCapStyle(0);
    contentStream.setLineWidth(0.59f); // value from Adobe

    // small triangle (values from Adobe)
    contentStream.moveTo(6.4995f, 20);
    contentStream.lineTo(0.295f, 7.287f);
    contentStream.lineTo(12.705f, 7.287f);
    contentStream.closeAndFillAndStroke();

    // rescale and translate so that "NP" fits below the triangle
    // values gathered by trial and error
    contentStream.transform(Matrix.getScaleInstance(0.001f * 4, 0.001f * 4));
    contentStream.transform(Matrix.getTranslateInstance(200, 0));
    addPath(contentStream, PDType1Font.HELVETICA_BOLD.getPath("N"));
    contentStream.transform(Matrix.getTranslateInstance(1300, 0));
    addPath(contentStream, PDType1Font.HELVETICA_BOLD.getPath("P"));
    contentStream.fill();
}
 
Example #3
Source File: DenseMerging.java    From testarea-pdfbox2 with Apache License 2.0 6 votes vote down vote up
PDDocument createTextDocument(PDRectangle size, Matrix textMatrix, String... lines) throws IOException {
    PDDocument document = new PDDocument();
    PDPage page = new PDPage(size);
    document.addPage(page);

    try (PDPageContentStream canvas = new PDPageContentStream(document, page)) {
        canvas.beginText();
        canvas.setTextMatrix(textMatrix);
        canvas.setFont(PDType1Font.HELVETICA_BOLD, 12);
        canvas.setLeading(14);
        for (String line : lines) {
            canvas.showText(line);
            canvas.newLine();
        }
        canvas.endText();
    }

    return document;
}
 
Example #4
Source File: JoinPages.java    From testarea-pdfbox2 with Apache License 2.0 6 votes vote down vote up
/**
 * @see #testJoinSmallAndBig()
 */
PDDocument prepareBiggerPdf() throws IOException {
    PDDocument document = new PDDocument();
    PDPage page = new PDPage(PDRectangle.A5);
    document.addPage(page);
    PDPageContentStream contentStream = new PDPageContentStream(document, page);
    contentStream.setNonStrokingColor(Color.GREEN);
    contentStream.addRect(0, 0, PDRectangle.A5.getWidth(), PDRectangle.A5.getHeight());
    contentStream.fill();
    contentStream.setNonStrokingColor(Color.BLACK);
    PDFont font = PDType1Font.HELVETICA;
    contentStream.beginText();
    contentStream.setFont(font, 18);
    contentStream.newLineAtOffset(2, PDRectangle.A5.getHeight() - 24);
    contentStream.showText("This is the Bigger page");
    contentStream.newLineAtOffset(0, -48);
    contentStream.showText("BIGGER!");
    contentStream.endText();
    contentStream.close();
    return document;
}
 
Example #5
Source File: RotatedTextOnLine.java    From testarea-pdfbox2 with Apache License 2.0 6 votes vote down vote up
/**
 * <a href="https://stackoverflow.com/questions/52054396/rotate-text-in-pdfbox-with-java">
 * Rotate text in pdfbox with java
 * </a>
 * <p>
 * This test shows how to show rotated text above a line.
 * </p>
 */
@Test
public void testRotatedTextOnLineForCedrickKapema() throws IOException {
    PDDocument doc = new PDDocument();
    PDPage page = new PDPage();
    doc.addPage(page);
    PDPageContentStream cos = new PDPageContentStream(doc, page);
    cos.transform(Matrix.getRotateInstance(-Math.PI / 6, 100, 650));
    cos.moveTo(0, 0);
    cos.lineTo(125, 0);
    cos.stroke();
    cos.beginText();
    String text = "0.72";
    cos.newLineAtOffset(50, 5);
    cos.setFont(PDType1Font.HELVETICA_BOLD, 12);
    cos.showText(text);
    cos.endText();
    cos.close();
    doc.save(new File(RESULT_FOLDER, "TextOnLine.pdf"));
    doc.close();
}
 
Example #6
Source File: DefaultVsNativeDrawerComparatorTest.java    From dss with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void nativeFontTest() throws IOException {
	initVisibleCombinationTest();
	
	SignatureImageParameters signatureImageParameters = new SignatureImageParameters();
	
	SignatureImageTextParameters textParameters = new SignatureImageTextParameters();
	textParameters.setText("My signature\nOne more line\nAnd the last line");
	textParameters.setTextColor(Color.BLUE);
	textParameters.setBackgroundColor(Color.YELLOW);
	textParameters.setSignerTextHorizontalAlignment(SignerTextHorizontalAlignment.CENTER);
	
	textParameters.setFont(new PdfBoxNativeFont(PDType1Font.HELVETICA));
	
	signatureImageParameters.setTextParameters(textParameters);
	signatureParameters.setImageParameters(signatureImageParameters);

	service.setPdfObjFactory(new PdfBoxDefaultObjectFactory());
	Exception exception = assertThrows(DSSException.class , () -> sign(testName + "_default"));
	assertEquals("PdfBoxNativeFont.class can be used only with PdfBoxNativeObjectFactory!", exception.getMessage());
	
	service.setPdfObjFactory(new PdfBoxNativeObjectFactory());
	DSSDocument nativeDrawerPdf = sign(testName + "_native");
	assertNotNull(nativeDrawerPdf);
}
 
Example #7
Source File: AddTextWithDynamicFonts.java    From testarea-pdfbox2 with Apache License 2.0 6 votes vote down vote up
/**
 * @see #testAddLikeCccompany()
 */
private static ByteArrayOutputStream generatePdfFromString(String content) throws IOException {
    PDPage page = new PDPage();

    try (PDDocument doc = new PDDocument();
         PDPageContentStream contentStream = new PDPageContentStream(doc, page)) {
        doc.addPage(page);
        contentStream.setFont(PDType1Font.HELVETICA, 12);

        // Or load a specific font from a file
        // contentStream.setFont(PDType0Font.load(this.doc, new File("/fontPath.ttf")), 12);

        contentStream.beginText();
        contentStream.showText(content);
        contentStream.endText();
        contentStream.close();
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        doc.save(os);
        return os;
    }
}
 
Example #8
Source File: TestProjectionProfile.java    From tabula-java with MIT License 6 votes vote down vote up
@Before
public void setUpProjectionProfile() {
	PDPage pdPage = new PDPage();
	
	TextElement textElement = new TextElement(5f, 15f, 10f, 20f, PDType1Font.HELVETICA, 1f, "test", 1f);
	TextElement textElement2 = new TextElement(5f, 15f, 10f, 20f, PDType1Font.HELVETICA, 1f, "test", 1f);
	List<TextElement> textList = new ArrayList<>();
	textList.add(textElement);
	textList.add(textElement2);

	Ruling ruling = new Ruling(0, 0, 10, 10);
	List<Ruling> rulingList = new ArrayList<>();
	rulingList.add(ruling);


	page = new Page(0, 0, 1, 1, 0, 1, pdPage, textList, rulingList);
	
	List<Rectangle> rectangles = new ArrayList<>();
	rectangles.add(new Rectangle(0f, 0f, 500f, 5f));
	
	pProfile = new ProjectionProfile(page, rectangles, 5, 5);
}
 
Example #9
Source File: PdfUtilTest.java    From easytable with MIT License 6 votes vote down vote up
@Test(timeout = 5000L)
public void testVeryBigText() {
    final StringBuilder builder = new StringBuilder();
    final List<String> expectedOutput = new ArrayList<>();

    for (int i = 0; i < 50; i++) {
        builder.append("https://averylonginternetdnsnamewhich-maybe-breaks-easytable.com ");

        // optimal text-break
        expectedOutput.add("https://averylonginternetdns-");
        expectedOutput.add("namewhich-maybe-breaks-");
        expectedOutput.add("easytable.com");
    }

    final List<String> actualOutput = PdfUtil.getOptimalTextBreakLines(builder.toString(), PDType1Font.HELVETICA, 8, 102);

    assertThat(actualOutput, equalTo(expectedOutput));
}
 
Example #10
Source File: PDTextAppearanceHandler.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
private void drawCrossHairs(PDAnnotationText annotation, final PDAppearanceContentStream contentStream)
        throws IOException
{
    PDRectangle bbox = adjustRectAndBBox(annotation, 20, 20);

    float min = Math.min(bbox.getWidth(), bbox.getHeight());

    contentStream.setMiterLimit(4);
    contentStream.setLineJoinStyle(0);
    contentStream.setLineCapStyle(0);
    contentStream.setLineWidth(0.61f); // value from Adobe

    contentStream.transform(Matrix.getScaleInstance(0.001f * min / 1.5f, 0.001f * min / 1.5f));
    contentStream.transform(Matrix.getTranslateInstance(0, 50));

    // we get the shape of a Symbol crosshair (0x2295) and use that one.
    // Adobe uses a different font (which one?), or created the shape from scratch.
    GeneralPath path = PDType1Font.SYMBOL.getPath("circleplus");
    addPath(contentStream, path);
    contentStream.fillAndStroke();
}
 
Example #11
Source File: PDTextAppearanceHandler.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
private void drawRightPointer(PDAnnotationText annotation, final PDAppearanceContentStream contentStream)
        throws IOException
{
    PDRectangle bbox = adjustRectAndBBox(annotation, 20, 17);

    float min = Math.min(bbox.getWidth(), bbox.getHeight());

    contentStream.setMiterLimit(4);
    contentStream.setLineJoinStyle(1);
    contentStream.setLineCapStyle(0);
    contentStream.setLineWidth(0.59f); // value from Adobe

    contentStream.transform(Matrix.getScaleInstance(0.001f * min / 0.8f, 0.001f * min / 0.8f));
    contentStream.transform(Matrix.getTranslateInstance(0, 50));

    // we get the shape of a Zapf Dingbats right pointer (0x27A4) and use that one.
    // Adobe uses a different font (which one?), or created the shape from scratch.
    GeneralPath path = PDType1Font.ZAPF_DINGBATS.getPath("a174");
    addPath(contentStream, path);
    contentStream.fillAndStroke();
}
 
Example #12
Source File: PDTextAppearanceHandler.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
private void drawCheck(PDAnnotationText annotation, final PDAppearanceContentStream contentStream)
        throws IOException
{
    PDRectangle bbox = adjustRectAndBBox(annotation, 20, 19);

    float min = Math.min(bbox.getWidth(), bbox.getHeight());

    contentStream.setMiterLimit(4);
    contentStream.setLineJoinStyle(1);
    contentStream.setLineCapStyle(0);
    contentStream.setLineWidth(0.59f); // value from Adobe

    contentStream.transform(Matrix.getScaleInstance(0.001f * min / 0.8f, 0.001f * min / 0.8f));
    contentStream.transform(Matrix.getTranslateInstance(0, 50));

    // we get the shape of a Zapf Dingbats check (0x2714) and use that one.
    // Adobe uses a different font (which one?), or created the shape from scratch.
    GeneralPath path = PDType1Font.ZAPF_DINGBATS.getPath("a20");
    addPath(contentStream, path);
    contentStream.fillAndStroke();
}
 
Example #13
Source File: TestTextElement.java    From tabula-java with MIT License 6 votes vote down vote up
@Test
public void mergeElementsWithSkippingRules() {
	
	List<TextElement> elements = new ArrayList<>();
	elements.add(new TextElement(0f, 15f, 10f, 20f, PDType1Font.HELVETICA, 1f, "A", 1f, 6f));
	elements.add(new TextElement(0f, 17f, 10f, 20f, PDType1Font.HELVETICA, 1f, "A", 1f, 6f));
	elements.add(new TextElement(0f, 25f, 10f, 20f, PDType1Font.HELVETICA, 1f, "B", 1f, 6f));
	elements.add(new TextElement(0.001f, 25f, 10f, 20f, PDType1Font.HELVETICA, 1f, " ", 1f, 6f));
	elements.add(new TextElement(0f, 35f, 10f, 20f, PDType1Font.HELVETICA, 1f, "C", 1f, 6f));
	elements.add(new TextElement(0f, 45f, 10f, 20f, PDType1Font.TIMES_ROMAN, 10f, "D", 1f, 6f));
	
	List<TextChunk> words = TextElement.mergeWords(elements);
	
	List<TextChunk> expectedWords = new ArrayList<>();
	TextChunk textChunk = new TextChunk(new TextElement(0f, 15f, 10f, 20f, PDType1Font.HELVETICA, 1f, "A", 1f, 6f));
	textChunk.add(new TextElement(0f, 25f, 10f, 20f, PDType1Font.HELVETICA, 1f, "B", 1f, 6f));
	textChunk.add(new TextElement(0f, 35f, 10f, 20f, PDType1Font.HELVETICA, 1f, "C", 1f, 6f));
	textChunk.add(new TextElement(0f, 45f, 10f, 20f, PDType1Font.TIMES_ROMAN, 10f, "D", 1f, 6f));
	expectedWords.add(textChunk);
	
	Assert.assertEquals(expectedWords, words);
	
}
 
Example #14
Source File: TestTextElement.java    From tabula-java with MIT License 6 votes vote down vote up
@Test
public void mergeElementsShouldBeIdempotent() {
	/*
   * a bug in TextElement.merge_words would delete the first TextElement in the array
   * it was called with. Discussion here: https://github.com/tabulapdf/tabula-java/issues/78
   */

	List<TextElement> elements = new ArrayList<>();
	elements.add(new TextElement(0f, 15f, 10f, 20f, PDType1Font.HELVETICA, 1f, "A", 1f, 6f));
	elements.add(new TextElement(0f, 25f, 10f, 20f, PDType1Font.HELVETICA, 1f, "B", 1f, 6f));
	elements.add(new TextElement(0f, 35f, 10f, 20f, PDType1Font.HELVETICA, 1f, "C", 1f, 6f));
	elements.add(new TextElement(0f, 45f, 10f, 20f, PDType1Font.HELVETICA, 1f, "D", 1f, 6f));
	
	List<TextChunk> words = TextElement.mergeWords(elements);
	List<TextChunk> words2 = TextElement.mergeWords(elements);
	Assert.assertEquals(words, words2);
}
 
Example #15
Source File: TestTextElement.java    From tabula-java with MIT License 6 votes vote down vote up
@Test
public void mergeFourElementsIntoOneWord() {
	
	List<TextElement> elements = new ArrayList<>();
	elements.add(new TextElement(0f, 15f, 10f, 20f, PDType1Font.HELVETICA, 1f, "A", 1f, 6f));
	elements.add(new TextElement(0f, 25f, 10f, 20f, PDType1Font.HELVETICA, 1f, "B", 1f, 6f));
	elements.add(new TextElement(0f, 35f, 10f, 20f, PDType1Font.HELVETICA, 1f, "C", 1f, 6f));
	elements.add(new TextElement(0f, 45f, 10f, 20f, PDType1Font.HELVETICA, 1f, "D", 1f, 6f));
	
	List<TextChunk> words = TextElement.mergeWords(elements);
	
	List<TextChunk> expectedWords = new ArrayList<>();
	TextChunk textChunk = new TextChunk(new TextElement(0f, 15f, 10f, 20f, PDType1Font.HELVETICA, 1f, "A", 1f, 6f));
	textChunk.add(new TextElement(0f, 25f, 10f, 20f, PDType1Font.HELVETICA, 1f, "B", 1f, 6f));
	textChunk.add(new TextElement(0f, 35f, 10f, 20f, PDType1Font.HELVETICA, 1f, "C", 1f, 6f));
	textChunk.add(new TextElement(0f, 45f, 10f, 20f, PDType1Font.HELVETICA, 1f, "D", 1f, 6f));
	expectedWords.add(textChunk);
	
	Assert.assertEquals(expectedWords, words);
	
}
 
Example #16
Source File: TestTextElement.java    From tabula-java with MIT License 6 votes vote down vote up
@Test
public void mergeFourElementsIntoFourWords() {
	
	List<TextElement> elements = new ArrayList<>();
	elements.add(new TextElement(0f, 15f, 10f, 20f, PDType1Font.HELVETICA, 1f, "A", 1f, 6f));
	elements.add(new TextElement(20f, 15f, 10f, 20f, PDType1Font.HELVETICA, 1f, "B", 1f, 6f));
	elements.add(new TextElement(40f, 15f, 10f, 20f, PDType1Font.HELVETICA, 1f, "C", 1f, 6f));
	elements.add(new TextElement(60f, 15f, 10f, 20f, PDType1Font.HELVETICA, 1f, "D", 1f, 6f));
	
	List<TextChunk> words = TextElement.mergeWords(elements);
	
	List<TextChunk> expectedWords = new ArrayList<>();
	expectedWords.add(new TextChunk(new TextElement(0f, 15f, 10f, 20f, PDType1Font.HELVETICA, 1f, "A", 1f, 6f)));
	expectedWords.add(new TextChunk(new TextElement(20f, 15f, 10f, 20f, PDType1Font.HELVETICA, 1f, "B", 1f, 6f)));
	expectedWords.add(new TextChunk(new TextElement(40f, 15f, 10f, 20f, PDType1Font.HELVETICA, 1f, "C", 1f, 6f)));
	expectedWords.add(new TextChunk(new TextElement(60f, 15f, 10f, 20f, PDType1Font.HELVETICA, 1f, "D", 1f, 6f)));
	
	Assert.assertEquals(expectedWords, words);
	
}
 
Example #17
Source File: TestTextElement.java    From tabula-java with MIT License 6 votes vote down vote up
@Test
public void createTextElementWithDirection() {
	
	TextElement textElement = new TextElement(5f, 15f, 10f, 20f, PDType1Font.HELVETICA, 1f, "A", 1f, 6f);
	
	Assert.assertNotNull(textElement);
	Assert.assertEquals("A", textElement.getText());
	Assert.assertEquals(1f, textElement.getFontSize(), 0);
	Assert.assertEquals(15f, textElement.getLeft(), 0);
	Assert.assertEquals(5f, textElement.getTop(), 0);
	Assert.assertEquals(10f, textElement.getWidth(), 0);
	Assert.assertEquals(20f, textElement.getHeight(), 0);
	Assert.assertEquals(PDType1Font.HELVETICA, textElement.getFont());
	Assert.assertEquals(1f, textElement.getWidthOfSpace(), 0);
	Assert.assertEquals(6f, textElement.getDirection(), 0);
	
	
}
 
Example #18
Source File: TestTextElement.java    From tabula-java with MIT License 6 votes vote down vote up
@Test
public void createTextElement() {
	
	TextElement textElement = new TextElement(5f, 15f, 10f, 20f, PDType1Font.HELVETICA, 1f, "A", 1f);
	
	Assert.assertNotNull(textElement);
	Assert.assertEquals("A", textElement.getText());
	Assert.assertEquals(1f, textElement.getFontSize(), 0);
	Assert.assertEquals(15f, textElement.getLeft(), 0);
	Assert.assertEquals(5f, textElement.getTop(), 0);
	Assert.assertEquals(10f, textElement.getWidth(), 0);
	Assert.assertEquals(20f, textElement.getHeight(), 0);
	Assert.assertEquals(PDType1Font.HELVETICA, textElement.getFont());
	Assert.assertEquals(1f, textElement.getWidthOfSpace(), 0);
	Assert.assertEquals(0f, textElement.getDirection(), 0);
	
	
}
 
Example #19
Source File: PDTextAppearanceHandler.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
private void drawStar(PDAnnotationText annotation, final PDAppearanceContentStream contentStream)
        throws IOException
{
    PDRectangle bbox = adjustRectAndBBox(annotation, 20, 19);

    float min = Math.min(bbox.getWidth(), bbox.getHeight());

    contentStream.setMiterLimit(4);
    contentStream.setLineJoinStyle(1);
    contentStream.setLineCapStyle(0);
    contentStream.setLineWidth(0.59f); // value from Adobe

    contentStream.transform(Matrix.getScaleInstance(0.001f * min / 0.8f, 0.001f * min / 0.8f));

    // we get the shape of a Zapf Dingbats star (0x2605) and use that one.
    // Adobe uses a different font (which one?), or created the shape from scratch.
    GeneralPath path = PDType1Font.ZAPF_DINGBATS.getPath("a35");
    addPath(contentStream, path);
    contentStream.fillAndStroke();
}
 
Example #20
Source File: PDFCanvas.java    From SwissQRBill with MIT License 6 votes vote down vote up
@Override
public void putTextLines(String[] lines, double x, double y, int fontSize, double leading) throws IOException {
    x *= MM_TO_PT;
    y *= MM_TO_PT;
    float lineHeight = (float) ((fontMetrics.getLineHeight(fontSize) + leading) * MM_TO_PT);
    contentStream.setFont(PDType1Font.HELVETICA, fontSize);
    contentStream.beginText();
    contentStream.newLineAtOffset((float) x, (float) y);
    boolean isFirstLine = true;
    for (String line : lines) {
        if (isFirstLine) {
            isFirstLine = false;
        } else {
            contentStream.newLineAtOffset(0, -lineHeight);
        }
        contentStream.showText(line);
    }
    contentStream.endText();
}
 
Example #21
Source File: ArrangeText.java    From testarea-pdfbox2 with Apache License 2.0 5 votes vote down vote up
/**
 * <a href="https://stackoverflow.com/questions/48902656/how-can-i-align-arrange-text-fields-into-two-column-layout-using-apache-pdfbox">
 * How can I Align/ Arrange text fields into two column layout using Apache PDFBox - java
 * </a>
 * <p>
 * This test shows how to align text in two columns.
 * </p>
 */
@Test
public void testArrangeTextForUser2967784() throws IOException {
    PDDocument document = new PDDocument();
    PDPage page = new PDPage();
    document.addPage(page);
    PDFont fontNormal = PDType1Font.HELVETICA;
    PDFont fontBold = PDType1Font.HELVETICA_BOLD;
    PDPageContentStream contentStream =new PDPageContentStream(document, page);
    contentStream.beginText();
    contentStream.newLineAtOffset(100, 600);
    contentStream.setFont(fontBold, 15);
    contentStream.showText("Name: ");
    contentStream.setFont(fontNormal, 15);
    contentStream.showText ("Rajeev");
    contentStream.newLineAtOffset(200, 00);
    contentStream.setFont(fontBold, 15);
    contentStream.showText("Address: " );
    contentStream.setFont(fontNormal, 15);
    contentStream.showText ("BNG");
    contentStream.newLineAtOffset(-200, -20);
    contentStream.setFont(fontBold, 15);
    contentStream.showText("State: " );
    contentStream.setFont(fontNormal, 15);
    contentStream.showText ("KAR");
    contentStream.newLineAtOffset(200, 00);
    contentStream.setFont(fontBold, 15);
    contentStream.showText("Country: " );
    contentStream.setFont(fontNormal, 15);
    contentStream.showText ("INDIA");
    contentStream.endText();
    contentStream.close();
    document.save(new File(RESULT_FOLDER, "arrangedTextForUser2967784.pdf"));
}
 
Example #22
Source File: PdfBoxPDFWatermarkWriter.java    From website with GNU Affero General Public License v3.0 5 votes vote down vote up
private void createPDFLines() {
	getColumn1line1().addLineText(PDType1Font.HELVETICA_OBLIQUE, 6.0f, getWatermark().getDocumentTitle());
	if (!StringUtils.isBlank(getWatermark().getDocumentRightsHolder())) {
		getColumn1line1().addLineText(PDType1Font.HELVETICA, 6.0f, " (" + getWatermark().getDocumentRightsHolder() + ") |");
	}
	getColumn1line1().addLineText(PDType1Font.HELVETICA, 6.0f, " Printed by " + getWatermark().getOutletName());
	
	getColumn1line2().addLineText(PDType1Font.HELVETICA, 6.0f, "for " + getWatermark().getCustomerName() + " | " + getWatermark().getTransactionDate().toString("yyyy/MM/dd"));
	
	getColumn2line1().addLineText(PDType1Font.HELVETICA_OBLIQUE, 6.0f, getWatermark().getUrl());

	getColumn2line2().addLineText(PDType1Font.HELVETICA, 6.0f, getWatermark().getAdditionalText());
	
}
 
Example #23
Source File: TestLine.java    From tabula-java with MIT License 5 votes vote down vote up
@Test
public void testAddTextChunkIntTextChunk() {
	Line line = new Line();
	
	TextElement tElement = new TextElement(0, 0, 0, 0, PDType1Font.HELVETICA_BOLD, 10, "test", 5);
	TextChunk tChunk = new TextChunk(tElement);
	line.addTextChunk(3, tChunk);
	
	assertEquals("test", line.getTextElements().get(3).getText());
	}
 
Example #24
Source File: TestTextElement.java    From tabula-java with MIT License 5 votes vote down vote up
@Test
public void mergeTenElementsIntoTwoWords() {
	
	List<TextElement> elements = new ArrayList<>();
	elements.add(new TextElement(0f, 0f, 10f, 20f, PDType1Font.HELVETICA, 1f, "H", 1f, 6f));
	elements.add(new TextElement(0f, 10f, 10f, 20f, PDType1Font.HELVETICA, 1f, "O", 1f, 6f));
	elements.add(new TextElement(0f, 20f, 10f, 20f, PDType1Font.HELVETICA, 1f, "L", 1f, 6f));
	elements.add(new TextElement(0f, 30f, 10f, 20f, PDType1Font.HELVETICA, 1f, "A", 1f, 6f));
	elements.add(new TextElement(0f, 60f, 10f, 20f, PDType1Font.HELVETICA, 1f, "M", 1f, 6f));
	elements.add(new TextElement(0f, 70f, 10f, 20f, PDType1Font.HELVETICA, 1f, "U", 1f, 6f));
	elements.add(new TextElement(0f, 80f, 10f, 20f, PDType1Font.HELVETICA, 1f, "N", 1f, 6f));
	elements.add(new TextElement(0f, 90f, 10f, 20f, PDType1Font.HELVETICA, 1f, "D", 1f, 6f));
	elements.add(new TextElement(0f, 100f, 10f, 20f, PDType1Font.HELVETICA, 1f, "O", 1f, 6f));
	
	List<TextChunk> words = TextElement.mergeWords(elements);
	
	List<TextChunk> expectedWords = new ArrayList<>();
	TextChunk textChunk = new TextChunk(new TextElement(0f, 0f, 10f, 20f, PDType1Font.HELVETICA, 1f, "H", 1f, 6f));
	textChunk.add(new TextElement(0f, 10f, 10f, 20f, PDType1Font.HELVETICA, 1f, "O", 1f, 6f));
	textChunk.add(new TextElement(0f, 20f, 10f, 20f, PDType1Font.HELVETICA, 1f, "L", 1f, 6f));
	textChunk.add(new TextElement(0f, 30f, 10f, 20f, PDType1Font.HELVETICA, 1f, "A", 1f, 6f));
	textChunk.add(new TextElement(0f, 30f, 10.5f, 20f, PDType1Font.HELVETICA, 1f, " ", 1f)); //Check why width=10.5?
	expectedWords.add(textChunk);
	TextChunk textChunk2 = new TextChunk(new TextElement(0f, 60f, 10f, 20f, PDType1Font.HELVETICA, 1f, "M", 1f, 6f));
	textChunk2.add(new TextElement(0f, 70f, 10f, 20f, PDType1Font.HELVETICA, 1f, "U", 1f, 6f));
	textChunk2.add(new TextElement(0f, 80f, 10f, 20f, PDType1Font.HELVETICA, 1f, "N", 1f, 6f));
	textChunk2.add(new TextElement(0f, 90f, 10f, 20f, PDType1Font.HELVETICA, 1f, "D", 1f, 6f));
	textChunk2.add(new TextElement(0f, 100f, 10f, 20f, PDType1Font.HELVETICA, 1f, "O", 1f, 6f));
	expectedWords.add(textChunk2);
	
	Assert.assertEquals(2, words.size());
	Assert.assertEquals(expectedWords, words);
	
}
 
Example #25
Source File: TestCell.java    From tabula-java with MIT License 5 votes vote down vote up
@Test
public void testGetTextElements() {
	Cell cell = new Cell(0, 0, 0, 0);
	assertTrue(cell.getTextElements().isEmpty());
	
	TextElement tElement = new TextElement(0, 0, 0, 0, PDType1Font.HELVETICA_BOLD, 10, "test", 5);
	TextChunk tChunk = new TextChunk(tElement);
	List<TextChunk> tList = new ArrayList<>();
	tList.add(tChunk);
	cell.setTextElements(tList);
	
	assertEquals("test", cell.getTextElements().get(0).getText());
	
	
	}
 
Example #26
Source File: TestLine.java    From tabula-java with MIT License 5 votes vote down vote up
@Test
public void testToString() {
	Line line = new Line();
	
	TextElement tElement = new TextElement(0, 0, 0, 0, PDType1Font.HELVETICA_BOLD, 10, "test", 5);
	TextChunk tChunk = new TextChunk(tElement);
	line.addTextChunk(0, tChunk);
	line.addTextChunk(0, tChunk);
	
	assertEquals("technology.tabula.Line[x=0.0,y=0.0,w=0.0,h=0.0,bottom=0.000000,right=0.000000,chunks='testtest', ]", line.toString());
}
 
Example #27
Source File: TestLine.java    From tabula-java with MIT License 5 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void testErrorAddTextChunkIntTextChunk() {
	Line line = new Line();
	
	TextElement tElement = new TextElement(0, 0, 0, 0, PDType1Font.HELVETICA_BOLD, 10, "test", 5);
	TextChunk tChunk = new TextChunk(tElement);
	line.addTextChunk(-1, tChunk);
	}
 
Example #28
Source File: TestLine.java    From tabula-java with MIT License 5 votes vote down vote up
@Test
public void testLessThanAddTextChunkIntTextChunk() {
	Line line = new Line();
	
	TextElement tElement = new TextElement(0, 0, 0, 0, PDType1Font.HELVETICA_BOLD, 10, "test", 5);
	TextChunk tChunk = new TextChunk(tElement);
	line.addTextChunk(0, tChunk);
	line.addTextChunk(0, tChunk);
	
	assertEquals("testtest", line.getTextElements().get(0).getText());
	}
 
Example #29
Source File: TestTextElement.java    From tabula-java with MIT License 5 votes vote down vote up
@Test
public void mergeTenElementsIntoTwoLines() {
	
	List<TextElement> elements = new ArrayList<>();
	elements.add(new TextElement(0f, 0f, 10f, 20f, PDType1Font.HELVETICA, 1f, "H", 1f, 6f));
	elements.add(new TextElement(0f, 10f, 10f, 20f, PDType1Font.HELVETICA, 1f, "O", 1f, 6f));
	elements.add(new TextElement(0f, 20f, 10f, 20f, PDType1Font.HELVETICA, 1f, "L", 1f, 6f));
	elements.add(new TextElement(0f, 30f, 10f, 20f, PDType1Font.HELVETICA, 1f, "A", 1f, 6f));
	elements.add(new TextElement(20f, 0f, 10f, 20f, PDType1Font.HELVETICA, 1f, "M", 1f, 6f));
	elements.add(new TextElement(20f, 10f, 10f, 20f, PDType1Font.HELVETICA, 1f, "U", 1f, 6f));
	elements.add(new TextElement(20f, 20f, 10f, 20f, PDType1Font.HELVETICA, 1f, "N", 1f, 6f));
	elements.add(new TextElement(20f, 30f, 10f, 20f, PDType1Font.HELVETICA, 1f, "D", 1f, 6f));
	elements.add(new TextElement(20f, 40f, 10f, 20f, PDType1Font.HELVETICA, 1f, "O", 1f, 6f));
	
	List<TextChunk> words = TextElement.mergeWords(elements);
	
	List<TextChunk> expectedWords = new ArrayList<>();
	TextChunk textChunk = new TextChunk(new TextElement(0f, 0f, 10f, 20f, PDType1Font.HELVETICA, 1f, "H", 1f, 6f));
	textChunk.add(new TextElement(0f, 10f, 10f, 20f, PDType1Font.HELVETICA, 1f, "O", 1f, 6f));
	textChunk.add(new TextElement(0f, 20f, 10f, 20f, PDType1Font.HELVETICA, 1f, "L", 1f, 6f));
	textChunk.add(new TextElement(0f, 30f, 10f, 20f, PDType1Font.HELVETICA, 1f, "A", 1f, 6f));
	expectedWords.add(textChunk);
	TextChunk textChunk2 = new TextChunk(new TextElement(20f, 0f, 10f, 20f, PDType1Font.HELVETICA, 1f, "M", 1f, 6f));
	textChunk2.add(new TextElement(20f, 10f, 10f, 20f, PDType1Font.HELVETICA, 1f, "U", 1f, 6f));
	textChunk2.add(new TextElement(20f, 20f, 10f, 20f, PDType1Font.HELVETICA, 1f, "N", 1f, 6f));
	textChunk2.add(new TextElement(20f, 30f, 10f, 20f, PDType1Font.HELVETICA, 1f, "D", 1f, 6f));
	textChunk2.add(new TextElement(20f, 40f, 10f, 20f, PDType1Font.HELVETICA, 1f, "O", 1f, 6f));
	expectedWords.add(textChunk2);
	
	Assert.assertEquals(2, words.size());
	Assert.assertEquals(expectedWords, words);
	
}
 
Example #30
Source File: HelloDoc.java    From pdfbox-layout with MIT License 5 votes vote down vote up
public static void main(String[] args) throws Exception {
Document document = new Document(40, 60, 40, 60);

Paragraph paragraph = new Paragraph();
paragraph.addText("Hello Document", 20,
	PDType1Font.HELVETICA);
document.add(paragraph);

final OutputStream outputStream = new FileOutputStream("hellodoc.pdf");
document.save(outputStream);

   }