Monday, 15 April 2019 08:36

Rotate a PDF page in Java

This article demonstrates how to rotate a PDF page using Spire.PDF for Java.

Below is the screenshot of the input.pdf file:

Rotate a PDF page in Java

import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.PdfPageRotateAngle;

import java.io.IOException;

public class RotatePDFPage {
    public static void main(String[] args) throws IOException {
        //Load the PDF document
        PdfDocument pdf = new PdfDocument();
        pdf.loadFromFile("Input.pdf");

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

        //Get the original rotation angle of the page
        int rotateAngle = page.getRotation().getValue();

        //Rotate the PDF page 90 degrees clockwise based on the original rotation angle
        rotateAngle += PdfPageRotateAngle.Rotate_Angle_90.getValue();
        page.setRotation((PdfPageRotateAngle.fromValue(rotateAngle)));

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

Output:

Rotate a PDF page in Java

This article demonstrates how to add, replace and delete comments in a PowerPoint document using Spire.Presentation for Java.

Add Comments:

import com.spire.presentation.*;
import java.awt.geom.Point2D;


public class PPTComment {
    public static void main(String[] args) throws Exception{
        //load the sample PowerPoint file
        Presentation ppt = new Presentation();
        ppt.loadFromFile("Sample.pptx");

        ICommentAuthor author = ppt.getCommentAuthors().addAuthor("E-iceblue", "comment:");

        //Add comment
        ppt.getSlides().get(0).addComment(author, "The first comment", new Point2D.Float(48, 9), new java.util.Date());

        ppt.getSlides().get(0).addComment(author, "The second added comment", new Point2D.Float(24, 20), new java.util.Date());

        //Save the document
        ppt.saveToFile("output/result.pptx", FileFormat.PPTX_2010);
        ppt.dispose();
    }
}

Add, Replace and Delete Comment in PowerPoint in Java

Replace and delete the comment:

public class PPTComment2{
    public static void main(String[] args) throws Exception{
        //load the sample PowerPoint file
        Presentation ppt = new Presentation();
        ppt.loadFromFile("output/result.pptx");

        //Replace comment
        ppt.getSlides().get(0).getComments()[0].setText("Replace comment");
        //Delete comment
        ppt.getSlides().get(1).deleteComment(ppt.getSlides().get(0).getComments()[1]);

        //Save the document
        ppt.saveToFile("output/result2.pptx", FileFormat.PPTX_2010);
        ppt.dispose();
    }
}

Add, Replace and Delete Comment in PowerPoint in Java

This article demonstrates how to add footer to PowerPoint document in Java application using Spire.Presentation for Java.

import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;

public class AddFooter {
    public static void main(String[] args) throws Exception {
        //Load a PPT document
        Presentation presentation = new Presentation();
        presentation.loadFromFile("Input.pptx");

        //Add footer
        presentation.setFooterText("Demo of Spire.Presentation");
        //Set the footer visible
        presentation.setFooterVisible(true);
        //Set the page number visible
        presentation.setSlideNumberVisible(true);
        //Set the date visible
        presentation.setDateTimeVisible(true);
        
        //Save the document
        presentation.saveToFile("AddFooter.pptx", FileFormat.PPTX_2010);
    }
}

Output:

Add-Footer-to-PowerPoint-Document-in-Java

Some long text, such as a phrase or a sentence, is likely to span two or more lines. When we try to highlight it, it’s required to be able to detect the whole phrase or sentence even if it spans two lines. This article is going to introduce how to find and highlight long text spanning multiple lines by using Spire.PDF with C# and VB.NET.

Code Snippets

[C#]
//load the sample PDF file
PdfDocument doc = new PdfDocument(@"C:\Users\Administrator\Desktop\Hello.pdf");

//find all matching strings from the first page, even if some of them span multiple lines
PdfTextFind[] findResults = doc.Pages[0].FindText("Besides, Spire.PDF for JAVA can be applied easily to convert PDF to XPS, XPS to PDF, PDF to SVG, and PDF to PDF/A in high quality.", TextFindParameter.CrossLine).Finds;

//highlight the first search result
findResults[0].ApplyHighLight(Color.Yellow);

//save to file
doc.SaveToFile("output.pdf", FileFormat.PDF);
[VB.NET]
'load the sample PDF file
Dim doc As PdfDocument =  New PdfDocument("C:\Users\Administrator\Desktop\Hello.pdf") 
 
'find all matching strings from the first page, even if some of them span multiple lines
Dim findResults() As PdfTextFind =  doc.Pages(0).FindText("Besides, Spire.PDF for JAVA can be applied easily to convert PDF to XPS, XPS to PDF, PDF to SVG, and PDF to PDF/A in high quality.",TextFindParameter.CrossLine).Finds 
 
'highlight the first search result
findResults(0).ApplyHighLight(Color.Yellow)
 
'save to file
doc.SaveToFile("output.pdf", FileFormat.PDF)

Output

Find and Highlight Text Spanning Multiple Lines in PDF in C#, VB.NET

With Spire.Doc for .NET, we can easily insert new text to word document at exact position, it also supports to insert new text after the certain text strings at many places. This article will show you how to insert new text strings after the searched text string in word document.

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;

namespace Word
{
    class Program
    {
        static void Main(string[] args)
        {
            //load the sample document
            Document doc = new Document();
            doc.LoadFromFile("Sample.docx", FileFormat.Docx2010);

            //find all the text string “New Zealand” from the sample document
            TextSelection[] selections = doc.FindAllString("New Zealand", true, true);
            int index = 0;

            //defines text range
            TextRange range = new TextRange(doc);


            //insert new text string (NY) after the searched text string
            foreach (TextSelection selection in selections)
            {
                range = selection.GetAsOneRange();
                TextRange newrange = new TextRange(doc);
                newrange.Text = ("(NY)");
                index = range.OwnerParagraph.ChildObjects.IndexOf(range);
                range.OwnerParagraph.ChildObjects.Insert(index + 1, newrange);

            }

            //find and highlight the newly added text string NY
            TextSelection[] text2 = doc.FindAllString("NY", false, true);
            foreach (TextSelection seletion in text2)
            {
                seletion.GetAsOneRange().CharacterFormat.HighlightColor = Color.Yellow;
            }

            //save the document 
            doc.SaveToFile("Result.docx", FileFormat.Docx2010);
        }
    }
}

Effective screenshot after adding the text strings to the searched text:

C# add new text strings after the searched text string in word document

A combination chart is a chart that combines at least two chart types in a single chart. This article introduces how to combine clustered column and line chart in PowerPoint using Spire.Presentation for Java.

import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;
import com.spire.presentation.charts.ChartType;
import com.spire.presentation.charts.IChart;
import com.spire.presentation.drawing.FillFormatType;

import java.awt.geom.Rectangle2D;

public class CombinationChart {

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

        //create a PowerPoint document
        Presentation presentation = new Presentation();

        //insert a column clustered chart
        Rectangle2D.Double rect = new   Rectangle2D.Double(50, 100, 550, 300);
        IChart chart = presentation.getSlides().get(0).getShapes().appendChart(ChartType.COLUMN_CLUSTERED, rect);

        //set chart title
        chart.getChartTitle().getTextProperties().setText("Sales vs. Unit Price");
        chart.getChartTitle().getTextProperties().isCentered(true);
        chart.getChartTitle().setHeight(30);
        chart.hasTitle(true);

        //write data to chart as chart data
        chart.getChartData().get(0,0).setText("Month");
        chart.getChartData().get(0,1).setText("Unit Price");
        chart.getChartData().get(0,2).setText("Sales");
        chart.getChartData().get(1,0).setText("January");
        chart.getChartData().get(1,1).setNumberValue(120);
        chart.getChartData().get(1,2).setNumberValue(5600);
        chart.getChartData().get(2,0).setText("February");
        chart.getChartData().get(2,1).setNumberValue(100);
        chart.getChartData().get(2,2).setNumberValue(7300);
        chart.getChartData().get(3,0).setText("March");
        chart.getChartData().get(3,1).setNumberValue(80);
        chart.getChartData().get(3,2).setNumberValue(10200);
        chart.getChartData().get(4,0).setText("April");
        chart.getChartData().get(4,1).setNumberValue(120);
        chart.getChartData().get(4,2).setNumberValue(5900);
        chart.getChartData().get(5,0).setText("May");
        chart.getChartData().get(5,1).setNumberValue(90);
        chart.getChartData().get(5,2).setNumberValue(9500);
        chart.getChartData().get(6,0).setText("June");
        chart.getChartData().get(6,1).setNumberValue(110);
        chart.getChartData().get(6,2).setNumberValue(7200);

        //set series labels
        chart.getSeries().setSeriesLabel(chart.getChartData().get("B1", "C1"));

        //set categories labels
        chart.getCategories().setCategoryLabels(chart.getChartData().get("A2", "A7"));

        //assign data to series values
        chart.getSeries().get(0).setValues(chart.getChartData().get("B2", "B7"));
        chart.getSeries().get(1).setValues(chart.getChartData().get("C2", "C7"));

        //change the chart type of series 2 to line with markers
        chart.getSeries().get(1).setType(ChartType.LINE_MARKERS);

        //plot data of series 2 on the secondary axis
        chart.getSeries().get(1).setUseSecondAxis(true);

        //hide grid links of secondary axis
        chart.getSecondaryValueAxis().getMajorGridTextLines().setFillType(FillFormatType.NONE);

        //set overlap
        chart.setOverLap(-50);

        //set gap width
        chart.setGapDepth(200);

        //save the document
        presentation.saveToFile("CombinationChart.pptx", FileFormat.PPTX_2010);
    }
}

Create a Combination Chart in PowerPoint in Java

This article demonstrates how to replace an existing image with a new image in a PDF file using Spire.PDF for Java.

Below is the screenshot of the input PDF file we used for demonstration:

Replace image with new image in PDF in Java

import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.PdfImage;

import java.io.IOException;

public class ReplaceImage {
    public static void main(String[] args) throws IOException {
        //Load the PDF file
        PdfDocument pdf = new PdfDocument();
        pdf.loadFromFile("Input.pdf");

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

        //Load an image
        PdfImage image = PdfImage.fromFile("Image.png");

        //Replace the first image in the first page with the loaded image
        page.replaceImage(0, image);

        //Save the file
        pdf.saveToFile("ReplaceImage.pdf");
    }
}

Output:

Replace image with new image in PDF in Java

Wednesday, 27 March 2019 07:03

Convert PowerPoint to XPS in Java

The article will guide you how to convert PowerPoint (.pptx and .ppt) documents to XPS by using Spire.Presentation for Java.

Here is the screenshot of the sample PowerPoint file.

Convert PowerPoint to XPS in Java

import com.spire.presentation.*;

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

    //create a presentation instance 
    Presentation ppt = new Presentation();

    //load the sample PowerPoint file
    ppt.loadFromFile("Sample.pptx");

    //save to XPS file
    ppt.saveToFile("toXPS.xps", FileFormat.XPS);
ppt.dispose();

    }
}

