Tuesday, 24 July 2018 07:10

Replace Text with a Word document in C#

Spire.Doc provides several overloaded Replace methods to replace text in different scenarios. This article is going to show you how to replace a specified text in a template document with another document using Spire.Doc.

The template document:

Replace Text with a Word document in C#

The document to replace text:

Replace Text with a Word document in C#

Detail steps:

Step 1: Load a template document.

Document document = new Document("Template.docx");

Step 2: Load another document to replace text.

IDocument replaceDocument = new Document("Document1.docx");

Step 3: Replace specified text with the other document.

document.Replace("Document 1", replaceDocument, false, true);  

Step 4: Save the file.

document.SaveToFile("Output.docx", FileFormat.Docx2013);

Output:

Replace Text with a Word document in C#

Full code:

using Spire.Doc;
using Spire.Doc.Interface;

namespace Replace_Text_With_Document
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load a template document 
            Document document = new Document("Template.docx");

            //Load another document to replace text
            IDocument replaceDocument = new Document("Document1.docx");

            //Replace specified text with the other document
            document.Replace("Document 1", replaceDocument, false, true);

            //Save the file
            document.SaveToFile("Output.docx", FileFormat.Docx2013);
        }
    }
}

The following code snippets show you how we can find x and y coordinates of an image in a PDF document.

Step 1: Create a PdfDocument object and load a sample PDF file.

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

Step 2: Get all the image information from the specified page.

PdfImageInfo[] imageInfo = doc.Pages[0].ImagesInfo;

Step 3: Get the x and y coordinates of the first image through Bounds property.

RectangleF rect = imageInfo[0].Bounds;
Console.WriteLine("The image is located at({0},{1})", rect.X, rect.Y);

Output:

How to Get X/Y Position of Image in PDF in C#, VB.NET

Full Code:

[C#]
PdfDocument doc = new PdfDocument();
doc.LoadFromFile("sample.pdf");
PdfImageInfo[] imageInfo = doc.Pages[0].ImagesInfo;
RectangleF rect = imageInfo[0].Bounds;
Console.WriteLine("The image is located at({0},{1})", rect.X, rect.Y);
[VB.NET]
Dim doc As PdfDocument = New PdfDocument()
doc.LoadFromFile("sample.pdf")
Dim imageInfo As PdfImageInfo() = doc.Pages(0).ImagesInfo
Dim rect As RectangleF = imageInfo(0).Bounds
Console.WriteLine("The image is located at({0},{1})", rect.X, rect.Y)

This article elaborates the steps to apply soft edges effect to an excel chart using Spire.XLS.

The example Excel file we used for demonstration:

Apply Soft Edges effect to Excel Chart 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: Get the chart.

IChart chart = sheet.Charts[0];

Step 4: Specify the size of the soft edge. Value can be set from 0 to 100.

chart.ChartArea.Shadow.SoftEdge = 10;

Step 5: Save the file.

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

Output:

Apply Soft Edges effect to Excel Chart in C#

Full code:

using Spire.Xls;
using Spire.Xls.Core;

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

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

            //Get the chart
            IChart chart = sheet.Charts[0];

            //Specify the size of the soft edge. Value can be set from 0 to 100
            chart.ChartArea.Shadow.SoftEdge = 10;

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

Hyperlinks in a PowerPoint file not only can be linked to external URLs, but also to the specific slide within the document. This article will show you how to create a hyperlink that links to a specified slide using Spire.Presentation.

Step 1: Create a PowerPoint file and append a slide to it.

Presentation presentation = new Presentation();
presentation.Slides.Append();

Step 2: Add a shape to the second slide.

IAutoShape shape = presentation.Slides[1].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(10, 50, 200, 50));
shape.TextFrame.Text = "Jump to the first slide"; 

Step 3: Create a hyperlink based on the shape and the text on it, linking to the first slide .

ClickHyperlink hyperlink = new ClickHyperlink(presentation.Slides[0]);
shape.Click = hyperlink;
shape.TextFrame.TextRange.ClickAction = hyperlink;

Step 4: Save the file.

presentation.SaveToFile("hyperlink.pptx", FileFormat.Pptx2010);

Output:

How to Link to a Specific Slide in PowerPoint in C#, VB.NET

Full Code:

