iPdfWriter 1.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package iPdfWriter --version 1.0.2
NuGet\Install-Package iPdfWriter -Version 1.0.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="iPdfWriter" Version="1.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add iPdfWriter --version 1.0.2
#r "nuget: iPdfWriter, 1.0.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install iPdfWriter as a Cake Addin
#addin nuget:?package=iPdfWriter&version=1.0.2

// Install iPdfWriter as a Cake Tool
#tool nuget:?package=iPdfWriter&version=1.0.2

What is iPdfWriter?

iPdfWriter is a lightweight implementation that allows modifying a pdf document totally or partially by replacing tags.

The idea is to try to quickly and easily facilitate the task of filling in the 'typical' report file that the client wants to send by email with the data filled in from their erp, vertical application, custom development, etc... to which I am sure that you have faced each other at some point.

I hope it helps someone. 😉

Usage

Samples

Sample 1 - Shows the use of text and image replacement with styles

Basic steps, for more details please see sample01.cs file.

  1. Load pdf file

    var doc = new PdfInput
    {
        AutoUpdateChanges = true,
        Input = "~/Resources/Sample-01/file-sample.pdf"
    };
    
  2. Define the styles to use

    Text styles

    private static readonly Dictionary<string, PdfTextStyle> TextStylesTable = new Dictionary<string, PdfTextStyle>
    {
        "ReportTitle",
        new PdfTextStyle
        {
            Font =
            {
                Name = "Arial",
                Size = 28.0f,
                Bold = YesNo.Yes,
                Italic = YesNo.Yes,
                Color = "Blue"
            },
            Content =
            {
                Alignment =
                {
                    Vertical = KnownVerticalAlignment.Center,
                    Horizontal = KnownHorizontalAlignment.Center
                }
            }
        }
    };         
    

    Image styles

    private static readonly Dictionary<string, PdfImageStyle> ImagesStylesTable = new Dictionary<string, PdfImageStyle>
    {           
        {
            "Default",
            new PdfImageStyle { Content = { Alignment = { Horizontal = KnownHorizontalAlignment.Left }}}
        }
    };                
    
  3. Replace #TITLE# tag with another text

    doc.Replace(new ReplaceText(
        new WithTextObject
        {
            Text = "#TITLE#",
            NewText = "Lorem ipsum",
            UseTestMode = useTestMode,
            TextOffset = PointF.Empty,
            Style = TextStylesTable["ReportTitle"],
            ReplaceOptions = ReplaceTextOptions.AccordingToMargins
        }));            
    
  4. Replace #BAR-CHART# tag with an image

    using (var barGraph = PdfImage.FromFile("~/Resources/Sample-01/Images/bar-chart.png"))
    {
        doc.Replace(new ReplaceText(
            new WithImageObject
            {
                Text = "#BAR-CHART#",
                UseTestMode = useTestMode,
                ImageOffset = PointF.Empty,
                Style = ImagesStylesTable["Default"],
                ReplaceOptions = ReplaceTextOptions.Default,
                Image = barGraph
            }));
    }
    
  5. Try to create pdf output result

    var result = doc.CreateResult();
    if (!result.Success)
    {
        // Handle errors                 
    }
    
  6. Save pdf file result

    var saveResult = result.Result.Action(new SaveToFile { OutputPath = "~/Output/Sample01/Sample-01" });
    if (!saveResult.Success)
    {
         // Handle errors                 
    }
    

Sample 2 - Shows the use of html table replacement in a pdf document

