page 117

PDF annotations are notes or markers added to documents, which are great for making comments, giving explanations, giving feedback, etc. Co-creators of documents often communicate with annotations. However, when the issues associated with the annotations have been dealt with or the document has been finalized, it is necessary to remove the annotations to make the document more concise and professional. This article shows how to delete PDF annotations programmatically using Spire.PDF for Java.

Install Spire.PDF for Java

First of all, you're required to add the Spire.Pdf.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf</artifactId>
        <version>11.11.11</version>
    </dependency>
</dependencies>

Remove the Specified Annotation

Annotations are page-level document elements. Therefore, deleting an annotation requires getting the page where the annotation is located first, and then you can use the PdfPageBase.getAnnotationsWidget().removeAt() method to delete the annotation. The detailed steps are as follows.

  • Create a PdfDocument instance.
  • Load a PDF document using PdfDocument.loadFromFile() method.
  • Get the first page using PdfDocument.getPages().get() method.
  • Remove the first annotation from this page using PdfPageBase.getAnnotationsWidget().removeAt() method.
  • Save the document using PdfDocument.saveToFile() method.
  • Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;

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

        //Create an object of PdfDocument
        PdfDocument pdf = new PdfDocument();

        //Load a PDF document
        pdf.loadFromFile("C:/Annotations.pdf");

        //Get the first page
        PdfPageBase page = pdf.getPages().get(0);

        //Remove the first annotation
        page.getAnnotationsWidget().removeAt(0);

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

Java: Remove Annotations from PDF Documents

Remove All Annotations from a Page

Spire.PDF for Java also provides PdfPageBase.getAnnotationsWidget().clear() method to remove all annotations in the specified page. The detailed steps are as follows.

  • Create a PdfDocument instance.
  • Load a PDF document using PdfDocument.loadFromFile() method.
  • Get the first page using PdfDocument.getPages().get() method.
  • Remove all annotations from the page using PdfPageBase.getAnnotationsWidget().clear() method.
  • Save the document using PdfDocument.saveToFile() method.
  • Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;

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

        //Create an object of PdfDocument
        PdfDocument pdf = new PdfDocument();

        //Load a PDF document
        pdf.loadFromFile("C:/Annotations.pdf");

        //Get the first page
        PdfPageBase page = pdf.getPages().get(0);

        //Remove all annotations in the page
        page.getAnnotationsWidget().clear();

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

Java: Remove Annotations from PDF Documents

Remove All Annotations from a PDF Document

To remove all annotations from a PDF document, we need to loop through all pages in the document and delete all annotations from each page. The detailed steps are as follows.

  • Create a PdfDocument instance.
  • Load a PDF document using PdfDocument.loadFromFile() method.
  • Loop through all pages to delete annotations.
  • Delete annotations in each page using PdfPageBase.getAnnotationsWidget().clear() method.
  • Save the document using PdfDocument.saveToFile() method.
  • Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;

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

        //Create an object of PdfDocument
        PdfDocument pdf = new PdfDocument();

        //Load a PDF document
        pdf.loadFromFile("C:/Users/Sirion/Desktop/Annotations.pdf");

        //Loop through the pages in the document
        for (PdfPageBase page : (Iterable) pdf.getPages()) {
            //Remove annotations in each page
            page.getAnnotationsWidget().clear();
        }

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

Java: Remove Annotations from PDF Documents

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Insert table to Text Box in Word in Java

2021-01-21 07:31:39 Written by Koohji

We have demonstrated how to insert text and image to textbox in a Word document by using Spire.Doc for Java. This article will demonstrate how to insert table to textbox in Word.

import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;

import java.awt.*;

public class insertTableIntoTextBox {
    public static void main(String[] args) throws Exception{
        //Create a new document; add a section and paragraph
        Document doc = new Document();
        Section section = doc.addSection();
        Paragraph paragraph = section.addParagraph();
        //Add a textbox to the paragraph
        TextBox textbox = paragraph.appendTextBox(380, 100);
        //Set the position of the textbox
        textbox.getFormat().setHorizontalOrigin(HorizontalOrigin.Page);
        textbox.getFormat().setHorizontalPosition(140);
        textbox.getFormat().setVerticalOrigin(VerticalOrigin.Page);
        textbox.getFormat().setVerticalPosition(50);
        //Insert table to the textbox
        Table table = textbox.getBody().addTable(true);
        //Specify the number of rows and columns of the table
        table.resetCells(4, 4);
        //Define the data
        String[][] data = new String[][]
                {
                        {"Name", "Age", "Gender", "ID"},
                        {"John", "28", "Male", "0023"},
                        {"Steve", "30", "Male", "0024"},
                        {"Lucy", "26", "female", "0025"}
                };
        //Add data to the table
        for (int i = 0; i < 4; i++) {
            TableRow dataRow = table.getRows().get(i);
            dataRow.getCells().get(i).setCellWidth(70,CellWidthType.Point);
            dataRow.setHeight(22);
            dataRow.setHeightType(TableRowHeightType.Exactly);
            for (int j = 0; j < 4; j++) {
                TextRange tableRange = table.getRows().get(i).getCells().get(j).addParagraph().appendText(data[i][j]);
                tableRange.getCharacterFormat().setFontName("Arial");
                tableRange.getCharacterFormat().setFontSize(11f);
                tableRange.getOwnerParagraph().getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
                tableRange.getCharacterFormat().setBold(true);
            }
        }
        //Set the background color for the first row
        TableRow row = table.getRows().get(0);
        for (int z = 0; z < row.getCells().getCount(); z++) {
            row.getCells().get(z).getCellFormat().getShading().setBackgroundPatternColor(new Color(176,224,238));
        }
        //Apply style to the table
        table.applyStyle(DefaultTableStyle.Table_Grid_5);
        //Save the document
        String output = "output/insertTableIntoTextBox1.docx";
        doc.saveToFile(output, FileFormat.Docx_2013);
    }
}

