Tuesday, 23 February 2021 07:30

Add multiple watermarks in presentation slides

Spire.Presentation supports to insert text watermark and image watermark to PowerPoint document. This article will show you how to use Spire.Presentation to add multiple watermarks to the presentation slides in C#/VB.NET.

C#
using Spire.Presentation;
using Spire.Presentation.Drawing;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace WatermarkDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PPT document and load file
            Presentation presentation = new Presentation();
            presentation.LoadFromFile("Sample.pptx");

            //Get the size of the watermark string
            Font font = new Font("Arial", 20);
            String watermarkText = "E-iceblue";
            SizeF size = TextRenderer.MeasureText("E-iceblue", font);
            float x = 30;
            float y = 80;
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    //Define a rectangle range
                    RectangleF rect = new RectangleF(x, y, size.Width, size.Height);

                    //Add a rectangle shape with a defined range
                    IAutoShape shape = presentation.Slides[0].Shapes.AppendShape(Spire.Presentation.ShapeType.Rectangle, rect);

                    //Set the style of the shape
                    shape.Fill.FillType = FillFormatType.None;
                    shape.ShapeStyle.LineColor.Color = Color.White;
                    shape.Rotation = -45;
                    shape.Locking.SelectionProtection = true;
                    shape.Line.FillType = FillFormatType.None;

                    //Add text to the shape
                    shape.TextFrame.Text = watermarkText;
                    TextRange textRange = shape.TextFrame.TextRange;
                    //Set the style of the text range
                    textRange.Fill.FillType = FillFormatType.Solid;
                    textRange.Fill.SolidColor.Color = Color.FromArgb(120, Color.HotPink);
                    textRange.EastAsianFont = new TextFont(font.Name);
                    textRange.FontHeight = font.Size;

                    x += (100 + size.Width);
                }
                x = 30;
                y += (100 + size.Height);
            }

            //Save the document
            presentation.SaveToFile("Watermark_result.pptx", FileFormat.Pptx2010);

        }
    }
}
VB.NET
Imports Spire.Presentation
Imports Spire.Presentation.Drawing
Imports System
Imports System.Drawing
Imports System.Windows.Forms

Namespace WatermarkDemo
    
    Class Program
        
        Private Shared Sub Main(ByVal args() As String)
            'Create a PPT document and load file
            Dim presentation As Presentation = New Presentation
            presentation.LoadFromFile("Sample.pptx")
            'Get the size of the watermark string
            Dim font As Font = New Font("Arial", 20)
            Dim watermarkText As String = "E-iceblue"
            Dim size As SizeF = TextRenderer.MeasureText("E-iceblue", font)
            Dim x As Single = 30
            Dim y As Single = 80
            Dim i As Integer = 0
            Do While (i < 3)
                Dim j As Integer = 0
                Do While (j < 3)
                    'Define a rectangle range
                    Dim rect As RectangleF = New RectangleF(x, y, size.Width, size.Height)
                    'Add a rectangle shape with a defined range
                    Dim shape As IAutoShape = presentation.Slides(0).Shapes.AppendShape(Spire.Presentation.ShapeType.Rectangle, rect)
                    'Set the style of the shape
                    shape.Fill.FillType = FillFormatType.None
                    shape.ShapeStyle.LineColor.Color = Color.White
                    shape.Rotation = -45
                    shape.Locking.SelectionProtection = true
                    shape.Line.FillType = FillFormatType.None
                    'Add text to the shape
                    shape.TextFrame.Text = watermarkText
                    Dim textRange As TextRange = shape.TextFrame.TextRange
                    'Set the style of the text range
                    textRange.Fill.FillType = FillFormatType.Solid
                    textRange.Fill.SolidColor.Color = Color.FromArgb(120, Color.HotPink)
                    textRange.EastAsianFont = New TextFont(font.Name)
                    textRange.FontHeight = font.Size
                    x = (x + (100 + size.Width))
                    j = (j + 1)
                Loop
                
                x = 30
                y = (y + (100 + size.Height))
                i = (i + 1)
            Loop
            
            'Save the document
            presentation.SaveToFile("Watermark_result.pptx", FileFormat.Pptx2010)
        End Sub
    End Class
End Namespace

Output:

Add multiple watermarks in presentation slides

This article demonstrates how to apply a shadow effect to the text in a PowerPoint slide using Spire.Presentation for Java.