Effective screenshot of the XPS file:

Convert PowerPoint to XPS in Java

Tuesday, 26 March 2019 07:12

Get PDF Page Labels in C#

Page labels are used to identify each page visually on the screen or in print. This article demonstrates how to get the PDF page labels using Spire.PDF.

Below is the screenshot of the sample PDF document:

Get PDF Page Labels in C#

Detail steps:

Step 1: Create a PdfDocument instance and load the sample.pdf file.

PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("sample.pdf");

Step 2: Get the labels of the pages in the PDF file.

StringBuilder sb = new StringBuilder();
for (int i = 0; i < pdf.Pages.Count; i++)
{
    sb.AppendLine(pdf.Pages[i].PageLabel);
}

Step 3: Save to a .txt file.

File.WriteAllText("PageLabels.txt", sb.ToString());

Output:

Get PDF Page Labels in C#

Full code:

using System.IO;
using System.Text;
using Spire.Pdf;

namespace Get_PDF_Page_Labels
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument instance
            PdfDocument pdf = new PdfDocument();
            //Load the PDF file
            pdf.LoadFromFile("sample.pdf");

            //Create a StringBuilder instance
            StringBuilder sb = new StringBuilder();
            //Get the lables of the pages in the PDF file
            for (int i = 0; i < pdf.Pages.Count; i++)
            {
                sb.AppendLine(pdf.Pages[i].PageLabel);
            }

            //Save to a .txt file
            File.WriteAllText("PageLabels.txt", sb.ToString());
        }
    }
}
Tuesday, 26 March 2019 04:04