The effective screenshot after insert table to Textbox in Word:

Insert table to Text Box in Word in Java

Add Line Numbers to a PDF in C#/VB.NET

2021-01-11 07:13:57 Written by Koohji

This article demonstrates how to add line numbers before chunks of text in a PDF page by using Spire.PDF for .NET.

Below is a screenshot of the input document.

Add Line Numbers to a PDF in C#, VB.NET

C#
using Spire.Pdf;
using Spire.Pdf.General.Find;
using Spire.Pdf.Graphics;
using System.Drawing;

namespace AddLineNumber
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument of instance
            PdfDocument doc = new PdfDocument();

            //Load a PDF document
            doc.LoadFromFile(@"G:\360MoveData\Users\Administrator\Desktop\sample.pdf");

            //Get the first page
            PdfPageBase page = doc.Pages[0];

            //Find the spcific text in the fisrt line
            PdfTextFind topLine = page.FindText("C# (pronounced See Sharp)", TextFindParameter.None).Finds[0];

            //Get the line height
            float lineHeight = topLine.Bounds.Height;

            //Get the Y coordinate of the selected text
            float y = topLine.Bounds.Y;

            //Find the spcific text in the second line
            PdfTextFind secondLine = page.FindText("language. C#", TextFindParameter.None).Finds[0];

            //Calculate the line spacing
            float lineSpacing = secondLine.Bounds.Top - topLine.Bounds.Bottom;

            //Find the specific text in the last line
            PdfTextFind bottomLine = page.FindText("allocation of objects", TextFindParameter.None).Finds[0];

            //Get the height of the chunks 
            float height = bottomLine.Bounds.Bottom;

            //Create a font
            PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 11f);

            int i = 1;
            while (y < height)
            {
                //Draw line number before a specific line of text
                page.Canvas.DrawString(i.ToString(), font, PdfBrushes.Black, new PointF(15, y));
                y += lineHeight + lineSpacing;
                i++;
            }

            //Save the document
            doc.SaveToFile("result.pdf");
        }
    }
}
VB.NET
Imports Spire.Pdf
Imports Spire.Pdf.General.Find
Imports Spire.Pdf.Graphics
Imports System.Drawing
 
Namespace AddLineNumber
    Class Program
        Shared  Sub Main(ByVal args() As String)
            'Create a PdfDocument of instance
            Dim doc As PdfDocument =  New PdfDocument() 
 
            'Load a PDF document
            doc.LoadFromFile("G:\360MoveData\Users\Administrator\Desktop\sample.pdf")
 
            'Get the first page
            Dim page As PdfPageBase =  doc.Pages(0) 
 
            'Find the spcific text in the fisrt line
            Dim topLine As PdfTextFind =  page.FindText("C# (pronounced See Sharp)",TextFindParameter.None).Finds(0) 
 
            'Get the line height
            Dim lineHeight As single =  topLine.Bounds.Height 
 
            'Get the Y coordinate of the selected text
            Dim y As single =  topLine.Bounds.Y 
 
            'Find the spcific text in the second line
            Dim secondLine As PdfTextFind =  page.FindText("language. C#",TextFindParameter.None).Finds(0) 
 
            'Calculate the line spacing
            Dim lineSpacing As single =  secondLine.Bounds.Top - topLine.Bounds.Bottom 
 
            'Find the specific text in the last line
            Dim bottomLine As PdfTextFind =  page.FindText("allocation of objects",TextFindParameter.None).Finds(0) 
 
            'Get the height of the chunks 
            Dim height As single =  bottomLine.Bounds.Bottom 
 
            'Create a font
            Dim font As PdfFont =  New PdfFont(PdfFontFamily.TimesRoman,11f) 
 
            Dim i As Integer =  1 
            While y < height
                'Draw line number before a specific line of text
                page.Canvas.DrawString(i.ToString(),font,PdfBrushes.Black,New PointF(15,y))
                y += lineHeight + lineSpacing
                i = i + 1
            End While
 
            'Save the document
            doc.SaveToFile("result.pdf")
        End Sub
    End Class
End Namespace

Output

Add Line Numbers to a PDF in C#, VB.NET

page 117

Coupon Code Copied!

Christmas Sale

Celebrate the season with exclusive savings

Save 10% Sitewide

Use Code:

View Campaign Details