Shape

Shape (3)

Monday, 22 June 2020 08:13

Java draw dash and solid line to PDF

Written by support iceblue

This article will show you how to draw dash and solid line in Java applications.

import com.spire.pdf.*;
import com.spire.pdf.graphics.*;
import java.awt.*;

public class DrawLines {
    public static void main(String[] args) {

        //Create a new PdfDocument instance and add a new page
        PdfDocument pdf = new PdfDocument();
        PdfPageBase page = pdf.getPages().add();

        //Set location and size
        float x = 150;
        float y = 100;
        float width = 300;


        //Create pens
        PdfPen pen = new PdfPen(new PdfRGBColor(Color.red), 3f);
        PdfPen pen1 = new PdfPen(new PdfRGBColor(Color.blue), 1f);

        //Set dash style and pattern
        pen.setDashStyle(PdfDashStyle.Dash);
        pen.setDashPattern(new float[]{1, 1, 1});

        //Draw lines to the PDF page
        page.getCanvas().drawLine(pen, x, y, x + width, y);
        page.getCanvas().drawLine(pen1, x, y+50, x + width, y+50);

        //Save the document
        pdf.saveToFile("output/drawLines_out.pdf");
    }
}

Effective screenshot after adding dash and solid lines to PDF:

Java draw dash and solid line to PDF

Tuesday, 26 February 2019 07:48

Create Spot Color in PDF in Java

Written by support iceblue

This article will demonstrate how to create spot color to PDF file using Spire.PDF in Java.

import com.spire.pdf.*;
import com.spire.pdf.colorspace.*;
import com.spire.pdf.graphics.*;
import java.awt.*;
import java.awt.geom.Point2D;


public class SpotColor {

    public static void main(String[] args) {

        //Create a PdfDocument instance
        PdfDocument pdf = new PdfDocument();

        //Add a page
        PdfPageBase page = pdf.getPages().add();

        //Define the spot color "MySpotColor" from the built-in color.
        PdfRGBColor pdfRGBColor = new PdfRGBColor(new Color(148,0,211));
        PdfSeparationColorSpace cs = new PdfSeparationColorSpace("MySpotColor",pdfRGBColor);

        //Apply the spot color while drawing content on the page.
        PdfSeparationColor color = new PdfSeparationColor(cs, 1f);
        PdfSolidBrush brush = new PdfSolidBrush(color);
        page.getCanvas().drawString("Tint=1.0", new PdfFont(PdfFontFamily.Helvetica, 10f), brush, new Point2D.Float(160, 160));

        //Draw pie with spot color(DarkViolet)
        page.getCanvas().drawPie(brush, 148, 200, 60, 60, 360, 360);

        page.getCanvas().drawString("Tint=0.7", new PdfFont(PdfFontFamily.Helvetica, 10f), brush, new Point2D.Float(230, 160));
        color = new PdfSeparationColor(cs, 0.7f);
        brush = new PdfSolidBrush(color);
        page.getCanvas().drawPie(brush, 218, 200, 60, 60, 360, 360);

        page.getCanvas().drawString("Tint=0.4", new PdfFont(PdfFontFamily.Helvetica, 10f), brush, new Point2D.Float(300, 160));
        color = new PdfSeparationColor(cs, 0.4f);
        brush = new PdfSolidBrush(color);
        page.getCanvas().drawPie(brush, 288, 200, 60, 60, 360, 360);

        page.getCanvas().drawString("Tint=0.1", new PdfFont(PdfFontFamily.Helvetica, 10f), brush, new Point2D.Float(370, 160));
        color = new PdfSeparationColor(cs, 0.1f);
        brush = new PdfSolidBrush(color);
        page.getCanvas().drawPie(brush, 358, 200, 60, 60, 360, 360);


        //Save the document
        pdf.saveToFile("output/drawContentWithSpotColor.pdf");

    }
}

Effective screenshot after adding spot color in PDF in Java application:

Create Spot Color in PDF in Java

Monday, 15 October 2018 05:47

Draw Shapes in PDF in Java

Written by support iceblue

This tutorial will show you how to use Spire. PDF for Java to draw shapes in a PDF document, as well as set shape border colors and fill colors.

Entire Code:

import java.awt.Color;
import java.awt.geom.Rectangle2D;

import com.spire.ms.System.Drawing.PointF;
import com.spire.pdf.graphics.*;


public class DrawShapes {

	public static void main(String[] args) {
		
		//create a PdfDocument object
        PdfDocument doc = new PdfDocument();
        
        //add a page
        PdfPageBase page = doc.getPages().add();      
        
        //draw a rectangle
        PdfPen pen =new PdfPen(new PdfRGBColor(Color.black),0.1);
        Rectangle2D.Float rect1 = new Rectangle2D.Float(0, 20, 120, 50);
        PdfLinearGradientBrush linearGradientBrush = new PdfLinearGradientBrush(rect1,new PdfRGBColor(Color.white),new PdfRGBColor(Color.blue),PdfLinearGradientMode.Horizontal);          
        page.getCanvas().drawRectangle(pen, linearGradientBrush, rect1);
        
        //draw an ellipse
        PointF centerStart= new PointF(205,45);
        PointF centerEnd= new PointF(205,45);
        PdfRadialGradientBrush radialGradientBrush = new PdfRadialGradientBrush(centerStart,0,centerEnd,25,new PdfRGBColor(Color.pink),new PdfRGBColor(Color.red));
        Rectangle2D.Float rect2= new Rectangle2D.Float(180, 20, 50, 50);
        page.getCanvas().drawEllipse(pen,radialGradientBrush,rect2);
        
        //draw a polygon
        PointF p1=new PointF(290,70);
        PointF p2=new PointF(310,45);
        PointF p3=new PointF(325,60);
        PointF p4=new PointF(340,20);
        PointF p5=new PointF(370,70);
        PointF[] points = {p1, p2, p3, p4, p5};
        PdfBrush brush= PdfBrushes.getGreenYellow();
        page.getCanvas().drawPolygon(pen,brush, points);
        
        //draw an arc
        float startAngle = 0;
        float sweepAngle = 270;
        Rectangle2D.Float rect3= new Rectangle2D.Float(0, 110, 50, 50);
        page.getCanvas().drawArc(pen, rect3, startAngle, sweepAngle);
        
        //draw a pie graph   
        Rectangle2D.Float rect4= new Rectangle2D.Float(70, 110, 50, 50);
        page.getCanvas().drawPie(pen, rect4, startAngle, sweepAngle);
        
        //draw a line
        PointF pStart=new PointF(205,110);
        PointF pEnd=new PointF(205,160);
        page.getCanvas().drawLine(pen, pStart, pEnd);
  
        //draw a Bezier curve
        PointF startPoint = new PointF(290, 135);
        PointF firstControlPoint = new PointF(330, 70);
        PointF secondControlPoint = new PointF(330, 200);
        PointF endPoint = new PointF(370, 135);
        page.getCanvas().drawBezier(pen, startPoint, firstControlPoint, secondControlPoint, endPoint);
        
        //save to file
        doc.saveToFile("DrawShapes.pdf", FileFormat.PDF);       
	}

}

Output:

Draw Shapes in PDF in Java