Thursday, 21 March 2019 06:35

Get Cell Type in Excel in C#

Cell type refers to the data type in a cell. There are six cell types in Spire.XLS, i.e. String, Number, Formula, Boolean, Error and Blank. This article is going to show you how to get the cell types of specified cells in an Excel file using Spire.XLS.

Detail steps

Step 1: Create a Workbook instance and load the Excel file.

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

Step 2: Get the second worksheet.

Worksheet sheet = workbook.Worksheets[1];

Step 3: Get the cell types of the cells in range "A2:A7".

foreach (CellRange range in sheet.Range["A2:A7"])
{
    XlsWorksheet.TRangeValueType cellType = sheet.GetCellType(range.Row, range.Column, false);
    sheet[range.Row, range.Column+1].Text = cellType.ToString();
    sheet[range.Row, range.Column + 1].Style.Font.Color = Color.Red;
    sheet[range.Row, range.Column+1].Style.Font.IsBold = true;
}

Step 4: Save the file.

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

Output:

Get Cell Type in Excel in C#

Full code:

using System.Drawing;
using Spire.Xls;
using Spire.Xls.Core.Spreadsheet;

namespace GetCellType
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load the Excel file
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("Input.xlsx");

            //Get the second worksheet
            Worksheet sheet = workbook.Worksheets[1];

            //Get the cell types of the cells in range “A2:A7”
            foreach (CellRange range in sheet.Range["A2:A7"])
            {
                XlsWorksheet.TRangeValueType cellType = sheet.GetCellType(range.Row, range.Column, false);
                sheet[range.Row, range.Column+1].Text = cellType.ToString();
                sheet[range.Row, range.Column + 1].Style.Font.Color = Color.Red;
                sheet[range.Row, range.Column+1].Style.Font.IsBold = true;
            }            

            //Save the file
            workbook.SaveToFile("GetCellType.xlsx", ExcelVersion.Version2013);            
        }
    }
}

To emphasize the text on a shape, we can only animate text instead of the whole shape. This article demonstrates how to apply animations to text in PowerPoint using Spire.Presenstation with C# and VB.NET.

Code Snippets

[C#]
//create a PowerPoint document
Presentation ppt = new Presentation();

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

//add a shape to the slide
IAutoShape shape = slide.Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(50, 50, 200, 80));
shape.Fill.FillType = FillFormatType.Solid;
shape.Fill.SolidColor.Color = Color.Purple;
shape.ShapeStyle.LineColor.Color = Color.White;
shape.AppendTextFrame("Welcome to download Spire.Presentation");

//apply animation to the text in shape
AnimationEffect animation = shape.Slide.Timeline.MainSequence.AddEffect(shape, AnimationEffectType.Float);
animation.SetStartEndParagraphs(0, 0);

//save to file
ppt.SaveToFile("result.pptx", FileFormat.Pptx2013);
ppt.Dispose();
[VB.NET]
'create a PowerPoint document
Dim ppt As Presentation =  New Presentation() 
 
'get the first slide
Dim slide As ISlide =  ppt.Slides(0) 
 
'add a shape to the slide
Dim shape As IAutoShape =  slide.Shapes.AppendShape(ShapeType.Rectangle,New RectangleF(50,50,200,80)) 
shape.Fill.FillType = FillFormatType.Solid
shape.Fill.SolidColor.Color = Color.Purple
shape.ShapeStyle.LineColor.Color = Color.White
shape.AppendTextFrame("Welcome to download Spire.Presentation")
 
'apply animation to the text in shape
Dim animation As AnimationEffect =  shape.Slide.Timeline.MainSequence.AddEffect(shape,AnimationEffectType.Float) 
animation.SetStartEndParagraphs(0, 0)
 
'save to file
ppt.SaveToFile("result.pptx", FileFormat.Pptx2013)
ppt.Dispose()

Output

Apply Animations to Text in PowerPoint in C#, VB.NET

Digital timestamps mark a PDF signature with the time and date as proof of integrity. A timestamp shows that the contents of the document existed at a point in time, and are unchanged. This article is going to introduce how to digitally sign a PDF document with a timestamp server by using Spire.PDF.

Code Snippets

