How to insert a new paragraph into an existing PDF using C# and .NET

  1. Add SautinSoft.Document from Nuget.
  2. Load a document.
  3. Find all centered paragraphs.
  4. Set its background to yellow.
  5. Save the document.

Input file:

Insert Paragraph input

Output result:

Insert Paragraph output

Complete code

using System;
using System.IO;
using System.Linq;
using SautinSoft.Document;

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get your free trial key here:   
            // https://sautinsoft.com/start-for-free/

            InsertParagraph();
        }
        /// <summary>
        /// Inserts a new paragraph into an existing PDF document.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/insert-paragraphs-to-pdf-document-net-csharp-vb.php
        /// </remarks>
        static void InsertParagraph()
        {
            string inpFile = @"..\..\..\example.pdf";
            string outFile = @"Result.pdf";

            DocumentCore dc = DocumentCore.Load(inpFile);
            Paragraph p = new Paragraph(dc);
            p.Content.Start.Insert("William Shakespeare is an English poet " +
                "and playwright, recognized as the greatest English-language writer, " +
                "the national poet of England and one of the outstanding playwrights of the world.",
                new CharacterFormat() { Size = 20, FontName = "Verdana", FontColor = new Color(53, 140, 203) });
            p.ParagraphFormat.Alignment = HorizontalAlignment.Justify;

            // Insert the paragraph as 1st element in the 1st section.
            dc.Sections[0].Blocks.Insert(0, p);

            dc.Save(outFile);
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile) { UseShellExecute = true });

        }
    }
}

Download

Imports System
Imports System.IO
Imports System.Linq
Imports SautinSoft.Document

Namespace Example
	Friend Class Program
		Shared Sub Main(ByVal args() As String)
			' Get your free trial key here:   
			' https://sautinsoft.com/start-for-free/

			InsertParagraph()
		End Sub
		''' <summary>
		''' Inserts a new paragraph into an existing PDF document.
		''' </summary>
		''' <remarks>
		''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/insert-paragraphs-to-pdf-document-net-csharp-vb.php
		''' </remarks>
		Private Shared Sub InsertParagraph()
			Dim inpFile As String = "..\..\..\example.pdf"
			Dim outFile As String = "Result.pdf"

			Dim dc As DocumentCore = DocumentCore.Load(inpFile)
			Dim p As New Paragraph(dc)
			p.Content.Start.Insert("William Shakespeare is an English poet " & "and playwright, recognized as the greatest English-language writer, " & "the national poet of England and one of the outstanding playwrights of the world.", New CharacterFormat() With {
				.Size = 20,
				.FontName = "Verdana",
				.FontColor = New Color("#358CCB")
			})
			p.ParagraphFormat.Alignment = HorizontalAlignment.Justify

			' Insert the paragraph as 1st element in the 1st section.
			dc.Sections(0).Blocks.Insert(0, p)

			dc.Save(outFile)
			System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(outFile) With {.UseShellExecute = True})

		End Sub
	End Class
End Namespace

Download


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.