Microsoft Technologies based on the .NET software framework. Miscellaneous topics that do not fit into specific categories.
Hi @Kmcnet ,
Since the same code works in a fresh project, the issue is almost certainly that PdfWriter is resolving to a different type than the iText 9 one in this project most likely a second PDF library (such as the older iTextSharp) still being referenced, whose PdfWriter wins name resolution and produces both errors.
The fastest way to confirm is to fully qualify the iText 9 types so there's no ambiguity. If this compiles in the failing project, the cause was a type conflict:
string webRootPath = _webHostEnvironment.WebRootPath;
string src = webRootPath + @"\forms\test.pdf";
byte[] pdfBytes;
using (var stream = new MemoryStream())
using (var wri = new iText.Kernel.Pdf.PdfWriter(stream))
using (var pdf = new iText.Kernel.Pdf.PdfDocument(new iText.Kernel.Pdf.PdfReader(src), wri))
using (var doc = new iText.Layout.Document(pdf))
{
doc.ShowTextAligned(
new iText.Layout.Element.Paragraph("Hello World"),
375, 775, 1,
iText.Layout.Properties.TextAlignment.LEFT,
iText.Layout.Properties.VerticalAlignment.TOP, 0);
doc.Close();
pdfBytes = stream.ToArray();
}
return new FileContentResult(pdfBytes, "application/pdf");
I dropped the unused PdfWriter mywri = new PdfWriter(); line (iText 9's PdfWriter has no parameterless constructor) and the doc.Flush() after Close(), since closing already finalizes the document. The structure matches iText's own "Hello PDF!" example, so the logic itself is fine.
Disclaimer: This is a non-Microsoft website. The page appears to be providing accurate, safe information. Watch out for ads on the site that may advertise products frequently classifies as a PUP (Potentially Unwanted Products). Thoroughly research any product advertised on the site before you decide to download and install it.
If the fully-qualified version compiles, that confirms the conflict. To find which reference is bringing in the second PdfWriter, pressing F12 on PdfWriter in your original code will show the type and assembly it resolves to anything other than iText.Kernel.Pdf.PdfWriter from itext.kernel.dll is the culprit. Checking the <PackageReference> entries in that project's .csproj for a second PDF package alongside iText 9.6.0 would pin it down.
If it still doesn't compile, a small sample project that reproduces it would help me dig further.
If the information I provided were helpful, I would greatly appreciate it if you could follow the instructions here. This can help other community members facing similar scenarios.