[C#]
Presentation presentation = new Presentation();
presentation.Slides.Append();
IAutoShape shape = presentation.Slides[1].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(10, 50, 200, 50));
shape.Fill.FillType = FillFormatType.None;
shape.Line.FillType = FillFormatType.None;
shape.TextFrame.Text = "Jump to the first slide";
ClickHyperlink hyperlink = new ClickHyperlink(presentation.Slides[0]);
shape.Click = hyperlink;
shape.TextFrame.TextRange.ClickAction = hyperlink;
presentation.SaveToFile("output.pptx", FileFormat.Pptx2010);
[VB.NET]
Dim presentation As Presentation = New Presentation()
presentation.Slides.Append()
Dim shape As IAutoShape = presentation.Slides(1).Shapes.AppendShape(ShapeType.Rectangle, New RectangleF(10, 50, 200, 50))
shape.Fill.FillType = FillFormatType.None
shape.Line.FillType = FillFormatType.None
shape.TextFrame.Text = "Jump to the first slide"
Dim hyperlink As ClickHyperlink = New ClickHyperlink(presentation.Slides(0))
shape.Click = hyperlink
shape.TextFrame.TextRange.ClickAction = hyperlink
presentation.SaveToFile("output.pptx", FileFormat.Pptx2010)

A tiled background usually refers to the background that is filled with one or more repetitions of a small image. In this article, you will learn how to tile an image in PDF and make a tile background for your PDFs in C# and VB.NET.

Step 1: Create a PdfDocument object and load a sample PDF document.

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

Step 2: Load an image file to PdfImage object.

PdfImage image = PdfImage.FromFile("logo.png");

Step 3: Create a PdfTilingBrush object specifying its size, set the transparency of the brush, and draw an image at the specified position of the brush.

PdfTilingBrush brush = new PdfTilingBrush(new SizeF(pdf.Pages[1].Canvas.Size.Width / 3, pdf.Pages[1].Canvas.Size.Height / 5));
brush.Graphics.SetTransparency(0.2f);
brush.Graphics.DrawImage(image,new PointF((brush.Size.Width-image.Width)/2,(brush.Size.Height-image.Height)/2));

Step 4: Draw rectangles on the PDF page using the brush.

pdf.Pages[1].Canvas.DrawRectangle(brush, new RectangleF(new PointF(0, 0), page.Canvas.Size));

Step 5: Save the file.

pdf.SaveToFile("output.pdf");

Output:

How to Add a Tiled Background Image to PDF in C#, VB.NET

Full Code:

[C#]
PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("sample.pdf");

PdfImage image = PdfImage.FromFile("logo.png");
foreach (PdfPageBase page in pdf.Pages)
{
    PdfTilingBrush brush = new PdfTilingBrush(new SizeF(page.Canvas.Size.Width / 3, page.Canvas.Size.Height / 5));
    brush.Graphics.SetTransparency(0.2f);
    brush.Graphics.DrawImage(image,new PointF((brush.Size.Width-image.Width)/2,(brush.Size.Height-image.Height)/2));
    page.Canvas.DrawRectangle(brush, new RectangleF(new PointF(0, 0), page.Canvas.Size));
}
pdf.SaveToFile("output.pdf");
[VB.NET]
Dim pdf As PdfDocument = New PdfDocument()
pdf.LoadFromFile("sample.pdf")
Dim image As PdfImage = PdfImage.FromFile("logo.png")
For Each page As PdfPageBase In pdf.Pages
    Dim brush As PdfTilingBrush = New PdfTilingBrush(New SizeF(page.Canvas.Size.Width / 3, page.Canvas.Size.Height / 5))
    brush.Graphics.SetTransparency(0.2F)
    brush.Graphics.DrawImage(image, New PointF((brush.Size.Width - image.Width) / 2, (brush.Size.Height - image.Height) / 2))
    page.Canvas.DrawRectangle(brush, New RectangleF(New PointF(0, 0), page.Canvas.Size))
Next
pdf.SaveToFile("output.pdf")

Spire.Doc supports to remove a specific paragraph along with all of the paragraphs from a word document. This article elaborates how we can remove paragraphs from a word document using Spire.Doc and C#.

The example word document we used:

Remove Paragraphs from Word document in C#

Remove a specific paragraph

//Instantiate a Document object 
Document document = new Document();
//Load the Word document
document.LoadFromFile("Input.docx");

//Remove the first paragraph from the first section of the document
document.Sections[0].Paragraphs.RemoveAt(0);

//Save the document
document.SaveToFile("RemoveParagraph.docx", FileFormat.Docx2013);

Output:

Remove Paragraphs from Word document in C#

Remove all of the paragraphs

//Instantiate a Document object
Document document = new Document();
//Load the Word document
document.LoadFromFile("Input.docx");

//Remove paragraphs from every section in the document
foreach(Section section in document.Sections)
{
    section.Paragraphs.Clear();
}

//Save the document
document.SaveToFile("RemoveAllParagraphs.docx", FileFormat.Docx2013);

Output:

Remove Paragraphs from Word document in C#

You can group shapes so that they function as a unit. It's useful to group shapes if you regularly use them together. In this article, you will learn how to create grouped shapes in a PowerPoint slide, by using Spire.Presentation with C# and VB.NET.

Step 1: Create a PowerPoint document and get the first the slide.

Presentation ppt = new Presentation();
ISlide slide= ppt.Slides[0];

Step 2: Create two shapes in the slide.

IShape rectangle = slide.Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(20, 100, 200, 40));
rectangle.Fill.FillType = FillFormatType.Solid;
rectangle.Fill.SolidColor.KnownColor = KnownColors.Gold;
rectangle.Line.Width = 0.1f;
IShape ribbon = slide.Shapes.AppendShape(ShapeType.Ribbon2, new RectangleF(60, 75, 120, 80));
ribbon.Fill.FillType = FillFormatType.Solid;
ribbon.Fill.SolidColor.KnownColor = KnownColors.Purple;
ribbon.Line.Width = 0.1f;

