page 169

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#]
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;


namespace Image
{
    class Program
    {
        static void Main(string[] args)
        {
            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]
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports System.Drawing


Namespace Image
	Class Program
		Private Shared Sub Main(args As String())
			Dim pdf As 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 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")
		End Sub
	End Class
End Namespace

When processing a Word document, you may need to remove some paragraphs. For example, after you copied contents from the Internet with a lot of redundant paragraphs to your document, you need to delete the extra paragraphs and keep only those that are useful. The deletion can be easily achieved by Spire.Doc for .NET by programming with no need for other software. This article will show you the detailed steps of removing paragraphs in a Word document using Spire.Doc for .NET.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Delete a Specific Paragraph in a Word Document

Spire.Doc for .NET provides a method RemoveAt() under ParagraphCollection to remove paragraphs.

The detailed steps of removing a specific paragraph are as follows:

  • Create an object of Document class.
  • Load a Word document using Document.LoadFromFile() method.
  • Get the first section using Document.Section[] property.
  • Remove the 4th paragraph using Section.Paragraphs.RemoveAt() method.
  • Save the document using Document.SaveToFile() method.
  • C#
  • VB.NET
using System;
using Spire.Doc;

namespace RemoveParagraphs
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Create an object of Document class
            Document document = new Document();

            //Load a Word document
            document.LoadFromFile("Sample.docx");

            //Get the first section
            Section section = document.Sections[0];

            //Remove the first paragraph in the section
            section.Paragraphs.RemoveAt(3);
            
            //Save the document
            document.SaveToFile("RemoveParagraphs.docx", FileFormat.Docx2013);
        }
    }
}

C#/VB.NET: Remove Paragraphs in a Word Document

Delete All Paragraphs in a Word Document

To remove all paragraphs, you can use the method Clear() under ParagraphCollection provided by Spire.Doc for .NET.

The detailed steps are as follows:

  • Create an object of Document class.
  • Load a Word Document using Document.LoadFromFile() method.
  • Loop through all sections, and remove all paragraphs in each section using Section.Paragraphs.Clear() method.
  • Save the document using Document.SaveToFile() method.
  • C#
  • VB.NET
using System;
using Spire.Doc;

namespace RemoveAllParagraphs
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Create an object of Document class
            Document document = new Document();

            //Load a Word document
            document.LoadFromFile("Sample.docx");

            //Loop through all sections
            foreach (Section section in document.Sections)
            {
                //Remove all paragraphs in the section
                section.Paragraphs.Clear();
            }

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

C#/VB.NET: Remove Paragraphs in a Word 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.

Grouping and ungrouping in PowerPoint are two useful features when working with shapes. Grouping allows you to join multiple shapes together so you can move, format, resize, and rotate them at once as if they were a single shape. Ungrouping lets you break the connection between grouped shapes so you can work on them individually again. In this article, you will learn how to use Spire.Presentation for .NET to group or ungroup shapes in PowerPoint in C# and VB.NET.

Install Spire.Presentation for .NET

To begin with, you need to add the DLL files included in the Spire.Presentation for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Presentation

Group Shapes in PowerPoint in C# and VB.NET

Spire.Presentation for .NET provides the ISlide.GroupShapes(ArrayList shapeList) method to group two or more shapes on a specific slide. The following are the detailed steps:

  • Initialize an instance of the Presentation class.
  • Get the first slide by its index through Presentation.Slides[0] property.
  • Add two shapes to the slide using ISlide.Shapes.AppendShape() method.
  • Initialize an instance of the ArrayList class.
  • Add the two shapes to the ArrayList.
  • Group the two shapes in the ArrayList using ISlide.GroupShapes(ArrayList shapeList) method.
  • Save the result document using Presentation.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Presentation;
using Spire.Presentation.Drawing;
using System.Collections;
using System.Drawing;

namespace GroupShapes
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Initialize an instance of the Presentation class
            Presentation ppt = new Presentation();
            //Get the first slide
            ISlide slide = ppt.Slides[0];

            //Add two shapes to 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;

            //Initialize an instance of the ArrayList class
            ArrayList list = new ArrayList();
            //Add the two shapes to the ArrayList
            list.Add(rectangle);
            list.Add(ribbon);

            //Group the two shapes
            slide.GroupShapes(list);

            //Save the result document
            ppt.SaveToFile("GroupShapes.pptx", FileFormat.Pptx2010);
            ppt.Dispose();            
        }
    }
}

C#/VB.NET: Group or Ungroup Shapes in PowerPoint

Ungroup Shapes in PowerPoint in C# and VB.NET

To ungroup the grouped shapes in a PowerPoint document, you need to iterate through all slides in the document and all shapes on each slide, find the grouped shapes and then ungroup them using ISlide.Ungroup(GroupShape) method. The following are the detailed steps:

  • Initialize an instance of the Presentation class.
  • Load the PowerPoint document using Presentation.LoadFromFile() method.
  • Iterate through all slides in the document.
  • Iterate through all shapes on each slide.
  • Check if the current shape is of GroupShape type. If the result is true, ungroup it using ISlide.Ungroup(GroupShape) method.
  • Save the result document using Presentation.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Presentation;
using Spire.Presentation.Drawing;
using System.Collections;
using System.Drawing;

namespace UngroupShapes
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Initialize an instance of the Presentation class
            Presentation ppt = new Presentation();
            //Load the PowerPoint document
            ppt.LoadFromFile("GroupShapes.pptx");

            //Iterate through all slides in the document 
            for (int i = 0; i < ppt.Slides.Count; i++)
            {
                ISlide slide = ppt.Slides[i];
                //Iterate through all shapes on each slide
                for (int j = 0; j < slide.Shapes.Count; j++)
                {
                    IShape shape = slide.Shapes[j];
                    //Detect if the shape is a grouped shape
                    if (shape is GroupShape)
                    {
                        GroupShape groupShape = shape as GroupShape;
                        //Ungroup the grouped shape
                        slide.Ungroup(groupShape);
                    }
                }
            }

            //Save the result document
            ppt.SaveToFile("UngroupShapes.pptx", FileFormat.Pptx2013);
            ppt.Dispose();
        }
    }
}

C#/VB.NET: Group or Ungroup Shapes in PowerPoint

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.

page 169