Basic steps, for more details please see sample02.cs file.

  1. Load pdf file

    var doc = new PdfInput
    {
        AutoUpdateChanges = true,
        Input = "~/Resources/Sample-02/file-sample.pdf"
    };
    
  2. Define HTML table with embbebed styles

    private static string GenerateHtmlDatatable()
    {
        var result = new StringBuilder();
        result.AppendLine("<table border='1' cellspacing='0' cellpadding='6' style='width:100%'>");
        result.AppendLine(" <tbody>");
        result.AppendLine("  <tr style='font-size:10.5pt; font-family:Arial; color:#404040; text-align: left;'>");
        result.AppendLine("    <td>&nbsp;</td>");
        result.AppendLine("    <td>Lorem ipsum</td>");
        result.AppendLine("    <td>Lorem ipsum</td>");
        result.AppendLine("    <td>Lorem ipsum</td>");
        result.AppendLine("  </tr>");
        result.AppendLine("  <tr style='font-size:10.5pt; font-family:Arial; color:#404040; text-align: left;'>");
        result.AppendLine("    <td>1</td>");
        result.AppendLine("    <td>In eleifend velit vitae libero sollicitudin euismod.</td>");
        result.AppendLine("    <td>Lorem</td>");
        result.AppendLine("    <td>&nbsp;</td>");
        result.AppendLine("  </tr>");
        result.AppendLine("  <tr style='font-size:10.5pt; font-family:Arial; color:#404040; text-align: left;'>");
        result.AppendLine("    <td>2</td>");
        result.AppendLine("    <td>Cras fringilla ipsum magna, in fringilla dui commodo a.</td>");
        result.AppendLine("    <td>Lorem</td>");
        result.AppendLine("    <td>&nbsp;</td>");
        result.AppendLine("  </tr>");
        result.AppendLine("  <tr style='font-size:10.5pt; font-family:Arial; color:#404040; text-align: left;'>");
        result.AppendLine("    <td>3</td>");
        result.AppendLine("    <td>LAliquam erat volutpat.</td>");
        result.AppendLine("    <td>Lorem</td>");
        result.AppendLine("    <td>&nbsp;</td>");
        result.AppendLine("  </tr>");
        result.AppendLine("  <tr style='font-size:10.5pt; font-family:Arial; color:#404040; text-align: left;'>");
        result.AppendLine("    <td>4</td>");
        result.AppendLine("    <td>Fusce vitae vestibulum velit. </td>");
        result.AppendLine("    <td>Lorem</td>");
        result.AppendLine("    <td>&nbsp;</td>");
        result.AppendLine("  </tr>");
        result.AppendLine("  <tr style='font-size:10.5pt; font-family:Arial; color:#404040; text-align: left;'>");
        result.AppendLine("    <td>5</td>");
        result.AppendLine("    <td>Etiam vehicula luctus fermentum.</td>");
        result.AppendLine("    <td>Ipsum</td>");
        result.AppendLine("    <td>&nbsp;</td>");
        result.AppendLine(" </tr>");
        result.AppendLine(" </tbody>");
        result.AppendLine("</table>");
    
        return result.ToString();
    }
    
  3. Replace #DATA-TABLE# tag with a HTML table

    doc.Replace(new ReplaceText(
        new WithTableObject
        {
            Text = "#DATA-TABLE#",
            UseTestMode = useTestMode,
            TableOffset = PointF.Empty,
            Style = PdfTableStyle.Default,
            ReplaceOptions = ReplaceTextOptions.FromPositionToRightMargin,
            Table = PdfTable.CreateFromHtml(GenerateHtmlDatatable())
        }));
    
  4. Try to create pdf output result

    var result = doc.CreateResult();
    if (!result.Success)
    {
        // Handle errors                 
    }
    
  5. Save pdf result to file

    var saveResult = result.Result.Action(new SaveToFile { OutputPath = "~/Output/Sample02/Sample-02" });
    if (!saveResult.Success)
    {
         // Handle errors                 
    }
    

Sample 3 - Shows the use of merge action

Basic steps, for more details please see sample03.cs file.

  1. Load pdf pages

    Page 1

    var page1 = new PdfInput
    {
        AutoUpdateChanges = true,
        Input = "~/Resources/Sample-03/file-sample-1.pdf"
    };
    

    Page 2

    var page2 = new PdfInput
    {
        AutoUpdateChanges = true,
        Input = "~/Resources/Sample-03/file-sample-2.pdf"
    };
    

    Page 3

    var page3 = new PdfInput
    {
        AutoUpdateChanges = true,
        Input = "~/Resources/Sample-03/file-sample-3.pdf"
    };
    

    Page 4

    var page4 = new PdfInput
    {
        AutoUpdateChanges = true,
        Input = "~/Resources/Sample-03/file-sample-4.pdf"
    };
    
  2. Replace Tags

    Replace the elements in the pages, for reasons of space I omit this step, We would do it as we have seen in examples 1 and 2.

  3. Create a list of elements to merge

    Note that you can set the order in which they will be merged.

    var files = new PdfObject
    {
        Items = new List<PdfInput>
        {
            new PdfInput {Index = 0, Input = page1},
            new PdfInput {Index = 1, Input = page2},
            new PdfInput {Index = 2, Input = page3},
            new PdfInput {Index = 3, Input = page4}
        }
    };
    
  4. Try to merge into a pdf output result

    var mergeResult = files.TryMergeInputs();
    if (!mergeResult.Success)
    {
        // Handle errors                 
    }
    
  5. Save merged result to file

    var saveResult =  mergeResult.Result.Action(new SaveToFile { OutputPath = "~/Output/Sample03/Sample-03" });
    if (!saveResult.Success)
    {
         // Handle errors                 
    }
    
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.1 is compatible.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 is compatible. 
.NET Framework net461 is compatible.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.8 2,216 6/28/2023
1.0.6 2,867 12/2/2022
1.0.5 2,727 11/24/2022
1.0.4 1,703 8/3/2022
1.0.3 2,228 8/2/2022
1.0.2 2,517 6/24/2022
1.0.1 1,511 6/23/2022
1.0.0 1,728 9/27/2020