iPdfWriter 1.0.5
See the version list below for details.
dotnet add package iPdfWriter --version 1.0.5
NuGet\Install-Package iPdfWriter -Version 1.0.5
<PackageReference Include="iPdfWriter" Version="1.0.5" />
paket add iPdfWriter --version 1.0.5
#r "nuget: iPdfWriter, 1.0.5"
// Install iPdfWriter as a Cake Addin #addin nuget:?package=iPdfWriter&version=1.0.5 // Install iPdfWriter as a Cake Tool #tool nuget:?package=iPdfWriter&version=1.0.5
What is iPdfWriter?
iPdfWriter is a lightweight implementation that allows modifying a pdf document totally or partially by replacing tags. Currently only works on windows
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.
Load pdf file
var doc = new PdfInput { AutoUpdateChanges = true, Input = "~/Resources/Sample-01/file-sample.pdf" };
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 }}} } };
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 }));
Replace #BAR-CHART# tag with an image
doc.Replace(new ReplaceText( new WithImageObject { Text = "#BAR-CHART#", UseTestMode = useTestMode, Offset = PointF.Empty, Style = StylesHelper.Sample01.ImagesStylesTable["Default"], ReplaceOptions = ReplaceTextOptions.Default, Image = PdfImage.FromFile("~Resources/Sample-01/Images/bar-chart.png") }));
Try to create pdf output result
var result = doc.CreateResult(); if (!result.Success) { // Handle errors }
Save pdf file result
sync mode
var saveResult = result.Result.Action(new SaveToFile { OutputPath = "~/Output/Sample01/Sample-01" }); if (!saveResult.Success) { // Handle errors }
async mode
var saveResult = await result.Result.Action(new SaveToFileAsync { 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.
Load pdf file
var doc = new PdfInput { AutoUpdateChanges = true, Input = "~/Resources/Sample-02/file-sample.pdf" };
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> </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> </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> </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> </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> </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> </td>"); result.AppendLine(" </tr>"); result.AppendLine(" </tbody>"); result.AppendLine("</table>"); return result.ToString(); }
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()) }));
Try to create pdf output result
sync mode
var result = doc.CreateResult(); if (!result.Success) { // Handle errors }
async mode
var result = await doc.CreateResultAsync(); if (!result.Success) { // Handle errors }
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.
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" };
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.
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} } };
Try to merge into a pdf output result
sync mode
var mergeResult = files.TryMergeInputs(); if (!mergeResult.Success) { // Handle errors }
async mode
var mergeResult = await files.TryMergeInputsAsync(); if (!mergeResult.Success) { // Handle errors }
Save merged result to file
var saveResult = mergeResult.Result.Action(new SaveToFile { OutputPath = "~/Output/Sample03/Sample-03" }); if (!saveResult.Success) { // Handle errors }
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. 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 | 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. |
-
.NETCoreApp 3.1
- DotNetZip (>= 1.16.0)
- iTextSharp (>= 5.5.13.3)
- itextsharp.xmlworker (>= 5.5.13.3)
- itextsharp.xtra (>= 5.5.13.3)
- Microsoft.Win32.Registry (>= 5.0.0)
- Microsoft.Windows.Compatibility (>= 6.0.0)
- Newtonsoft.Json (>= 13.0.1)
- System.Drawing.Common (>= 6.0.0)
-
.NETFramework 4.6.1
- DotNetZip (>= 1.16.0)
- iTextSharp (>= 5.5.13.3)
- itextsharp.xmlworker (>= 5.5.13.3)
- itextsharp.xtra (>= 5.5.13.3)
- Microsoft.Win32.Registry (>= 5.0.0)
- Microsoft.Windows.Compatibility (>= 6.0.0)
- Newtonsoft.Json (>= 13.0.1)
- System.Drawing.Common (>= 6.0.0)
-
.NETStandard 2.0
- DotNetZip (>= 1.16.0)
- iTextSharp (>= 5.5.13.3)
- itextsharp.xmlworker (>= 5.5.13.3)
- itextsharp.xtra (>= 5.5.13.3)
- Microsoft.Win32.Registry (>= 5.0.0)
- Microsoft.Windows.Compatibility (>= 6.0.0)
- Newtonsoft.Json (>= 13.0.1)
- System.Drawing.Common (>= 6.0.0)
-
.NETStandard 2.1
- DotNetZip (>= 1.16.0)
- iTextSharp (>= 5.5.13.3)
- itextsharp.xmlworker (>= 5.5.13.3)
- itextsharp.xtra (>= 5.5.13.3)
- Microsoft.Win32.Registry (>= 5.0.0)
- Microsoft.Windows.Compatibility (>= 6.0.0)
- Newtonsoft.Json (>= 13.0.1)
- System.Drawing.Common (>= 6.0.0)
-
net6.0
- DotNetZip (>= 1.16.0)
- iTextSharp (>= 5.5.13.3)
- itextsharp.xmlworker (>= 5.5.13.3)
- itextsharp.xtra (>= 5.5.13.3)
- Microsoft.Win32.Registry (>= 5.0.0)
- Microsoft.Windows.Compatibility (>= 6.0.0)
- Newtonsoft.Json (>= 13.0.1)
- System.Drawing.Common (>= 6.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.