Wednesday, 27 February 2019 08:32

Shrink Text to Fit in a Cell in Excel in C#

Shrink to fit is a useful option in Excel, it enables us to automatically reduce the font size in a cell until the text fits within the cell. This article demonstrates how to accomplish the same functionality programmatically in C# using Spire.XLS.

Below is the screenshot of the input Excel file:

Shrink Text to Fit in a Cell in Excel in C#

Detail steps:

Step 1: Instantiate a Workbook object and load the Excel file.

Workbook workbook = new Workbook();
workbook.LoadFromFile(@"Input.xlsx");

Step 2: Get the first worksheet.

Worksheet sheet = workbook.Worksheets[0];

Step 3: Specify the cell range to shrink text.

CellRange cell = sheet.Range["A1:E3"];

Step 4: Enable ShrinkToFit.

CellStyle style = cell.Style;
style.ShrinkToFit = true;

Step 5: Save the file.

workbook.SaveToFile("ShrinkTextToFitCell.xlsx", ExcelVersion.Version2013);

Output:

Shrink Text to Fit in a Cell in Excel in C#

Full code:

//Load the Excel file
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"Input.xlsx");

//Get the first worksheet
Worksheet sheet = workbook.Worksheets[0];

//The cell range to shrink text
CellRange cell = sheet.Range["A1:E3"];

//Enable ShrinkToFit
CellStyle style = cell.Style;
style.ShrinkToFit = true;

//Save the file
workbook.SaveToFile("ShrinkTextToFitCell.xlsx", ExcelVersion.Version2013);
Tuesday, 26 February 2019 07:48

Create Spot Color in PDF in Java

This article will demonstrate how to create spot color to PDF file using Spire.PDF in Java.

import com.spire.pdf.*;
import com.spire.pdf.colorspace.*;
import com.spire.pdf.graphics.*;
import java.awt.*;
import java.awt.geom.Point2D;


public class SpotColor {

    public static void main(String[] args) {

        //Create a PdfDocument instance
        PdfDocument pdf = new PdfDocument();

        //Add a page
        PdfPageBase page = pdf.getPages().add();

        //Define the spot color "MySpotColor" from the built-in color.
        PdfRGBColor pdfRGBColor = new PdfRGBColor(new Color(148,0,211));
        PdfSeparationColorSpace cs = new PdfSeparationColorSpace("MySpotColor",pdfRGBColor);

        //Apply the spot color while drawing content on the page.
        PdfSeparationColor color = new PdfSeparationColor(cs, 1f);
        PdfSolidBrush brush = new PdfSolidBrush(color);
        page.getCanvas().drawString("Tint=1.0", new PdfFont(PdfFontFamily.Helvetica, 10f), brush, new Point2D.Float(160, 160));

        //Draw pie with spot color(DarkViolet)
        page.getCanvas().drawPie(brush, 148, 200, 60, 60, 360, 360);

        page.getCanvas().drawString("Tint=0.7", new PdfFont(PdfFontFamily.Helvetica, 10f), brush, new Point2D.Float(230, 160));
        color = new PdfSeparationColor(cs, 0.7f);
        brush = new PdfSolidBrush(color);
        page.getCanvas().drawPie(brush, 218, 200, 60, 60, 360, 360);

        page.getCanvas().drawString("Tint=0.4", new PdfFont(PdfFontFamily.Helvetica, 10f), brush, new Point2D.Float(300, 160));
        color = new PdfSeparationColor(cs, 0.4f);
        brush = new PdfSolidBrush(color);
        page.getCanvas().drawPie(brush, 288, 200, 60, 60, 360, 360);

        page.getCanvas().drawString("Tint=0.1", new PdfFont(PdfFontFamily.Helvetica, 10f), brush, new Point2D.Float(370, 160));
        color = new PdfSeparationColor(cs, 0.1f);
        brush = new PdfSolidBrush(color);
        page.getCanvas().drawPie(brush, 358, 200, 60, 60, 360, 360);


        //Save the document
        pdf.saveToFile("output/drawContentWithSpotColor.pdf");

    }
}

