We have demonstrated how to use Spire.Doc for Java to add text watermark and image watermark to word document. This article will show you how to add WordArt to the Word header to get the multiple watermarks on the Word document:

Insert multiple text watermarks to Word

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.ShapeLineStyle;
import com.spire.doc.documents.ShapeType;
import com.spire.doc.fields.ShapeObject;
import java.awt.*;

public class WordWatermark {

    public static void main(String[] args) {

        //Load the sample document
        Document doc = new Document();
        doc.loadFromFile("Sample.docx");
        //Add WordArt shape and set the size
        ShapeObject shape = new ShapeObject(doc, ShapeType.Text_Plain_Text);
        shape.setWidth(60);
        shape.setHeight(20);
        //Set the text, position and sytle for the wordart
        shape.setVerticalPosition(30);
        shape.setHorizontalPosition(20);
        shape.setRotation(315);
        shape.getWordArt().setText("Confidential");
        shape.setFillColor(Color.red);
        shape.setLineStyle(ShapeLineStyle.Single);
        shape.setStrokeColor(new Color(192, 192, 192, 255));
        shape.setStrokeWeight(1);

        Section section;
        HeaderFooter header;
        for (int n = 0; n < doc.getSections().getCount(); n++) {
            section = doc.getSections().get(n);
            //Get the header of section
            header = section.getHeadersFooters().getHeader();
            Paragraph paragraph1;
            for (int i = 0; i < 4; i++) {
                //Add the hearder to the paragraph
                paragraph1 = header.addParagraph();
                for (int j = 0; j < 3; j++) {
                    //copy the word are and add it to many places
                    shape = (ShapeObject) shape.deepClone();
                    shape.setVerticalPosition(50 + 150 * i);
                    shape.setHorizontalPosition(20 + 160 * j);
                    paragraph1.getChildObjects().add(shape);
                }
            }
        }
        //Save the document to file
        doc.saveToFile("Result.docx", FileFormat.Docx_2013);

    }
}

Effective screenshot:

Java insert multiple text watermarks to Word document

Tuesday, 29 September 2020 08:40

Convert HTML to PDF in Java

This article will demonstrate how to use Spire.Doc for Java to convert HTML string and HTML file to PDF in Java applications.

HTML String to PDF

import com.spire.doc.*;
import java.io.*;

public class htmlStringToWord {

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

        String inputHtml = "InputHtml.txt";
        //Create a new document
        Document document = new Document();
        //Add a section
        Section sec = document.addSection();

        String htmlText = readTextFromFile(inputHtml);
        //add a paragraph and append html string.
        sec.addParagraph().appendHTML(htmlText);

        //Save to PDF
        document.saveToFile("HTMLstringToPDF.pdf", FileFormat.PDF);
    }
    public static String readTextFromFile(String fileName) throws IOException{
        StringBuffer sb = new StringBuffer();
        BufferedReader br = new BufferedReader(new FileReader(fileName));
        String content = null;
        while ((content = br.readLine()) != null) {
            sb.append(content);
        }
        return sb.toString();
    }
}

Java Convert HTML to PDF

Convert HTML file to PDF

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

public class htmlFileToWord {

    public static void main(String[] args) throws Exception {
        // Load the sample HTML file
        Document document = new Document();
        document.loadFromFile("InputHtmlFile.html", FileFormat.Html, XHTMLValidationType.None);

        //Save to file
        document.saveToFile("Result.pdf",FileFormat.PDF);
    }
}

Effective screenshot:

Java Convert HTML to PDF

Monday, 28 September 2020 01:49

Set Dpi when Converting Excel to Image in Java

This article demonstrates how to set dpi on x and y axis when converting an Excel worksheet to an image using Spire.XLS for Java.

import com.spire.xls.*;

public class ConvertExcelToImage {

    public static void main(String[] args) {

        //Create a Workbook object
        Workbook wb = new Workbook();

        //Load an Excel file
        wb.loadFromFile("C:\\Users\\Administrator\\Desktop\\data.xlsx");

        //Set dpi on x and y axis
        wb.getConverterSetting().setXDpi(300);
        wb.getConverterSetting().setYDpi(300);

        //Declare a Worksheet variable
        Worksheet sheet;

        //Loop through the worksheets 
        for (int i = 0; i < wb.getWorksheets().size(); i++) {

            //Get the specific worksheet
            sheet = wb.getWorksheets().get(i);
            
            //Convert worksheet to image 
            sheet.saveToImage("C:\\Users\\Administrator\\Desktop\\Output\\image-" + i + ".png");
        }
    }
}