Step 3: Add the two shape objects to an array list.

ArrayList list = new ArrayList();
list.Add(rectangle);
list.Add(ribbon);

Step 4: Grope the shapes in the list.

ppt.Slides[0].GroupShapes(list);

Step 5: Save to file.

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

Output:

How to Group Shapes in PowerPoint in C#, VB.NET

Full Code:

[C#]
Presentation ppt = new Presentation();
ISlide slide= ppt.Slides[0];

IShape rectangle = slide.Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(20, 100, 200, 40));
rectangle.Fill.FillType = FillFormatType.Solid;
rectangle.Fill.SolidColor.KnownColor = KnownColors.Gold;
rectangle.Line.Width = 0.1f;
IShape ribbon = slide.Shapes.AppendShape(ShapeType.Ribbon2, new RectangleF(60, 75, 120, 80));
ribbon.Fill.FillType = FillFormatType.Solid;
ribbon.Fill.SolidColor.KnownColor = KnownColors.Purple;
ribbon.Line.Width = 0.1f;

ArrayList list = new ArrayList();
list.Add(rectangle);
list.Add(ribbon);
ppt.Slides[0].GroupShapes(list);
ppt.SaveToFile("output.pptx", FileFormat.Pptx2010);
[VB.NET]
Dim ppt As Presentation = New Presentation()
Dim slide As ISlide = ppt.Slides(0)
Dim rectangle As IShape = slide.Shapes.AppendShape(ShapeType.Rectangle, New RectangleF(20, 100, 200, 40))
rectangle.Fill.FillType = FillFormatType.Solid
rectangle.Fill.SolidColor.KnownColor = KnownColors.Gold
rectangle.Line.Width = 0.1F
Dim ribbon As IShape = slide.Shapes.AppendShape(ShapeType.Ribbon2, New RectangleF(60, 75, 120, 80))
ribbon.Fill.FillType = FillFormatType.Solid
ribbon.Fill.SolidColor.KnownColor = KnownColors.Purple
ribbon.Line.Width = 0.1F
Dim list As ArrayList = New ArrayList()
list.Add(rectangle)
list.Add(ribbon)
ppt.Slides(0).GroupShapes(list)
ppt.SaveToFile("output.pptx", FileFormat.Pptx2010)

Spire.Presentation provides a property "IsHidden" to enable developers to hide or show a specific series of a chart. This article demonstrates how we can accomplish this function using Spire.Presentation and C#.

Below is the screenshot of the original chart:

Hide or Show a Series of a Chart in PowerPoint in C#

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: Get the first chart.

IChart chart = slide.Shapes[0] as IChart;

Step 4: Hide the first series of the chart.

//Hide the first series of the chart
chart.Series[0].IsHidden = true;

//Show the first series of the chart
//chart.Series[0].IsHidden = false;

Step 5: Save the file.

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

Screenshot after hiding the first series:

Hide or Show a Series of a Chart in PowerPoint in C#

using Spire.Presentation;
using Spire.Presentation.Charts;

namespace Hide_Chart_Series_in_PPT
{
    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];

            //Get the first chart
            IChart chart = slide.Shapes[0] as IChart;

            //Hide the first series of the chart
            chart.Series[0].IsHidden = true;

            //Show the first series of the chart
            //chart.Series[0].IsHidden = false;

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