[C#]
//create a PdfDocument object and load a PDF file
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(@"C:\Users\Administrator\Desktop\Example.pdf");

//load the certificate .pfx file
PdfCertificate cert = new PdfCertificate(@"C:\Users\Administrator\Desktop\gary.pfx", "e-iceblue");

//add a signature to the specified position
PdfSignature signature = new PdfSignature(doc, doc.Pages[0], cert, "signature");
signature.Bounds = new RectangleF(new PointF(350, 700), new SizeF(180, 90));

//set the signature content
signature.NameLabel = "Digitally signed by:Gary";
signature.LocationInfoLabel = "Location:";
signature.LocationInfo = "CN";
signature.ReasonLabel = "Reason: ";
signature.Reason = "Ensure authenticity";
signature.ContactInfoLabel = "Contact Number: ";
signature.ContactInfo = "028-81705109";
signature.DocumentPermissions = PdfCertificationFlags.AllowFormFill | PdfCertificationFlags.ForbidChanges;
signature.GraphicsMode = GraphicMode.SignImageAndSignDetail;
signature.SignImageSource = PdfImage.FromFile(@"C:\Users\Administrator\Desktop\company-logo.jpg");

//configure a timestamp server
string url = "http://timestamp.wosign.com/rfc3161";
signature.ConfigureTimestamp(url);

//save to file
doc.SaveToFile("output.pdf");
[VB.NET]
'create a PdfDocument object and load a PDF file
Dim doc As PdfDocument =  New PdfDocument() 
doc.LoadFromFile("C:\Users\Administrator\Desktop\Example.pdf")
 
'load the certificate .pfx file
Dim cert As PdfCertificate =  New PdfCertificate("C:\Users\Administrator\Desktop\gary.pfx","e-iceblue") 
 
'add a signature to the specified position
Dim signature As PdfSignature =  New PdfSignature(doc,doc.Pages(0),cert,"signature") 
signature.Bounds = New RectangleF(New PointF(350, 700), New SizeF(180, 90))
 
'set the signature content
signature.NameLabel = "Digitally signed by:Gary"
signature.LocationInfoLabel = "Location:"
signature.LocationInfo = "CN"
signature.ReasonLabel = "Reason: "
signature.Reason = "Ensure authenticity"
signature.ContactInfoLabel = "Contact Number: "
signature.ContactInfo = "028-81705109"
signature.DocumentPermissions = PdfCertificationFlags.AllowFormFill | PdfCertificationFlags.ForbidChanges
signature.GraphicsMode = GraphicMode.SignImageAndSignDetail
signature.SignImageSource = PdfImage.FromFile("C:\Users\Administrator\Desktop\company-logo.jpg")
 
'configure a timestamp server
Dim url As String =  "http://timestamp.wosign.com/rfc3161" 
signature.ConfigureTimestamp(url)
 
'save to file
doc.SaveToFile("output.pdf")

Output

How to Digitally Sign PDF with Timestamp Server in C#, VB.NET

PowerPoint print settings allow users to control how presentation slides are printed, such as print all slides, print some selected slides, print slides with frames or not, print many slides into one page, print order, print color and so on. This article will show you how to set print options when print PowerPoint documents in C#.

Firstly, view Microsoft PowerPoint's page print settings:

Set the print settings of PowerPoint document in C#

Code Snippets of set print settings of PowerPoint document by using PrinterSettings object to print the presentation slides:

static void Main(string[] args)
{
    //load the sample document from file
    Presentation ppt = new Presentation();
    ppt.LoadFromFile("Sample.pptx");


    //use PrinterSettings object to print presentation slides
    PrinterSettings ps = new PrinterSettings();
    ps.PrintRange = PrintRange.AllPages;
    ps.PrintToFile = true;
    ps.PrintFileName = ("Print.xps");

    //print the slide with frame
    ppt.SlideFrameForPrint = true;

    //print the slide with Grayscale
    ppt.GrayLevelForPrint = true;

    //Print 4 slides horizontal
    ppt.SlideCountPerPageForPrint = PageSlideCount.Four;
    ppt.OrderForPrint = Order.Horizontal;


    ////only select some slides to print
    //ppt.SelectSlidesForPrint("1", "3");


    //print the document
    ppt.Print(ps);

}

Code Snippets of set print document name by using PrintDocument object to print presentation slides:

static void Main(string[] args)
{
    //load the sample document from file
    Presentation ppt = new Presentation();
    ppt.LoadFromFile("Sample.pptx");

    //use PrintDocument object to print presentation slides
    PresentationPrintDocument document = new PresentationPrintDocument(ppt);

    //print document to virtual printer 
    document.PrinterSettings.PrinterName = "Microsoft XPS Document Writer";

    //print the slide with frame
    ppt.SlideFrameForPrint = true;

    //print 4 slides horizontal
    ppt.SlideCountPerPageForPrint = PageSlideCount.Four;
    ppt.OrderForPrint = Order.Horizontal;

    //print the slide with Grayscale
    ppt.GrayLevelForPrint = true;

    //set the print document name
    document.DocumentName = "Print Task"; 

    document.PrinterSettings.PrintToFile = true;
    document.PrinterSettings.PrintFileName = ("Print.xps");

    ppt.Print(document);                   
  
}

Start from version 7.3.3, Spire.Doc supports keeping Word headings as PDF bookmarks when converting Word to PDF. This article demonstrates how to use Spire.Doc to accomplish this functionality.

The input Word document:

Keep Word Headings as PDF Bookmarks When Converting Word to PDF in C#

Example code

using Spire.Doc;

namespace Keep_word_headings_as_pdf_bookmarks
{
    class Program
    {
        static void Main(string[] args)
        {
            Document doc = new Document();
            doc.LoadFromFile("Input.docx");

            ToPdfParameterList topdf = new ToPdfParameterList();
            topdf.CreateWordBookmarks = true;
            topdf.CreateWordBookmarksUsingHeadings = true;

            doc.SaveToFile("Output.pdf", topdf);
        }
    }
}

The output PDF document:

Keep Word Headings as PDF Bookmarks When Converting Word to PDF in C#

Thursday, 07 March 2019 06:32

Convert PDF to PostScript in C#, VB.NET

A PostScript (.PS) file is an image saved in the PostScript page description language. The PostScript file format is widely used by publishers primarily for printing purposes. This article will show you how we can easily convert PDF to PostScript by using Sprie.PDF with C# and VB.NET.

Below is the screenshot of the input PDF file.

Convert PDF to PostScript in C#, VB.NET

Full Code

[C#]
//create a PdfDocument instance
PdfDocument document = new PdfDocument();

//load a sample PDF file
document.LoadFromFile(@"C:\Users\Administrator\Desktop\Introduction.pdf");

//save as .ps file
document.SaveToFile("toPostScript.ps", FileFormat.POSTSCRIPT);
[VB.NET]
'create a PdfDocument instance
Dim document As PdfDocument =  New PdfDocument() 
 
'load a sample PDF file
document.LoadFromFile("C:\Users\Administrator\Desktop\Introduction.pdf")
 
'save as .ps file
document.SaveToFile("toPostScript.ps", FileFormat.POSTSCRIPT)

Output

Convert PDF to PostScript in C#, VB.NET

Wednesday, 06 March 2019 07:45

Convert PDF to PCL in C#, VB.NET

A PCL file is a Printer Command Language document. Printer Command Language is a page description language developed by HP as a printer protocol and has been widely supported in many printers now. Start from version 5.2.3, Spire.PDF supports converting PDF file to PCL format. There are six major levels of PCL, the PCL here refers to PCL 6 (PCL 6 Enhanced or PCL XL).

Example code

[C#]
//Create a PdfDocument instance
PdfDocument pdf = new PdfDocument();
//Load the PDF file
pdf.LoadFromFile("Input.pdf");
//Save to PCL format
pdf.SaveToFile("ToPCL.pcl", FileFormat.PCL);
[VB.NET]
Private Sub SurroundingSub()
    Dim pdf As PdfDocument = New PdfDocument()
    pdf.LoadFromFile("Input.pdf")
    pdf.SaveToFile("ToPCL.pcl", FileFormat.PCL)
End Sub
Tuesday, 05 March 2019 09:02

Convert Word to PCL

PCL File is Digital printed document created in the Printer Command Language (more commonly referred to as PCL) page description language. From v7.1.19, Spire.Doc supports to convert word document to PCL. There are many kinds of standard for PCL document; the PCL here refers to PCL 6 (PCL 6 Enhanced or PCL XL). This article will show you how to save word document to PCL in C# and VB.NET by only three lines of codes.

[C#]
//load the sample document
Document doc = new Document();
doc.LoadFromFile("Sample.docx", FileFormat.Docx2010);

//save the document as a PCL file
doc.SaveToFile("Result.pcl", FileFormat.PCL);   
[VB.NET]
Private Sub SurroundingSub()
Dim doc As Document = New Document()
doc.LoadFromFile("Sample.docx", FileFormat.Docx2010)
doc.SaveToFile("Result.pcl", FileFormat.PCL)
End Sub

The slide master in PowerPoint preserves some fixed styles, such as background image, title and theme color, which can be inherited by other slides. This article demonstrates how to customize the slide masters in a PowerPoint file and apply them to different slides using Spire.Presentation for Java.

Apply One Slide Master in PowerPoint

import com.spire.presentation.*;
import com.spire.presentation.drawing.BackgroundType;
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.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;


public class ModifyAndApplySlideMaster {

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

        //create a Presentation object and specify the slide size
        Presentation presentation = new Presentation();
        presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);

        //get the first slide master
        IMasterSlide masterSlide = presentation.getMasters().get(0);

        //set the background image of the slide master
        String backgroundPic = "C:/Users/Administrator/Desktop/bg.jpg";
        BufferedImage image = ImageIO.read(new FileInputStream(backgroundPic));
        IImageData imageData = presentation.getImages().append(image);
        masterSlide.getSlideBackground().setType(BackgroundType.CUSTOM);
        masterSlide.getSlideBackground().getFill().setFillType(FillFormatType.PICTURE);
        masterSlide.getSlideBackground().getFill().getPictureFill().setFillType(PictureFillType.STRETCH);
        masterSlide.getSlideBackground().getFill().getPictureFill().getPicture().setEmbedImage(imageData);

        //add an image (company logo) to slide master
        String logo = "C:/Users/Administrator/Desktop/logo.png";
        image = ImageIO.read(new FileInputStream(logo));
        imageData = presentation.getImages().append(image);
        IEmbedImage imageShape = masterSlide.getShapes().appendEmbedImage(ShapeType.RECTANGLE,imageData,new Rectangle2D.Float(40,40,200,60));
        imageShape.getLine().setFillType(FillFormatType.NONE);

        //add some text (company name) to slide master
        IAutoShape textShape = masterSlide.getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle2D.Float((float) presentation.getSlideSize().getSize().getWidth()-200,(float) presentation.getSlideSize().getSize().getHeight()-60,200,30));//Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(ppt.SlideSize.Size.Width-200, ppt.SlideSize.Size.Height-60, 200, 30));
        textShape.getTextFrame().setText("Chengdu E-iceblue Co., Ltd.");
        textShape.getTextFrame().getTextRange().setFontHeight(15f);
        textShape.getTextFrame().getTextRange().getFill().setFillType(FillFormatType.SOLID);
        textShape.getTextFrame().getTextRange().getFill().getSolidColor().setColor(Color.blue);
        textShape.getTextFrame().getTextRange().getParagraph().setAlignment(TextAlignmentType.CENTER);
        textShape.getFill().setFillType(FillFormatType.NONE);
        textShape.getLine().setFillType(FillFormatType.NONE);

        //append a new slide
        presentation.getSlides().append();

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

Modify and Apply Slide Maters in PowerPoint in Java

Apply Multiple Slide Maters in PowerPoint

import com.spire.presentation.*;
import com.spire.presentation.drawing.BackgroundType;
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 CreateAndApplyMultiSlideMasters {

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

        //create a Presentation object and specify the slide size
        Presentation presentation = new Presentation();
        presentation.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);

        //add four new slides to the presentation 
        for (int i = 0; i < 4; i++)
        {
            presentation.getSlides().append();
        }

        //get the first slide master
        IMasterSlide first_master = presentation.getMasters().get(0);

        //create another slide master based on the first one 
        presentation.getMasters().appendSlide(first_master);
        IMasterSlide second_master = presentation.getMasters().get(1);

        //set different background images for the two masters
        String pic1 = "C:/Users/Administrator/Desktop/image1.png";
        String pic2 = "C:/Users/Administrator/Desktop/image2.png";
        BufferedImage image = ImageIO.read(new FileInputStream(pic1));
        IImageData imageData = presentation.getImages().append(image);
        first_master.getSlideBackground().setType(BackgroundType.CUSTOM);
        first_master.getSlideBackground().getFill().setFillType(FillFormatType.PICTURE);
        first_master.getSlideBackground().getFill().getPictureFill().setFillType(PictureFillType.STRETCH);
        first_master.getSlideBackground().getFill().getPictureFill().getPicture().setEmbedImage(imageData);
        image = ImageIO.read(new FileInputStream(pic2));
        imageData = presentation.getImages().append(image);
        second_master.getSlideBackground().setType(BackgroundType.CUSTOM);
        second_master.getSlideBackground().getFill().setFillType(FillFormatType.PICTURE);
        second_master.getSlideBackground().getFill().getPictureFill().setFillType(PictureFillType.STRETCH);
        second_master.getSlideBackground().getFill().getPictureFill().getPicture().setEmbedImage(imageData);

        //apply the first master along with the layout to the first slide
        presentation.getSlides().get(0).setLayout(first_master.getLayouts().get(6));

        //apply the second master along with the layout to the rest slides
        for (int i = 1; i < presentation.getSlides().getCount(); i++)
        {
            presentation.getSlides().get(i).setLayout(second_master.getLayouts().get(6));
        }

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

Modify and Apply Slide Maters in PowerPoint in Java

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);