Effective screenshot after adding spot color in PDF in Java application:

Create Spot Color in PDF in Java

Monday, 25 February 2019 07:49

Convert PowerPoint to PDF in Java

The article demonstrates how to convert PowerPoint (.pptx and .ppt) documents to PDF documents using Spire.Presentation for Java.

Here is the screenshot of the sample PowerPoint file.

Convert PowerPoint to PDF in Java

Entire Code

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

public class PPTXtoPDF {

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

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

        //load the sample PowerPoint file
        presentation.loadFromFile("C:/Users/Administrator/Desktop/example.pptx");

        //save to PDF file
        presentation.saveToFile("toPDF.pdf", FileFormat.PDF);
        presentation.dispose();
    }
}

Output

Convert PowerPoint to PDF in Java

This article demonstrates how to set the row height and column width of an existing table in a PowerPoint document using Spire.Presentation for Java.

Below is the screenshot of the input PowerPoint document:

Set Table Row Height and Column Width in PowerPoint in Java

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

public class Table_Row_Height_and_Column_Width {
    public static void main(String[] args) throws Exception {
        //Load the PowerPoint document
        Presentation ppt = new Presentation();
        ppt.loadFromFile("Table.pptx");

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

        //Get the first table in the slide
        ITable table = (ITable) slide.getShapes().get(0);

        //Change the height of the first table row and the width of the first table column
        table.getTableRows().get(0).setHeight(100);
        table.getColumnsList().get(0).setWidth(250);

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

Output:

Set Table Row Height and Column Width in PowerPoint in Java

Thursday, 21 February 2019 07:17

Java add or delete page on PDF

When we get a PDF documents, we many only need to use some page of the whole PDF document. And sometimes we need to insert a new page to add some new contents. This article, we will show you how to add and delete the PDF page in Java applications.

Firstly, view the PDF sample as below:

Java add or delete page on PDF

import com.spire.pdf.*;
import java.awt.*;
import java.awt.print.*;
import com.spire.pdf.graphics.*;
import java.awt.geom.Rectangle2D;


public class Print {

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

        //Create a new PDFDocument and load the sample 
        PdfDocument pdf = new PdfDocument();
        pdf.loadFromFile("Sample.pdf");

        //Delete the second page
        pdf.getPages().removeAt(1);

        //Add a new page to PDF document
        PdfPageBase page = pdf.getPages().add();

        //Draw text string to the page and set the font for it.
        PdfTrueTypeFont font= new PdfTrueTypeFont(new Font("Arial Unicode MS",Font.PLAIN,14),true);

        PdfRGBColor blue = new PdfRGBColor();
        blue.setB((byte) 255);
        PdfSolidBrush brush = new PdfSolidBrush(blue);
        Rectangle2D.Float rctg1 = new Rectangle2D.Float();
        rctg1.setRect(0,70,page.getCanvas().getClientSize().getWidth() / 2,100);
        page.getCanvas().drawString("New page added by Spire.PDF ", font, brush, rctg1);

        //Save the document
        pdf.saveToFile("output/Newpage.pdf");

        }
    }

Effective screenshot after delete and add a new page on PDF:

Java add or delete page on PDF

Tuesday, 19 February 2019 02:08

Add Navigation Buttons to PDF in Java

Adding navigation buttons to a PDF document makes it easy for readers to jump to a certain page from another page. This article demonstrates how we can create a button that redirects to a special page (for example, home page, last page and next page) or a particular page (for example, page 9) in PDF using Spire.PDF for Java.

Entire Code:

import com.spire.pdf.actions.PdfActionDestination;
import com.spire.pdf.actions.PdfGoToAction;
import com.spire.pdf.actions.PdfNamedAction;
import com.spire.pdf.fields.PdfButtonField;
import com.spire.pdf.general.PdfDestination;
import com.spire.pdf.graphics.PdfRGBColor;
import com.spire.pdf.graphics.PdfTrueTypeFont;

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

public class AddNavigationButton {

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

