iText 9 Errors
Hello everyone and thanks for the help in advance. I am working on a .Net 10 MVC project that utilize iText 9 to generate Pdf files. The code looks like this:
string webRootPath = _webHostEnvironment.WebRootPath;
string src = webRootPath + @"\forms\test.pdf";
byte[] pdfBytes;
PdfWriter mywri = new PdfWriter();
using (var stream = new MemoryStream())
using (var wri = new PdfWriter(stream))
using (var pdf = new PdfDocument(new PdfReader(src), wri))
using (var doc = new Document(pdf))
{
doc.ShowTextAligned(new Paragraph("Hello World"),
375, 775, 1, TextAlignment.LEFT, VerticalAlignment.TOP, 0);
doc.Close();
doc.Flush();
pdfBytes = stream.ToArray();
}
return new FileContentResult(pdfBytes, "application/pdf");
```When I run this code in my existing project, I receive two errors, PdfWrite does not contain a constructor that takes 1 arguments and Cannot convert PdfWriter to iTextKernel.Pdf.DocumentProperties. However, when I take this exact code and place it in a new MVC project, it works fine. I have verified the same iText 9.6.0 nuget packages are installed and the appropriate using statements are identical on the controller. I have not idea what the problem is. Any help will be appreciated.
2 answers
-
Tom Tran (WICLOUD CORPORATION) 5,050 Reputation points • Microsoft External Staff • Moderator
Hi @Kmcnet ,
Since the same code works in a fresh project, the issue is almost certainly that
PdfWriteris resolving to a different type than the iText 9 one in this project most likely a second PDF library (such as the olderiTextSharp) still being referenced, whosePdfWriterwins 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'sPdfWriterhas no parameterless constructor) and thedoc.Flush()afterClose(), 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 onPdfWriterin your original code will show the type and assembly it resolves to anything other thaniText.Kernel.Pdf.PdfWriterfromitext.kernel.dllis the culprit. Checking the<PackageReference>entries in that project's.csprojfor 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.
-
Tom Tran (WICLOUD CORPORATION) 5,050 Reputation points • Microsoft External Staff • Moderator
Hi @Kmcnet ,
I'm just checking in to see if you have any questions, please feel free to comment below. I'll be happy to assist.
Sign in to comment -
-
AI answer
The behavior difference between the two projects indicates that the existing project is resolving a different
PdfWritertype or a different iText version/assembly than the new test MVC project, even though the same NuGet package version appears installed.The two specific errors:
- “
PdfWriterdoes not contain a constructor that takes 1 arguments” - “Cannot convert
PdfWritertoiTextKernel.Pdf.DocumentProperties”
mean that, at compile time, the
PdfWritersymbol in the existing project does not match the iText 9 API that your code expects. This typically happens due to:- Conflicting references (another assembly defining
PdfWriter, or an older iText version) being picked up first. - Mismatched target frameworks or binding redirects causing a different iText assembly to load/compile against.
To fix this in the existing project:
- Check the resolved type for
PdfWriter
In the problematic project, use the IDE to inspect the symbol:- Hover over
PdfWriterand confirm the fully qualified name (namespace and assembly). It must match the iText 9 type you expect. - If it shows a different namespace/assembly than in the new MVC project, that’s the conflict.
- Hover over
- Remove conflicting references
- In the existing project, open the project file and remove any old or duplicate iText references (e.g., older iText 7/8, or other libraries that define
PdfWriter). - Clean and rebuild after removing them.
- In the existing project, open the project file and remove any old or duplicate iText references (e.g., older iText 7/8, or other libraries that define
- Force the correct namespace
- Temporarily replace
PdfWriterwith its fully qualified name from iText 9 (for example,iText.Kernel.Pdf.PdfWriterif that is the correct namespace in your package). - If the error changes or disappears, it confirms a name collision.
- Temporarily replace
- Compare project files
- Compare the
.csprojof the working new MVC project with the existing project:- Ensure the same
TargetFrameworkand the same iText package reference (same version, same Include). - Remove any extra package references related to older PDF libraries.
- Ensure the same
- Compare the
- Clean build artifacts
- Delete
binandobjfolders of the existing project. - Rebuild the solution to ensure no stale assemblies are being used.
- Delete
Once the existing project resolves the same
PdfWritertype as the new MVC project, the constructor callnew PdfWriter(stream)and thePdfDocumentconstructor overload will compile and run as expected.
References:
AI-generated content may be incorrect. Read our transparency notes for more information. - “
