Thursday, 08 November 2018 07:59

How to convert Word to PostScript in C#

PostScript is a page description language that is an industry standard for outputting high-resolution text and graphics. From Version 6.11.2, Spire.Doc supports to convert doc/docx to a postscript file in both WinForm app and ASP.NET app. This article will demonstrate how to convert word to PostScript in C# and VB.NET.

Firstly, view the sample word document:

How to convert Word to PostScript in C#

[C#]
using Spire.Doc;

namespace Word
{
    class Program
    {
        static void Main(string[] args)
        {            
            Document doc = new Document();
            doc.LoadFromFile("Sample.docx", FileFormat.Docx2010);
            doc.SaveToFile("Result.ps", FileFormat.PostScript);                 
        }
    }
}
[VB.NET]
Imports Spire.Doc
Namespace Word
    Class Program
        Private Shared Sub Main(ByVal args As String())
            Dim doc As Document = New Document()
            doc.LoadFromFile("Sample.docx", FileFormat.Docx2010)
            doc.SaveToFile("Result.ps", FileFormat.PostScript)
        End Sub
    End Class
End Namespace

Effective screenshot of the resulted PostScript file converted from .docx document:

How to convert Word to PostScript in C#

Wednesday, 29 December 2021 06:05

Java: Convert PDF to PDF/A

PDF/A is a kind of PDF format designed for archiving and long-term preservation of electronic documents. Unlike paper documents that are easily damaged or smeared, PDF/A format ensures that documents can be reproduced in exactly the same way even after long-term storage. This article will demonstrate how to convert PDF to PDF/A-1A, 2A, 3A, 1B, 2B and 3B compliant PDF 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.10.7</version>
    </dependency>
</dependencies>
    

Convert PDF to PDF/A

The detailed steps are as follows:

  • Create a PdfStandardsConverter instance, and pass in a sample PDF file as a parameter.
  • Convert the sample file to PdfA1A conformance level using PdfStandardsConverter.toPdfA1A() method.
  • Convert the sample file to PdfA1B conformance level using PdfStandardsConverter. toPdfA1B() method.
  • Convert the sample file to PdfA2A conformance level using PdfStandardsConverter. toPdfA2A() method.
  • Convert the sample file to PdfA2B conformance level using PdfStandardsConverter. toPdfA2B() method.
  • Convert the sample file to PdfA3A conformance level using PdfStandardsConverter. toPdfA3A() method.
  • Convert the sample file to PdfA3B conformance level using PdfStandardsConverter. toPdfA3B() method.
  • Java
import com.spire.pdf.conversion.PdfStandardsConverter;

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

        //Create a PdfStandardsConverter instance, and pass in a sample file as a parameter
        PdfStandardsConverter converter = new PdfStandardsConverter("sample.pdf");

        //Convert to PdfA1A
        converter.toPdfA1A("output/ToPdfA1A.pdf");

        //Convert to PdfA1B
        converter.toPdfA1B("output/ToPdfA1B.pdf");

        //Convert to PdfA2A
        converter.toPdfA2A( "output/ToPdfA2A.pdf");

        //Convert to PdfA2B
        converter.toPdfA2B("output/ToPdfA2B.pdf");

        //Convert to PdfA3A
        converter.toPdfA3A("output/ToPdfA3A.pdf");

        //Convert to PdfA3B
        converter.toPdfA3B("output/ToPdfA3B.pdf");
    }
}

Java: Convert PDF to PDF/A

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.

Tuesday, 22 March 2022 07:09

Java: Extract Images from a PDF Document

If you'd like to use the images embedded in a PDF document elsewhere, you can extract and save them in a folder. This article will show you how to programmatically extract images from a PDF document 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.10.7</version>
    </dependency>
</dependencies>
    

Extract Images from a PDF Document