import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import com.spire.presentation.drawing.OuterShadowEffect;

import java.awt.*;
import java.awt.geom.Rectangle2D;

public class SetShadowEffect {

    public static void main(String[] args) throws Exception {

        //Create a Presentation object
        Presentation presentation = new Presentation();
        presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);

        //Get the first slide
        ISlide slide = presentation.getSlides().get(0);

        //Add a rectangle to slide
        IAutoShape shape = slide.getShapes().appendShape(ShapeType.RECTANGLE,new Rectangle2D.Float(50,80,500,100));
        shape.getFill().setFillType(FillFormatType.NONE);
        shape.getLine().setFillType(FillFormatType.NONE);

        //Set text of the shape
        shape.appendTextFrame("Text shading on slide");

        //Set font style
        shape.getTextFrame().getTextRange().setFontHeight(38f);
        shape.getTextFrame().getTextRange().setLatinFont(new TextFont("Arial Black"));
        shape.getTextFrame().getTextRange().getFill().setFillType(FillFormatType.SOLID);
        shape.getTextFrame().getTextRange().getFill().getSolidColor().setColor(Color.BLACK);

        //Create a OuterShadowEffect object
        OuterShadowEffect outerShadow= new OuterShadowEffect();

        //Set the shadow effect
        outerShadow.setBlurRadius(0);
        outerShadow.setDirection(50);
        outerShadow.setDistance(10);
        outerShadow.getColorFormat().setColor(Color.orange);

        //Apply shadow effect to text
        shape.getTextFrame().getTextRange().getEffectDag().setOuterShadowEffect(outerShadow);

        //Save to file
        presentation.saveToFile("output/AddShadow.pptx", FileFormat.PPTX_2013);
    }
}

Apply a Shadow Effect to Text in PowerPoint in Java

This article will demonstrate how to use Spire.XLS for Java to remove the formulas but keep the values on the Excel worksheet.

Firstly, view the original Excel:

Java remove the formulas but keep the values on Excel worksheet

import com.spire.xls.*;

public class Test {
    public static void main(String[] args) throws Exception {

        String inputFile = "Sample.xlsx";
        String outputFile="output/removeFormulasButKeepValues_result.xlsx";

        //Create a workbook.
        Workbook workbook = new Workbook();
        //Load the file from disk.
        workbook.loadFromFile(inputFile);
        //Loop through worksheets.
        for (Worksheet sheet : (Iterable) workbook.getWorksheets())
        {
            //Loop through cells.
            for (CellRange cell : (Iterable) sheet.getRange())
            {
                //If the cell contains formula, get the formula value, clear cell content, and then fill the formula value into the cell.
                if (cell.hasFormula())
                {
                    Object value = cell.getFormulaValue();
                    cell.clear(ExcelClearOptions.ClearContent);
                    cell.setValue(value.toString());
                }
            }
        }
        //Save to file
        workbook.saveToFile(outputFile, ExcelVersion.Version2013);
    }
}

Output:

Java remove the formulas but keep the values on Excel worksheet

This article demonstrates how to detect whether a PDF file is encrypted or not by using Spire.PDF for .NET. Spire.PDF offers a method named IsPasswordProtected(string fileName) which returns a boolean value. If the value is true, means the PDF is encrypted with password, otherwise it's not.

C#
using Spire.Pdf;
using System;

namespace PdfDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            string fileName = "Sample.pdf";
            bool value = PdfDocument.IsPasswordProtected(fileName);

            Console.WriteLine(value);
            Console.ReadKey();

        }
    }
}
VB.NET
Imports Spire.Pdf
Imports System

Namespace PdfDemo
    
    Class Program
        
        Private Shared Sub Main(ByVal args() As String)
            Dim fileName As String = "Sample.pdf"
            Dim value As Boolean = PdfDocument.IsPasswordProtected(fileName)
            Console.WriteLine(value)
            Console.ReadKey
        End Sub
    End Class
End Namespace

After running the project, we get the Output that shows the PDF file is password protected:

Detect if the PDF file is password protected in C#

We have demonstrated how to use Spire.Doc for Java to add multiple text watermarks to word document. This article will show you how to add multiple image watermarks to the Word document with the help of Spire.Doc for Java.

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.HeaderFooter;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.TextWrappingStyle;
import com.spire.doc.fields.DocPicture;