        //create a PdfDocument object and load a sample PDF file
        PdfDocument doc = new PdfDocument();
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\SalesReport.pdf");

        //get the last page
        PdfPageBase lastPage = doc.getPages().get(doc.getPages().getCount() - 1);

        //allow creating forms in PDF
        doc.setAllowCreateForm(true);

        //define float variables to specify the location and size of the button
        float x = 80;
        float y = 300;
        float width = 150;
        float height = 22;

        //create a truetype font
        PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS", Font.PLAIN, 13), true);

        //add a button that navigates to the first page
        PdfButtonField btn_1 = new PdfButtonField(lastPage, "btn_1");
        Rectangle2D.Float rect = new Rectangle2D.Float(x, y, width, height);
        btn_1.setBounds(rect);
        btn_1.setFont(font);
        btn_1.setText("Return to Page 1");
        btn_1.setBackColor(new PdfRGBColor(Color.ORANGE));
        btn_1.setForeColor(new PdfRGBColor(Color.blue));
        btn_1.setBorderColor(new PdfRGBColor(Color.blue));
        PdfNamedAction namedAction = new PdfNamedAction(PdfActionDestination.FirstPage);
        btn_1.getActions().setMouseDown(namedAction);
        doc.getForm().getFields().add(btn_1);

        //add a button that navigates to a particular page
        PdfButtonField btn_2 = new PdfButtonField(lastPage, "btn_2");
        rect = new Rectangle2D.Float( x, y + height + 5, width, height);
        btn_2.setBounds(rect);
        btn_2.setText("Jump to Page 9");
        btn_2.setFont(font);
        btn_2.setBackColor(new PdfRGBColor(Color.ORANGE));
        btn_2.setForeColor(new PdfRGBColor(Color.blue));
        btn_2.setBorderColor(new PdfRGBColor(Color.blue));
        PdfGoToAction goToAction = new PdfGoToAction(new PdfDestination(doc.getPages().get(8)));
        btn_2.getActions().setMouseDown(goToAction);
        doc.getForm().getFields().add(btn_2);

        //save to file
        doc.saveToFile("NavigationButton.pdf", FileFormat.PDF);
        doc.close();
    }
}

Output:

Add Navigation Buttons to PDF in Java

This article demonstrates how to create a chart without reference to the worksheet data range using Spire.XLS.

Detail steps:

Step 1: Create a workbook and get the first worksheet.

Workbook wb = new Workbook();            
Worksheet sheet = wb.Worksheets[0];

Step 2: Add a chart to the worksheet.

Chart chart = sheet.Charts.Add();

Step 3: Add a series to the chart.

var series = chart.Series.Add();

Step 4: Add data.

series.EnteredDirectlyValues = new object[] { 10, 20, 30 };

Step 5: Save the file.

wb.SaveToFile("result.xlsx", ExcelVersion.Version2013);

Output:

Create Chart without Using Worksheet Data Range in C#

Full code:

using Spire.Xls;

namespace Create_chart
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a workbook
            Workbook wb = new Workbook();
            
            //Get the first worksheet
            Worksheet sheet = wb.Worksheets[0];

            //Add a chart to the worksheet
            Chart chart = sheet.Charts.Add();

            //Add a series to the chart
            var series = chart.Series.Add();

            //Add data 
            series.EnteredDirectlyValues = new object[] { 10, 20, 30 };

            //Save the file
            wb.SaveToFile("result.xlsx", ExcelVersion.Version2013);
        }
    }
}
Tuesday, 12 February 2019 07:30

Insert an Image to a PowerPoint Table in Java

This article demonstrates how to insert an image to a table cell in PowerPoint using Spire.Presentataion for Java.

import com.spire.presentation.FileFormat;
import com.spire.presentation.ITable;
import com.spire.presentation.Presentation;
import com.spire.presentation.drawing.FillFormatType;
import com.spire.presentation.drawing.IImageData;
import com.spire.presentation.drawing.PictureFillType;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;

public class InsertImageToTableCell {

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

