Skip to main content
Skip table of contents

Drawing lines


drawinglines

JAVA

JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.graphics;

import com.itextpdf.kernel.colors.Color;
import com.itextpdf.kernel.colors.DeviceCmyk;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;

import java.io.File;

public class DrawingLines {
    public static final String DEST = "./target/sandbox/graphics/drawing_lines.pdf";

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new DrawingLines().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        PdfCanvas canvas = new PdfCanvas(pdfDoc.addNewPage());

        // Create a 100% Magenta color
        Color magentaColor = new DeviceCmyk(0.f, 1.f, 0.f, 0.f);
        canvas
                .setStrokeColor(magentaColor)
                .moveTo(36, 36)
                .lineTo(36, 806)
                .lineTo(559, 36)
                .lineTo(559, 806)
                .closePathStroke();

        pdfDoc.close();
    }
}

C#

C#
using System;
using System.IO;
using iText.Kernel.Colors;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;

namespace iText.Samples.Sandbox.Graphics
{
    public class DrawingLines
    {
        public static readonly string DEST = "results/sandbox/graphics/drawing_lines.pdf";

        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();

            new DrawingLines().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            PdfCanvas canvas = new PdfCanvas(pdfDoc.AddNewPage());

            // Create a 100% Magenta color
            Color magentaColor = new DeviceCmyk(0f, 1f, 0f, 0f);
            canvas
                .SetStrokeColor(magentaColor)
                .MoveTo(36, 36)
                .LineTo(36, 806)
                .LineTo(559, 36)
                .LineTo(559, 806)
                .ClosePathStroke();

            pdfDoc.Close();
        }
    }
}
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.