Align Text in PowerPoint Table in Java

This article demonstrates how to align text within a table cell in PowerPoint using Spire.Presenatation for Java.

Code Snippets

import com.spire.presentation.*;

public class AlignTextInTableCell {

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

        //create a PowerPoint file
        Presentation presentation = new Presentation();

        //add a table
        Double[] widths = new Double[]{100d, 200d, 100d, 100d};
        Double[] heights = new Double[]{30d, 70d, 70d};
        ITable table = presentation.getSlides().get(0).getShapes().appendTable(30,80, widths, heights);
        table.setStylePreset(TableStylePreset.NONE);

        //set the horizontal alignment for the cells in the first row
        table.get(0, 0).getTextFrame().setText("Left");
        table.get(0, 0).getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.LEFT);
        table.get(1, 0).getTextFrame().setText("Horizontal Center");
        table.get(1, 0).getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.CENTER);
        table.get(2, 0).getTextFrame().setText("Right");
        table.get(2, 0).getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.RIGHT);
        table.get(3, 0).getTextFrame().setText("Justify");
        table.get(3, 0).getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.JUSTIFY);

        //Set the vertical alignment for the cells in the second row
        table.get(0, 1).getTextFrame().setText("Top");
        table.get(0, 1).setTextAnchorType(TextAnchorType.TOP);
        table.get(1, 1).getTextFrame().setText("Vertical Center");
        table.get(1, 1).setTextAnchorType(TextAnchorType.CENTER);
        table.get(2, 1).getTextFrame().setText("Bottom");
        table.get(2, 1).setTextAnchorType(TextAnchorType.BOTTOM);

        //set the both horizontal and vertical alignment for the cells in the third row
        table.get(0, 2).getTextFrame().setText("Top Left");
        table.get(0, 2).getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.LEFT);
        table.get(0, 2).setTextAnchorType(TextAnchorType.TOP);
        table.get(1, 2).getTextFrame().setText("Center");
        table.get(1, 2).getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.CENTER);
        table.get(1, 2).setTextAnchorType(TextAnchorType.CENTER);
        table.get(2, 2).getTextFrame().setText("Bottom Right");
        table.get(2, 2).getTextFrame().getParagraphs().get(0).setAlignment(TextAlignmentType.RIGHT);
        table.get(2, 2).setTextAnchorType(TextAnchorType.BOTTOM);

        //save to file
        presentation.saveToFile("output/Alignment.pptx", FileFormat.PPTX_2013);
        presentation.dispose();
    }
}

Output

Align Text in PowerPoint Table in Java