By using Spire.Doc, you can not only retrieve the style names of all paragraphs in a Word document, but also get the paragraphs with a specific style name. This is useful especially when you need to get the text in Title, Heading 1, Subtitle, etc.

Paragraph Style Names in Word Paragraph Style Names in Spire.Doc
Title Title
Heading 1 Heading1
Heading 2 Heading2
Heading 3 Heading3
Heading 4 Heading3
Subtitle Subtitle

Step 1: Load a sample Word file when initializing the Document object.

Document doc = new Document("sample.docx");

Step 2: Traverse the sections and paragraphs in the document and determine if the paragraph style name is "Heading1", if so, write the paragraph text on screen.

foreach (Section section in doc.Sections)
{
    foreach (Paragraph paragraph in section.Paragraphs)
    {
        if (paragraph.StyleName == "Heading1")
        {
            Console.WriteLine(paragraph.Text);
        }
    }
}

Output:

Get Paragraphs by Style Name in Word in C#, VB.NET

Full Code:

[C#]
Document doc = new Document("sample.docx");
foreach (Section section in doc.Sections)
{
    foreach (Paragraph paragraph in section.Paragraphs)
    {
        if (paragraph.StyleName == "Heading1")
        {
            Console.WriteLine(paragraph.Text);
        }
    }
}
[VB.NET]
Dim doc As Document = New Document("sample.docx")
For Each section As Section In doc.Sections
    For Each paragraph As Paragraph In section.Paragraphs
        If paragraph.StyleName = "Heading1" Then
            Console.WriteLine(paragraph.Text)
        End If
    Next
Next

Spire.PDF supports to delete rows or columns from a PDF grid before drawing it onto a PDF page. This article demonstrates the detail steps of how to delete a row and a column from a PDF grid using Spire.PDF.

Detail steps:

Step 1: Create a PDF document and add a page to it.

PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add();

Step 2: Create a PDF grid.

PdfGrid grid = new PdfGrid();
//Set cell padding
grid.Style.CellPadding = new PdfPaddings(3, 3, 1, 1);

Step 3: Add 3 rows and 4 columns to the grid.

PdfGridRow row1 = grid.Rows.Add();
PdfGridRow row2 = grid.Rows.Add();
PdfGridRow row3 = grid.Rows.Add();

grid.Columns.Add(4);

Step 4: Set columns' width.

foreach (PdfGridColumn column in grid.Columns)
{
    column.Width = 60f;
}

Step 5: Add values to grid cells.

for (int i = 0; i < grid.Columns.Count; i++)
{
    row1.Cells[i].Value = String.Format("column{0}", i + 1);
    row2.Cells[i].Value = "a";
    row3.Cells[i].Value = "b";
}

Step 6: Delete the second row and the second column from the grid.

grid.Rows.RemoveAt(1);

grid.Columns.RemoveAt(1);

Step 7: Draw the grid onto the page and save the file.

grid.Draw(page, new PointF(0, 20));

doc.SaveToFile("Output.pdf");

Output:

Delete Rows and Columns from a PDF Grid in C#

Full code:

using System;
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Grid;

namespace Delete_Row_and_Column_from_PDFGrid
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PDF document
            PdfDocument doc = new PdfDocument();

            //Add a page
            PdfPageBase page = doc.Pages.Add();

            //Create a PDF grid
            PdfGrid grid = new PdfGrid();

            //Set cell padding
            grid.Style.CellPadding = new PdfPaddings(3, 3, 1, 1);

            //Add 3 rows and 4 columns to the grid
            PdfGridRow row1 = grid.Rows.Add();
            PdfGridRow row2 = grid.Rows.Add();
            PdfGridRow row3 = grid.Rows.Add();
            grid.Columns.Add(4);

            //Set columns’ width
            foreach (PdfGridColumn column in grid.Columns)
            {
                column.Width = 60f;
            }

            //Add values to grid cells
            for (int i = 0; i < grid.Columns.Count; i++)
            {
                row1.Cells[i].Value = String.Format("column{0}", i + 1);
                row2.Cells[i].Value = "a";
                row3.Cells[i].Value = "b";
            }

            //Delete the second row
            grid.Rows.RemoveAt(1);

            //Delete the second column
            grid.Columns.RemoveAt(1);

            //Draw the grid to the page
            grid.Draw(page, new PointF(0, 20));

            //Save the file
            doc.SaveToFile("Output.pdf");
        }
    }
}
Page 2 of 96