Set Dpi when Converting Excel to Image in Java

Sunday, 27 September 2020 07:14

Ungroup Shapes in PowerPoint in C#, VB.NET

This article demonstrates how to ungroup grouped shapes in a PowerPoint document using Spire.Presentation for .NET.

The input PowerPoint document:

Ungroup Shapes in PowerPoint in C#, VB.NET

C#
using Spire.Presentation;

namespace UngroupShapes
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Presentation instance
            Presentation ppt = new Presentation();
            //Load the PowerPoint document
            ppt.LoadFromFile("Sample.pptx");

            //Get the first slide
            ISlide slide = ppt.Slides[0];

            //Loop through the shapes in the slide
            for(int i = 0; i< slide.Shapes.Count;i++)
            {
                IShape shape = slide.Shapes[i];
                //Detect if the shape is a grouped shape
                if (shape is GroupShape)
                {
                    GroupShape groupShape = shape as GroupShape;
                    //Ungroup the grouped shape
                    slide.Ungroup(groupShape);
                }
            }

            //Save the resultant document
            ppt.SaveToFile("UngroupShapes.pptx", FileFormat.Pptx2013);
        }
    }
}
VB.NET
Imports Spire.Presentation

Namespace UngroupShapes
    Class Program
        Private Shared Sub Main(ByVal args As String())
            Dim ppt As Presentation = New Presentation()
            ppt.LoadFromFile("Sample.pptx")
            Dim slide As ISlide = ppt.Slides(0)

            For i As Integer = 0 To slide.Shapes.Count - 1
                Dim shape As IShape = slide.Shapes(i)

                If TypeOf shape Is GroupShape Then
                    Dim groupShape As GroupShape = TryCast(shape, GroupShape)
                    slide.Ungroup(groupShape)
                End If
            Next

            ppt.SaveToFile("UngroupShapes.pptx", FileFormat.Pptx2013)
        End Sub
    End Class
End Namespace

The output PowerPoint document after ungrouping shapes:

Ungroup Shapes in PowerPoint in C#, VB.NET

Tuesday, 15 August 2023 07:18

Java: Convert SVG to PDF

SVG files are vector-based graphics that can be scaled and resized without losing quality. While they can be very useful in certain scenarios, there may still be times when you need to convert them to PDFs for further processing, sharing, distributing, printing, or archiving. In this article, you will learn how to programmatically convert SVG images to PDF files 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>10.4.4</version>
    </dependency>
</dependencies>
    

Convert SVG to PDF in Java

Spire.PDF for Java offers the PdfDocument.loadFromSvg() method to load an SVG file directly, and you can then convert it to a PDF file through the PdfDocument.saveToFile() method. The following are the detailed steps.

  • Create a PdfDocument instance.
  • Load a sample SVG file using PdfDocument.loadFromSvg() method.
  • Convert the SVG file to PDF using PdfDocument.saveToFile(String filename, FileFormat.PDF) method.
  • Java
import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;

public class SVGToPDF {
    public static void main(String[] args) {
        //Create a PdfDocument instance
        PdfDocument pdf = new PdfDocument();

        //Load a sample SVG file
        pdf.loadFromSvg("sample.svg");

        //Save as PDF file
        pdf.saveToFile("SVGToPDF.pdf", FileFormat.PDF);
    }
}

Java: Convert SVG to PDF

Add SVG Images to PDF in Java

In addition to converting SVG to PDF, Spire.PDF for Java also supports adding SVG images to PDF files. During the process, you are allowed to set the position and size of the SVG image. The following are the detailed steps.

  • Create a PdfDocument instance and load an SVG file using PdfDocument.loadFromSvg() method.
  • Create a template based on the content of the SVG file using PdfDocument.getPages().get().createTemplate() method.
  • Create another PdfDocument object and load a PDF file using PdfDocument.loadFromFile() method.
  • Get a specified page in the PDF file using PdfDocument.getPages().get() method.
  • Draw the template with a custom size at a specified location on the PDF page using PdfPageBase.getCanvas().drawTemplate() method.
  • Save the result PDF file using PdfDocument.saveToFile() method.
  • Java
import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.graphics.PdfTemplate;

import java.awt.*;
import java.awt.geom.Point2D;

