I am in the process of evaluating Spire.PDF for my customer. As an experiment I was just using the watermark functionality.
Structure of the solution
- Class library (.NET Standard 20 )
EXE (.NET 461)
Class library
The class library project has the following class and Nuget package reference to "Spire.PDF 5.8.7"
- Code: Select all
public class SpireWrapper
{
public void StampFile(string fileIn, string fileOut, string waterMark)
{
using (var doc = new Spire.Pdf.PdfDocument(fileIn))
{
foreach (PdfPageBase page in doc.Pages)
{
PdfTilingBrush brush
= new PdfTilingBrush(new SizeF(page.Canvas.ClientSize.Width / 2, page.Canvas.ClientSize.Height / 3));
brush.Graphics.SetTransparency(0.3f);
brush.Graphics.Save();
brush.Graphics.TranslateTransform(brush.Size.Width / 2, brush.Size.Height / 2);
brush.Graphics.RotateTransform(-45);
brush.Graphics.DrawString(waterMark,
new PdfFont(PdfFontFamily.Helvetica, 24), PdfBrushes.Violet, 0, 0,
new PdfStringFormat(PdfTextAlignment.Center));
brush.Graphics.Restore();
brush.Graphics.SetTransparency(1);
page.Canvas.DrawRectangle(brush, new RectangleF(new PointF(0, 0), page.Canvas.ClientSize));
}
doc.SaveToFile(fileOut);
}
}
public void ReadAllText1(string fileIn)
{
using (var doc = new Spire.Pdf.PdfDocument(fileIn))
{
foreach (PdfPageBase page in doc.Pages)
{
var allText=page.FindAllText();
}
}
}
}
Console EXE project
This project has the following class
- Code: Select all
class Program
{
static void Main(string[] args)
{
try
{
var wrapper = new ClassLibrary1.SpireWrapper();
string file = "Brainjuicer100308flash.pdf";
string fileIn = file;
string fileOut = "result.pdf";
wrapper.StampFile(fileIn, fileOut, "Watermark message");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
Results
During execution the EXE throws the error
- Code: Select all
System.IO.FileLoadException: Could not load file or assembly 'Spire.Pdf, Version=5.8.7.2320, Culture=neutral, PublicKeyToken=663f351905198cb3' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
File name: 'Spire.Pdf, Version=5.8.7.2320, Culture=neutral, PublicKeyToken=663f351905198cb3'
at ClassLibrary1.SpireWrapper.StampFile(String fileIn, String fileOut, String waterMark)
at ConsoleApp1.Program.Main(String[] args) in C:\Users\saurabhd\MyTrials\Apex-Stuff\Apex-issues\APEX-1766-Watermark\WaterMark\ConsoleApp1\Program.cs:line 20
Workaround
If the class library is changed to .NET Framework 461 then there are no problems. However this becomes problematic because we would like to target all our class libraries for .NET Standard 20. Because of this we now have to maintain two sets of class libraries with shared code. It works, but not desirable.
As per Microsoft's documentation (I was not allowed to post the MSDN url)
.NET Standard 20 and .NET Framework 461 should be compatible.
Any suggestions?