public class WordImageWatermark {

    public static void main(String[] args) throws Exception {
        //Load the sample file
        Document doc=new Document();
        doc.loadFromFile("Sample.docx");
        //Load the image
        DocPicture picture = new DocPicture(doc);
        picture.loadImage("Logo.png");

        //Set the text wrapping style
        picture.setTextWrappingStyle(TextWrappingStyle.Behind);

        for (int n = 0; n < doc.getSections().getCount(); n++) {
            Section section = doc.getSections().get(n);
            //Get the head of section
            HeaderFooter header = section.getHeadersFooters().getHeader();
            Paragraph paragrapg1;
            if(header.getParagraphs().getCount()>0){
                paragrapg1=header.getParagraphs().get(0);

            }else {
                //Add the header to the paragraph
                paragrapg1 = header.addParagraph();
            }

            for (int p = 0; p < 3; p++) {

                for (int q = 0; q < 2; q++) {
                    //copy the image and add it to many places
                    picture = (DocPicture)picture.deepClone();
                    picture.setVerticalPosition(100 + 200 * p);
                    picture.setHorizontalPosition(50 + 210 * q);
                    paragrapg1.getChildObjects().add(picture);
                }
            }
        }
        //Save the document to file
        doc.saveToFile("Result.docx", FileFormat.Docx_2013);
    }
}

Output:

Java add multiple image watermarks to Word document

Monday, 01 February 2021 05:42

Get Coordinates of Images in PDF in Java

This article shows you how to get x and y coordinates of images in a PDF page by using Spire.PDF for Java.

import com.spire.pdf.exporting.PdfImageInfo;
import java.awt.geom.Rectangle2D;

public class GetCoordinateOfImage {

    public static void main(String[] args) {

        //Create a PdfDocument object
        PdfDocument doc = new PdfDocument();

        //Load a PDF file
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Images.pdf");

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

        //Get the image information of the page
        PdfImageInfo[] imageInfo = page.getImagesInfo();

        //Loop through the image information
        for (int i = 0; i < imageInfo.length; i++) {

            //Get the bounds property of a specific image
            Rectangle2D rect = imageInfo[i].getBounds();

            //Get the x and y coordinates
            System.out.println(String.format("The coordinate of image %d:(%f, %f)", i+1, rect.getX(), rect.getY()));
        }
    }
}

Get Coordinates of Images in PDF in Java

Monday, 25 January 2021 09:03

Java remove annotations from PDF

We have demonstrated how to use Spire.PDF for java to add annotation to PDF document. This article will demonstrate how to delete annotations from PDF document.

Firstly, view the sample PDF with annotations:

Java remove annotations from PDF

Spire.PDF supports to delete a specified annotation and it also supports to clear all the annotations at one time.

import com.spire.pdf.*;

public class deleteAnnotation {
    public static void main(String[] args) throws Exception {
        //Load the sample PDF file
        PdfDocument document = new PdfDocument();
        document.loadFromFile("annotations.pdf");
        
        ////Remove the first annotation from the first page of PDF
        //document.getPages().get(0).getAnnotationsWidget().removeAt(0);
 
        //Remove all annotations
        document.getPages().get(0).getAnnotationsWidget().clear();

        String result = "output/deleteAllAnnotations_out.pdf";

        //Save the document
        document.saveToFile(result);
    }
}

Effective screenshot after delete all the annotations:

Java remove annotations from PDF

Thursday, 21 January 2021 07:31

Insert table to Text Box in Word in Java

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).setWidth(70);
            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().setBackColor(new Color(176,224,238));
        }
        //Apply style to the table
        table.applyStyle(DefaultTableStyle.Table_Grid_5);
        //Save the document
        String output = "output/insertTableIntoTextBox.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

Monday, 11 January 2021 07:13

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

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

We have introduced how to compare two Word documents in C# and VB.NET. From Spire.Doc V8.12.14, it supports to get the differences between two Word documents in a structure list. This article will show you how to use Spire.Doc to get the differences by comparing two Word documents.

C#
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using Spire.Doc.Formatting.Revisions;
using System;