public class AddSVGImagetoPDF {
    public static void main(String[] args) {
        //Create a PdfDocument instance
        PdfDocument doc1 = new PdfDocument();

        //Load an SVG file
        doc1.loadFromSvg("Image.svg");

        //Create a template based on the content of the SVG file
        PdfTemplate template = doc1.getPages().get(0).createTemplate();

        //Create another PdfDocument instance
        PdfDocument doc2 = new PdfDocument();

        //Load a PDF file
        doc2.loadFromFile("Intro.pdf");

        //Draw the template with a custom size at a specified location in the PDF file
        doc2.getPages().get(0).getCanvas().drawTemplate(template, new Point2D.Float(100,200), new Dimension(400,280) );

        //Save the result file
        doc2.saveToFile("AddSVGtoPDF.pdf", FileFormat.PDF);
        doc1.close();
        doc2.close();
    }
}

Java: Convert SVG to PDF

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.

This article demonstrates how to add shadow effects to shapes in PowerPoint by using Spire.Presentation for Java. In addition to the PresetShadowEffect shown in this article, you can also add InnerShadowEffect, OuterShadowEffect and SoftEdgeEffect, etc.

import com.spire.presentation.*;
import com.spire.presentation.drawing.*;
import java.awt.*;
import java.awt.geom.Rectangle2D;

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

        //Create a Presentation instance
        Presentation ppt = new Presentation();

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

        //Add a shape to the slide.
        Rectangle2D rect = new Rectangle2D.Float(120, 100, 150, 300);
        IAutoShape shape = slide.getShapes().appendShape(ShapeType.RECTANGLE, rect);

        //Fill the shape with a picture
        shape.getFill().setFillType(FillFormatType.PICTURE);
        shape.getFill().getPictureFill().getPicture().setUrl("https://cdn.e-iceblue.com/C:\\Users\\Administrator\\Desktop\\img.png");
        shape.getLine().setFillType(FillFormatType.NONE);
        shape.getFill().getPictureFill().setFillType(PictureFillType.STRETCH);

        //Set shadow effect
        PresetShadow presetShadow = new PresetShadow();
        presetShadow.setPreset(PresetShadowValue.BACK_RIGHT_PERSPECTIVE);
        presetShadow.getColorFormat().setColor(Color.lightGray);

        //Apply the shadow effect to shape
        shape.getEffectDag().setPresetShadowEffect(presetShadow);

        //Save the document
        ppt.saveToFile("output/ShapeShadow.pptx", FileFormat.PPTX_2013);
    }
}

Add Shadow Effects to Shapes in PowerPoint in Java

We have demonstrated how to use PresentationPrintDocument to print the presentation slides. This article will show you how to use PrinterSettings to print Presentation slides in Java applications.

import com.spire.ms.Printing.*;
import com.spire.presentation.*;
public class PrintPPT {

    public static void main(String[] args) throws Exception {
        //Load the sample document
        Presentation presentation = new Presentation();
        presentation.loadFromFile("Sample.pptx");

        //Use PrinterSettings object to print presentation slides
        PrinterSettings ps = new PrinterSettings();
        ps.setPrintRange(PrintRange.AllPages);
        //ps.setPrintToFile(true);

        //Print the slide with frame
        presentation.setSlideFrameForPrint(true);

        //Print the slide with Grayscale
        presentation.setGrayLevelForPrint(true);

        //Print 4 slides horizontal
        presentation.setSlideCountPerPageForPrint(PageSlideCount.Four);
        presentation.setOrderForPrint(Order.Horizontal);

        //Only select some slides to print
        presentation.SelectSlidesForPrint("1", "3");

        //Print the document
        presentation.print(ps);
        presentation.dispose();
    }
}

This article demonstrates how to detect merged cells in an Excel worksheet and unmerge the merged cells using Spire.XLS for Java.

The input Excel file:

Detect Merged Cells in an Excel Worksheet in Java

import com.spire.xls.CellRange;
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

public class DetectMergedCells {
    public static void main(String[] args) throws Exception {
        //Create a Workbook instance
        Workbook workbook = new Workbook();
        //Load the Excel file
        workbook.loadFromFile( "Input.xlsx");

        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Get the merged cell ranges in the first worksheet and put them into a CellRange array
        CellRange[] range = sheet.getMergedCells();

        //Traverse through the array and unmerge the merged cells
        for(CellRange cell : range){
            cell.unMerge();
        }
        
        //Save the result file
        workbook.saveToFile("DetectMergedCells.xlsx", ExcelVersion.Version2013);
    }
}

The output Excel file:

Detect Merged Cells in an Excel Worksheet in Java

Wednesday, 16 September 2020 06:52

Remove Section in PowerPoint in C#, VB.NET

This article demonstrates how to remove a specific section or all the sections but keep the slide(s) in the section(s) in PowerPoint by using Spire.Presentation for .NET.

Below is the screenshot of the input PowerPoint document which contains two sections:

