Search code examples
c#itextitext7accessible

While adding watermark text using iTextSharp, iText 7 warnings in PAC tool


Question 1: iTextSharp I added watermark text to pdf using iTextsharp.Sample code as below. when I check the PDF accessibility through PAC tool, I got warnings saying "Possibly Inappropriate use of Span structure element".

public void override OnEndPage(PdfWriter writer, Document document) {
            float fontSize = 80;
            float xPosition = 300;
            float yPosition = 400;
            float angle = 45;
            PdfContentByte under = writer.DirectContentUnder;
            BaseFont baseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
        Font f = new Font(baseFont, 80, Font.NORMAL, BaseColor.LIGHT_GRAY);
        Phrase phrse = new Phrase("TEXT",f);
            ColumnText.ShowTextAligned(under , Element.ALIGN_LEFT, phrase, 75, 680,0); 

        }

Can anyone please help me to resolve those warnings?

Question 2: iText 7 I added watermarkText using iText 7. Sample code as below.

PdfGState gs1 = new PdfGState();
gs1.setFillOpacity(0.5f);
PdfCanvas canvas = new PdfCanvas(docEvent.getPage());
canvas.beginMarkedContentSequence(PdfName.ARTIFACT);
canvas.setExtGState(gs1);
new Canvas(canvas, pdfDoc, page.getPageSize())
                    .setFontColor(Color.LIGHT_GRAY)
                    .setFontSize(60)
                    .setFont(font)
                    .showTextAligned(new Paragraph("WATERMARK"), 298, 421, pdfDoc.getPageNumber(page),
                            TextAlignment.CENTER, VerticalAlignment.MIDDLE, 45);
canvas.endMarkedContentSequence();

When I check the PDF accessibility through PAC tool, I got error "This pdf is corrupt. Unusable". How I can solve this error?


Solution

  • You are adding content that isn't tagged. That's not allowed. Please read the FAQ on the official web site: How to add a page number in the header of a PDF/A Level A file? It describes the same problem, and it explains how to add content as an artifact. Artifacts are pieces of content such as page numbers, headers, footers, watermarks,... that aren't part of the real content.

    You are adding a watermark like this:

    ColumnText.ShowTextAligned(under , Element.ALIGN_LEFT, phrase, 75, 680,0);
    

    Marking this content as an artifact, is done like this:

    under.beginMarkedContentSequence(PdfName.ARTIFACT);
    ColumnText.ShowTextAligned(under , Element.ALIGN_LEFT, phrase, 75, 680,0);
    under.endMarkedContentSequence();
    

    Note that you're still using iText 5. The most future-proof version is iText 7. iText 7 has better Tagged PDF support. The section about Tagged PDF was completely rewritten in ISO 32000-2 (aka the PDF 2.0 standard). There will be no PDF 2.0 support in iText 5. The first version that supports PDF 2.0 is iText 7.1.

    If you invest in iText 5 development now, you might have to rewrite all your code the day you have to comply with the rules for Tagged PDF as defined in PDF 2.0. If I were you, I'd upgrade now.