namespace GetWordDifferences
    {
    class Program
    {
        static void Main(string[] args)

        {
            //Load the first Word document
            Document doc1 = new Document();
            doc1.LoadFromFile("Sample1.docx");

            //Load the second Word document
            Document doc2 = new Document();
            doc2.LoadFromFile("Sample2.docx");

            //Compare the two Word documents
            doc1.Compare(doc2, "Author");

            foreach (Section sec in doc1.Sections)
            {
                foreach (DocumentObject docItem in sec.Body.ChildObjects)
                {
                    if (docItem is Paragraph)
{
                        Paragraph para = docItem as Paragraph;
                        if (para.IsInsertRevision)
                        { 
                            EditRevision insRevison = para.InsertRevision;
                            EditRevisionType insType = insRevison.Type; 
                            string insAuthor = insRevison.Author; 
                            DateTime insDateTime = insRevison.DateTime; 
                        }

                        else if (para.IsDeleteRevision)
                        { 
                            EditRevision delRevison = para.DeleteRevision; 
                            EditRevisionType delType = delRevison.Type; 
                            string delAuthor = delRevison.Author; 
                            DateTime delDateTime = delRevison.DateTime; 
                        }

                        foreach (ParagraphBase paraItem in para.ChildObjects)
                        {
                            if (paraItem.IsInsertRevision)
                            { 
                                EditRevision insRevison = paraItem.InsertRevision; 
                                EditRevisionType insType = insRevison.Type; 
                                string insAuthor = insRevison.Author; 
                                DateTime insDateTime = insRevison.DateTime; 
                            }

                            else if (paraItem.IsDeleteRevision)
                            { 
                                EditRevision delRevison = paraItem.DeleteRevision; 
                                EditRevisionType delType = delRevison.Type; 
                                string delAuthor = delRevison.Author; 
                                DateTime delDateTime = delRevison.DateTime; 
                            }

                        }
                    }
                }
            }

            //Get the difference about revisions
            DifferRevisions differRevisions = new DifferRevisions(doc1);
            var insetRevisionsList = differRevisions.InsertRevisions;
            var deletRevisionsList = differRevisions.DeleteRevisions;      
        }
    }
 }
VB.NET
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Imports Spire.Doc.Formatting.Revisions
Imports System

Namespace GetWordDifferences
    
    Class Program
        
        Private Shared Sub Main(ByVal args() As String)
            'Load the first Word document
            Dim doc1 As Document = New Document
            doc1.LoadFromFile("Sample1.docx")
            'Load the second Word document
            Dim doc2 As Document = New Document
            doc2.LoadFromFile("Sample2.docx")
            'Compare the two Word documents
            doc1.Compare(doc2, "Author")
            For Each sec As Section In doc1.Sections
                For Each docItem As DocumentObject In sec.Body.ChildObjects
                    If (TypeOf docItem Is Paragraph) Then
                        Dim para As Paragraph = CType(docItem,Paragraph)
                        If para.IsInsertRevision Then
                            Dim insRevison As EditRevision = para.InsertRevision
                            Dim insType As EditRevisionType = insRevison.Type
                            Dim insAuthor As String = insRevison.Author
                            Dim insDateTime As DateTime = insRevison.DateTime
                        ElseIf para.IsDeleteRevision Then
                            Dim delRevison As EditRevision = para.DeleteRevision
                            Dim delType As EditRevisionType = delRevison.Type
                            Dim delAuthor As String = delRevison.Author
                            Dim delDateTime As DateTime = delRevison.DateTime
                        End If
                        
                        For Each paraItem As ParagraphBase In para.ChildObjects
                            If paraItem.IsInsertRevision Then
                                Dim insRevison As EditRevision = paraItem.InsertRevision
                                Dim insType As EditRevisionType = insRevison.Type
                                Dim insAuthor As String = insRevison.Author
                                Dim insDateTime As DateTime = insRevison.DateTime
                            ElseIf paraItem.IsDeleteRevision Then
                                Dim delRevison As EditRevision = paraItem.DeleteRevision
                                Dim delType As EditRevisionType = delRevison.Type
                                Dim delAuthor As String = delRevison.Author
                                Dim delDateTime As DateTime = delRevison.DateTime
                            End If
                            
                        Next
                    End If
                    
                Next
            Next
            'Get the difference about revisions
            Dim differRevisions As DifferRevisions = New DifferRevisions(doc1)
            Dim insetRevisionsList = differRevisions.InsertRevisions
            Dim deletRevisionsList = differRevisions.DeleteRevisions
        End Sub
    End Class
End Namespace
Page 2 of 132