Remove Section in PowerPoint in C#, VB.NET

C#
//Create a Presentation instance
Presentation ppt = new Presentation();
//Load a PowerPoint document
ppt.LoadFromFile("AddSection.pptx");

//Remove the second section
ppt.SectionList.RemoveAt(1);

//Remove all the sections
//ppt.SectionList.RemoveAll();

//Save the result document
ppt.SaveToFile("RemoveSection.pptx", FileFormat.Pptx2013);
VB.NET
'Create a Presentation instance
Dim ppt As Presentation = New Presentation
'Load a PowerPoint document
ppt.LoadFromFile("AddSection.pptx")
'Remove the second section
ppt.SectionList.RemoveAt(1)
'Remove all the sections
'ppt.SectionList.RemoveAll()
'Save the result document
ppt.SaveToFile("RemoveSection.pptx", FileFormat.Pptx2013)

The output PowerPoint document after removing the second section:

Remove Section in PowerPoint in C#, VB.NET

Hyperlinks are useful features in Excel documents, providing quick access to other relevant resources such as websites, email addresses, or specific cells within the same workbook. However, sometimes you may want to modify or delete existing hyperlinks for various reasons, such as updating broken links, correcting typos, or removing outdated information. In this article, we will demonstrate how to modify or delete hyperlinks in Excel in Java using Spire.XLS for Java library.

Install Spire.XLS for Java

First of all, you're required to add the Spire.Xls.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.xls</artifactId>
        <version>14.4.1</version>
    </dependency>
</dependencies>
    

Modify Hyperlinks in Excel in Java

If there are issues with the functionality of a hyperlink caused by damage or spelling errors, you may need to modify it. The following steps demonstrate how to modify an existing hyperlink in an Excel file:

  • Create an instance of Workbook class.
  • Load an Excel file using the Workbook.loadFromFile() method.
  • Get a specific worksheet using the Workbook.getWorksheets().get() method.
  • Get the collection of all hyperlinks in the worksheet using the Worksheet.getHyperLinks() method.
  • Change the values of TextToDisplay and Address property using the HyperLinksCollection.get().setTextToDisplay() and HyperLinksCollection.get().setAddress method.
  • Save the result file using the Workbook.saveToFile() method.
  • Java
import com.spire.xls.*;
import com.spire.xls.collections.HyperLinksCollection;

public class ModifyHyperlink {
    public static void main(String[] args) {
        //Create a Workbook instance
        Workbook workbook = new Workbook();

        //Load an Excel file
        workbook.loadFromFile("Sample.xlsx");

        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Get the collection of all hyperlinks in the worksheet
        HyperLinksCollection links = sheet.getHyperLinks();

        //Change the values of TextToDisplay and Address property
        links.get(0).setTextToDisplay("Republic of Indonesia");
        links.get(0).setAddress("https://www.indonesia.travel/gb/en/home");

        //Save the document
        workbook.saveToFile("ModifyHyperlink.xlsx", ExcelVersion.Version2013);
        workbook.dispose();
    }
}

Java: Modify or Delete Hyperlinks in Excel

Delete Hyperlinks from Excel in Java

Spire.XLS for Java also offers the Worksheet.getHyperLinks().removeAt() method to remove hyperlinks. The following are the steps to delete hyperlink from Excel in Java.

  • Create an instance of Workbook class.
  • Load an Excel file using the Workbook.loadFromFile() method.
  • Get a specific worksheet using the Workbook.getWorksheets().get() method.
  • Get the collection of all hyperlinks in the worksheet using the Worksheet.getHyperLinks() method.
  • Remove a specific hyperlink and keep link text using the Worksheet.getHyperLinks().removeAt() method.
  • Save the result file using the Workbook.saveToFile() method.
  • Java
import com.spire.xls.*;
import com.spire.xls.collections.HyperLinksCollection;

public class RemoveHyperlink {
    public static void main(String[] args) {
        //Create a Workbook instance
        Workbook workbook = new Workbook();

        //Load an Excel file
        workbook.loadFromFile("Sample.xlsx");

        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Get the collection of all hyperlinks in the worksheet
        HyperLinksCollection links = sheet.getHyperLinks();

        //Remove the first hyperlink and keep link text
        sheet.getHyperLinks().removeAt(0);

        //Remove all content from the cell
        //sheet.getCellRange("A7").clearAll();

        //Save the document
        String output = "RemoveHyperlink.xlsx";
        workbook.saveToFile(output, ExcelVersion.Version2013);
        workbook.dispose();
    }
}

Java: Modify or Delete Hyperlinks in Excel

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.