        //create a Presentation object and load an example PowerPoint file
        Presentation presentation = new Presentation();
        presentation.loadFromFile("C:/Users/Administrator/Desktop/example.pptx");

        //append a table to the first slide
        Double[] widths = new Double[]{100d,100d};
        Double[] heights = new Double[]{100d,100d};
        ITable table = presentation.getSlides().get(0).getShapes().appendTable(100,100, widths, heights);

        //insert an image to the cell(0,0)
        table.get(0,0).getFillFormat().setFillType(FillFormatType.PICTURE);
        table.get(0,0).getFillFormat().getPictureFill().setFillType(PictureFillType.STRETCH);
        BufferedImage bufferedImage = ImageIO.read(new FileInputStream("C:/Users/Administrator/Desktop/logo.png"));
        IImageData imageData = presentation.getImages().append(bufferedImage);
        table.get(0,0).getFillFormat().getPictureFill().getPicture().setEmbedImage(imageData);

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

Insert an Image to a PowerPoint Table in Java

Wednesday, 30 January 2019 07:55

Set PDF Viewer Preference in Java

PDF viewer preference allows users to view PDF with specified view mode or display layout. This article demonstrates how to set the viewer preference in a PDF file using Spire.PDF for Java.

import com.spire.pdf.*;

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

        //Load the PDF file
        PdfDocument pdf = new PdfDocument();
        pdf.loadFromFile("Additional.pdf");

        //Set viewer preference
        pdf.getViewerPreferences().setCenterWindow(true);
        pdf.getViewerPreferences().setDisplayTitle(false);
        pdf.getViewerPreferences().setFitWindow(true);
        pdf.getViewerPreferences().setHideMenubar(true);
        pdf.getViewerPreferences().setHideToolbar(true);
        pdf.getViewerPreferences().setPageLayout(PdfPageLayout.Single_Page);
        //pdf.getViewerPreferences().setPageMode(PdfPageMode.Full_Screen);
        //pdf.getViewerPreferences().setPrintScaling(PrintScalingMode.App_Default);

        //Save the file.
        pdf.saveToFile("ViewerPreference.pdf");
        //Close
        pdf.close();
    }
}

Output:

Set PDF Viewer Preference in Java

Wednesday, 30 January 2019 07:10

Insert Video in PowerPoint in Java

This article demonstrates how to insert a video file (.mp4) in a presentation slide by using Spire.Presentation for Java.

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

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;

public class InsertVideo {

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

        //create a Presentation object and load an example PowerPoint file
        Presentation presentation = new Presentation();
        presentation.loadFromFile("C:/Users/Administrator/Desktop/example.pptx");

        //add a shape to the first slide
        Rectangle2D.Double labelRect = new Rectangle2D.Double(50, 120, 100, 50);
        IAutoShape labelShape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, labelRect);
        labelShape.getLine().setFillType(FillFormatType.NONE);
        labelShape.getFill().setFillType(FillFormatType.NONE);
        labelShape.getTextFrame().setText("Video:");
        labelShape.getTextFrame().getTextRange().setFontHeight(28);
        labelShape.getTextFrame().getTextRange().setLatinFont(new TextFont("Myriad Pro Light"));
        labelShape.getTextFrame().getTextRange().getFill().setFillType(FillFormatType.SOLID);
        labelShape.getTextFrame().getTextRange().getFill().getSolidColor().setColor(Color.BLACK);

        //append a video file to the slide and set the cover image
        Rectangle2D.Double videoRect = new Rectangle2D.Double(150, 120, 400, 225);
        IVideo video = presentation.getSlides().get(0).getShapes().appendVideoMedia((new java.io.File("C:/Users/Administrator/Desktop/video.mp4")).getAbsolutePath(), videoRect);
        BufferedImage coverImage = ImageIO.read( new File("C:/Users/Administrator/Desktop/coverImage.jpg"));
        video.getPictureFill().getPicture().setEmbedImage(presentation.getImages().append(coverImage));

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

Insert Video in PowerPoint in Java

Page 2 of 104