Spire.PDF for Java offers the PdfPageBase.extractImages() method to extract images from a PDF document. The detailed steps are listed below.

  • Create a PdfDocument instance and load a PDF sample document using PdfDocument.loadFromFile() method.
  • Loop through all pages of the document and extract images from the given page using PdfPageBase.extractImages() method.
  • Specify the path and name of the output document.
  • Save images as .png files.
  • Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class ExtractImage {
    public static void main(String[] args) throws IOException {
        //create a PdfDocument instance
        PdfDocument doc = new PdfDocument();

        //load a PDF sample file
        doc.loadFromFile("sample.pdf");

        //declare an int variable
        int index = 0;

        //loop through all pages
        for (PdfPageBase page : (Iterable<PdfPageBase>) doc.getPages()) {

            //extract images  from the given page
            for (BufferedImage image : page.extractImages()) {

                //specify the file path and name
                File output = new File("C:\\Users\\Administrator\\Desktop\\ExtractedImages\\" + String.format("Image_%d.png", index++));

                //save images as .png files
              ImageIO.write(image, "PNG", output);
            }
        }
    }
}

Java: Extract Images from a PDF Document

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.

Every PowerPoint presentation has a slide master which contains all the styles for your slides. You can quickly change the look of your entire presentation by selecting the slide master, and then adopting a theme, adding a background picture or changing the color scheme.

In this article, you will learn how to access and customize the slide master in an existing presentation.

Source File:

Apply a Slide Master to a Presentation in 

C#, VB.NET

Detail steps:

Step 1: Load the source file.

Presentation ppt = new Presentation();
ppt.LoadFromFile(@"sample.pptx");

Step 2: Get the first slide master from the presentation.

IMasterSlide masterSlide = ppt.Masters[0];

Step 3: Customize the background of the slide master.

string backgroundPic = "background.png";
RectangleF rect = new RectangleF(0, 0, ppt.SlideSize.Size.Width, ppt.SlideSize.Size.Height);
masterSlide.SlideBackground.Fill.FillType = FillFormatType.Picture;
IEmbedImage image = masterSlide.Shapes.AppendEmbedImage(ShapeType.Rectangle, backgroundPic, rect);
masterSlide.SlideBackground.Fill.PictureFill.Picture.EmbedImage = image as IImageData;

Step 4: Change the color scheme.

masterSlide.Theme.ColorScheme.Accent1.Color = Color.Red;
masterSlide.Theme.ColorScheme.Accent2.Color = Color.RosyBrown;
masterSlide.Theme.ColorScheme.Accent3.Color = Color.Ivory;
masterSlide.Theme.ColorScheme.Accent4.Color = Color.Lavender;
masterSlide.Theme.ColorScheme.Accent5.Color = Color.Black;

Step 5: Add an image to the slide master. If you want, you can add any other document elements to slide master so that they can display on each slide.

string logo = "logo.png";
IEmbedImage imageShape = masterSlide.Shapes.AppendEmbedImage(ShapeType.Rectangle, logo, new RectangleF(40, 40, 240, 65));
imageShape.Line.FillFormat.FillType = FillFormatType.None;

Step 6: Save the document.

ppt.SaveToFile("result.pptx", FileFormat.Pptx2013);

Result:

Apply a Slide Master to a Presentation in C#, VB.NET

Full code:

[C#]
using Spire.Presentation;
using Spire.Presentation.Drawing;
using System.Drawing;
namespace ApplySlideMaster 
{
    class Program
    {
        static void Main(string[] args)
        {
            Presentation ppt = new Presentation();
            ppt.LoadFromFile(@"sample.pptx");

            IMasterSlide masterSlide = ppt.Masters[0];
            string backgroundPic = "background.png";
            string logo = "logo.png";
            RectangleF rect = new RectangleF(0, 0, ppt.SlideSize.Size.Width, ppt.SlideSize.Size.Height);
            masterSlide.SlideBackground.Fill.FillType = FillFormatType.Picture;
            IEmbedImage image = masterSlide.Shapes.AppendEmbedImage(ShapeType.Rectangle, backgroundPic, rect);
            masterSlide.SlideBackground.Fill.PictureFill.Picture.EmbedImage = image as IImageData;
            masterSlide.Theme.ColorScheme.Accent1.Color = Color.Red;
            masterSlide.Theme.ColorScheme.Accent2.Color = Color.RosyBrown;
            masterSlide.Theme.ColorScheme.Accent3.Color = Color.Ivory;
            masterSlide.Theme.ColorScheme.Accent4.Color = Color.Lavender;
            masterSlide.Theme.ColorScheme.Accent5.Color = Color.Black;
            IEmbedImage imageShape = masterSlide.Shapes.AppendEmbedImage
            (ShapeType.Rectangle, logo, new RectangleF(40, 40, 240, 65));
            imageShape.Line.FillFormat.FillType = FillFormatType.None;

            ppt.SaveToFile("result.pptx", FileFormat.Pptx2013);

            }
        }
    }
[VB.NET]
Imports Spire.Presentation
Imports Spire.Presentation.Drawing
Imports System.Drawing
Namespace ApplySlideMaster
	Class Program
		Private Shared Sub Main(args As String())
			Dim ppt As New Presentation()
			ppt.LoadFromFile("sample.pptx")

			Dim masterSlide As IMasterSlide = ppt.Masters(0)
			Dim backgroundPic As String = "background.png"
			Dim logo As String = "logo.png"
			Dim rect As New RectangleF(0, 0, ppt.SlideSize.Size.Width, ppt.SlideSize.Size.Height)
			masterSlide.SlideBackground.Fill.FillType = FillFormatType.Picture
			Dim image As IEmbedImage = masterSlide.Shapes.AppendEmbedImage(ShapeType.Rectangle, backgroundPic, rect)
			masterSlide.SlideBackground.Fill.PictureFill.Picture.EmbedImage = TryCast(image, IImageData)
			masterSlide.Theme.ColorScheme.Accent1.Color = Color.Red
			masterSlide.Theme.ColorScheme.Accent2.Color = Color.RosyBrown
			masterSlide.Theme.ColorScheme.Accent3.Color = Color.Ivory
			masterSlide.Theme.ColorScheme.Accent4.Color = Color.Lavender
			masterSlide.Theme.ColorScheme.Accent5.Color = Color.Black
			Dim imageShape As IEmbedImage = masterSlide.Shapes.AppendEmbedImage(ShapeType.Rectangle, logo, New RectangleF(40, 40, 240, 65))
			imageShape.Line.FillFormat.FillType = FillFormatType.None

			ppt.SaveToFile("result.pptx", FileFormat.Pptx2013)

		End Sub
	End Class
End Namespace

Spire.Presentation supports to align text vertically in a table cell on the presentation slides. In this article, we will show you how to use C# to align the text in table cell and set the text direction. Firstly, view the screenshot on Microsoft PowerPoint of the vertical alignment and the text direction:

Vertically align the text in 

table cell

Detail steps:

Step 1: Create a new PowerPoint document and load the sample document from file.

Presentation ppt = new Presentation();
ppt.LoadFromFile("Sample1.pptx",FileFormat.Pptx2010);

Step 2: Get the table shape from the first presentation slide.

ITable table = null;
foreach (IShape shape in ppt.Slides[0].Shapes)
{
    if (shape is ITable)
    {
        table = (ITable)shape;

Step 3: Aligning the text vertically and set the text direction.

table[i, 0].TextAnchorType = TextAnchorType.Center;          
table[i, 0].VerticalTextType = VerticalTextType.Vertical270;

table[i, 1].TextAnchorType = TextAnchorType.Center;
table[i, 1].VerticalTextType = VerticalTextType.Horizontal;

table[i, 2].TextAnchorType = TextAnchorType.Center;
table[i, 2].VerticalTextType = VerticalTextType.Vertical;

Step 4: Save the document to file.

ppt.SaveToFile("Result.pptx", FileFormat.Pptx2010);

Effective screenshot after set the vertical alignment and the text direction:

Vertically align the text in 

table cell

Full codes of vertical align the text and set the text direction.:

using Spire.Presentation;

namespace AlignText
{

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

            Presentation ppt = new Presentation();

            ppt.LoadFromFile("Sample1.pptx", FileFormat.Pptx2010);

            ITable table = null;
            foreach (IShape shape in ppt.Slides[0].Shapes)
            {
                if (shape is ITable)
                {
                    table = (ITable)shape;

                    for (int i = 0; i < table.ColumnsList.Count; i++)
                    {
                        table[i, 0].TextAnchorType = TextAnchorType.Center;
                        table[i, 0].VerticalTextType = VerticalTextType.Vertical270;


                        table[i, 1].TextAnchorType = TextAnchorType.Center;
                        table[i, 1].VerticalTextType = VerticalTextType.Horizontal;

                        table[i, 2].TextAnchorType = TextAnchorType.Center;
                        table[i, 2].VerticalTextType = VerticalTextType.Vertical;

                    }
                }
            }

            ppt.SaveToFile("Result.pptx", FileFormat.Pptx2010);

        }
    }
}

With Spire.Presentation, we can programmatically set the Alternative Text for PowerPoint shapes along with get the Alternative Text of PowerPoint shapes. This article demonstrates how we can use Spire.Presentation to accomplish this function.

Detail steps:

Step 1: Instantiate a Presentation object and load the PowerPoint file.

Presentation ppt = new Presentation();
ppt.LoadFromFile("Input.pptx");

Step 2: Get the first slide.

ISlide slide = ppt.Slides[0];

Step 3: Set or get the alternative text of the first Shape in the slide.

//Set the alternative text (title and description)
slide.Shapes[0].AlternativeTitle = "Rectangle";
slide.Shapes[0].AlternativeText = "This is a Rectangle";

//Get the alternative text (title and description)
//string title = slide.Shapes[0].AlternativeTitle;
//string description = slide.Shapes[0].AlternativeText;

Step 4: Save the file.

ppt.SaveToFile("Output.pptx", FileFormat.Pptx2013);

Screenshot after setting alternative text:

Set and Get Alternative Text 

(Title and Description) of PowerPoint Shapes in C#

Full code:

using Spire.Presentation;

namespace Set_and_Get_Alternative_Text_of_Shape
{
    class Program
    {
        static void Main(string[] args)
        {
            //Instantiate a Presentation object
            Presentation ppt = new Presentation();
            //Load the PowerPoint file
            ppt.LoadFromFile("Input.pptx");

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

            //Set the alternative text (title and description)
            slide.Shapes[0].AlternativeTitle = "Rectangle";
            slide.Shapes[0].AlternativeText = "This is a Rectangle";

            //Get the alternative text (title and description)
            //string title = slide.Shapes[0].AlternativeTitle;
            //string description = slide.Shapes[0].AlternativeText;

            //Save the file
            ppt.SaveToFile("Output.pptx", FileFormat.Pptx2013);
        }
    }
}

Text watermark and image watermark are two kinds of watermarks in Word document. The text watermark always shows some additional but related information to the word context. While image watermark is used to make the Word document be more attractive. This section will demonstrate how to use Spire.Doc to add text watermark and image watermark to Word document in C#.

Add Image Watermark in C#:

using Spire.Doc;
namespace SetImageWatermark
{
    class Program
    {
        static void Main(string[] args)
        {
            {
                //create a new instance of Document and load the document from file.
                Document doc = new Document();
                doc.LoadFromFile("Sample.docx", FileFormat.Docx2013);

                //create a new instance of the PictureWatermark and load the picture from file.
                PictureWatermark picture = new PictureWatermark();
                picture.Picture = System.Drawing.Image.FromFile("logo.png");

                //set the image watermark scaling and Washout property
                picture.Scaling = 20;
                picture.IsWashout = false;

                //add the picture watermark
                doc.Watermark = picture;

                //save the document to file
                doc.SaveToFile("ImageWatermark.docx", FileFormat.Docx2013);

            }
        }
    }
}

Add text watermark and image watermark to word document in C#

Add Text Watermark in C#::

using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;
namespace SetTextWatermark
{
    class Program
    {
        static void Main(string[] args)
        {
            {
                //create a new instance of Document and load the document from file.
                Document doc = new Document();
                doc.LoadFromFile("Sample.docx", FileFormat.Docx2013);

                //create a new instance of the TextWatermark 
                TextWatermark txtWatermark = new TextWatermark();

                //set the text watermark with text string, font, color and layout.
                txtWatermark.Text = "Confidential";
                txtWatermark.FontSize = 45;
                txtWatermark.Color = Color.Green;
                txtWatermark.Layout = WatermarkLayout.Diagonal;

                //add the text watermark
                doc.Watermark = txtWatermark;

                //save the file.
                doc.SaveToFile("TextWatermark.docx", FileFormat.Docx2013);

            }
        }
    }
}

Add text watermark and image watermark to word document in C#

Friday, 08 December 2017 02:13

Conditionally Format Dates in Excel with C#

Microsoft Excel provides 10 date options, ranging from yesterday to next month (see below image) to format selected cells based on the current date. Spire.XLS supports all of these options, in this article, we’re going to show you how to conditionally format dates in Excel using Spire.XLS. If you want to highlight cells based on a date in another cell, or create rules for other dates (i.e., more than a month from the current date), you will have to create your own conditional formatting rule based on a formula.

Conditionally Format Dates in Excel with C#

Detail steps:

Step 1: Initialize an object of Workbook class 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: Add a condition to the used range in the worksheet.

ConditionalFormatWrapper conditionalFormat = sheet.AllocatedRange.ConditionalFormats.AddCondition();

Step 4: Specify the format type of the condition as time period and set the time period as last 7 days.

conditionalFormat.FormatType = ConditionalFormatType.TimePeriod;
conditionalFormat.SetTimePeriod(TimePeriodType.Last7Days);

Step 5:Set the highlight color.

conditionalFormat.BackColor = Color.Orange;

Step 6:Save the file.

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

Screenshot::

Conditionally Format Dates in Excel with C#

Full Code:

[C#]
using Spire.Xls;
using Spire.Xls.Core.Spreadsheet.ConditionalFormatting;
using System.Drawing;
namespace ConditionallyFormatDates
{
    class Program
    {
        static void Main(string[] args)
        {
            {

                //Initialize an object of Workbook class
                Workbook workbook = new Workbook();
                //Load the Excel file
                workbook.LoadFromFile("Input.xlsx");

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

                //Highlight cells that contain a date occurring in the last 7 days
                ConditionalFormatWrapper conditionalFormat = sheet.AllocatedRange.ConditionalFormats.AddCondition();
                conditionalFormat.FormatType = ConditionalFormatType.TimePeriod;
                conditionalFormat.SetTimePeriod(TimePeriodType.Last7Days);
                conditionalFormat.BackColor = Color.Orange;

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

        }
    }
}

It is possible to add text transparency to any text shape in PowerPoint. In order to make the text transparent, we’d need to apply the transparency level to the text shape. This article will show you how to set the transparency level of text using Spire.Presentation.

Step 1: Create a Presentation instance.

Presentation ppt = new Presentation();

Step 2: Add a shape to the first slide.

IAutoShape textboxShape = ppt.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(50, 70,300, 120));
textboxShape.ShapeStyle.LineColor.Color = Color.Transparent;
textboxShape.Fill.FillType = FillFormatType.None;

Step 3: Add text to shape.

textboxShape.TextFrame.Text = "Text Transparency";

Step 4: Set the fill type of TextRange to solid, and fill the TextRange using the color created with specified alpha value.

textboxShape.TextFrame.TextRange.Fill.FillType = FillFormatType.Solid;
int alpha = 55;
textboxShape.TextFrame.TextRange.Fill.SolidColor.Color = Color.FromArgb(alpha, Color.Purple);

Step 5:Save the file.

ppt.SaveToFile("result.pptx", FileFormat.Pptx2013);

Output:

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

Full Code:

[C#]
using Spire.Presentation;
using Spire.Presentation.Drawing;
using System.Drawing;
namespace ApplyTransparency
{
    class Program
    {
        static void Main(string[] args)
        {

            //create a PowerPoint document
            Presentation ppt = new Presentation();
            ppt.SlideSize.Type = SlideSizeType.Screen16x9;

            //add a shape
            IAutoShape textboxShape = ppt.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(50, 70, 300, 120));
            textboxShape.ShapeStyle.LineColor.Color = Color.Transparent;
            textboxShape.Fill.FillType = FillFormatType.None;

            //remove default blank paragraphs
            textboxShape.TextFrame.Paragraphs.Clear();

            //add three paragraphs, apply color with different alpha values to text
            int alpha = 55;
            for (int i = 0; i < 3; i++)
            {
                textboxShape.TextFrame.Paragraphs.Append(new TextParagraph());
                textboxShape.TextFrame.Paragraphs[i].TextRanges.Append(new TextRange("Text Transparency"));
                textboxShape.TextFrame.Paragraphs[i].TextRanges[0].Fill.FillType = FillFormatType.Solid;
                textboxShape.TextFrame.Paragraphs[i].TextRanges[0].Fill.SolidColor.Color = Color.FromArgb(alpha, Color.Purple);
                alpha += 100;
            }

            //save to file
            ppt.SaveToFile("result.pptx", FileFormat.Pptx2013); 

        }
    }
}
[VB.NET]
Imports Spire.Presentation
Imports Spire.Presentation.Drawing
Imports System.Drawing
Namespace ApplyTransparency
	Class Program
		Private Shared Sub Main(args As String())

			'create a PowerPoint document
			Dim ppt As New Presentation()
			ppt.SlideSize.Type = SlideSizeType.Screen16x9

			'add a shape
			Dim textboxShape As IAutoShape = ppt.Slides(0).Shapes.AppendShape(ShapeType.Rectangle, New RectangleF(50, 70, 300, 120))
			textboxShape.ShapeStyle.LineColor.Color = Color.Transparent
			textboxShape.Fill.FillType = FillFormatType.None

			'remove default blank paragraphs
			textboxShape.TextFrame.Paragraphs.Clear()

			'add three paragraphs, apply color with different alpha values to text
			Dim alpha As Integer = 55
			For i As Integer = 0 To 2
				textboxShape.TextFrame.Paragraphs.Append(New TextParagraph())
				textboxShape.TextFrame.Paragraphs(i).TextRanges.Append(New TextRange("Text Transparency"))
				textboxShape.TextFrame.Paragraphs(i).TextRanges(0).Fill.FillType = FillFormatType.Solid
				textboxShape.TextFrame.Paragraphs(i).TextRanges(0).Fill.SolidColor.Color = Color.FromArgb(alpha, Color.Purple)
				alpha += 100
			Next

			'save to file
			ppt.SaveToFile("result.pptx", FileFormat.Pptx2013)

		End Sub
	End Class
End Namespace
Tuesday, 29 August 2017 07:01

Set text style in word document

using NPOI.XWPF.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NPOI
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create document
            XWPFDocument doc = new XWPFDocument();

            //Create paragraph
            XWPFParagraph para1 = doc.CreateParagraph();
            para1.Alignment = ParagraphAlignment.CENTER;

            //Set style of the paragraph text
            XWPFRun run1 = para1.CreateRun();
            run1.SetColor("Blue");
            run1.FontSize = 18;
            run1.SetText("This is the first paragraph");
            run1.SetBold(true);

            XWPFParagraph para2 = doc.CreateParagraph();
            para2.Alignment = ParagraphAlignment.LEFT;
            para2.BorderTop = Borders.WAVE;   

            XWPFRun run2 = para2.CreateRun();
            run2.Subscript = VerticalAlign.BASELINE;            
            run2.SetColor("Red");
            run2.IsItalic = true;
            run2.SetUnderline(UnderlinePatterns.Dash);
            run2.SetText("This is the second paragraph");

            //Save the file
            using (FileStream file = File.Create("TextStyle.docx"))
            {
                doc.Write(file);
            }

            //Launch
            System.Diagnostics.Process.Start("TextStyle.docx");
        }
    }
}


Page 61 of 66