Watermark on a pdf gets set at the top left corner instead of center of the page

345 views Asked by At

Code I have used to watermark is shown below. For the first two pages, watermark is shown properly, but for the third page, the watermark gets shown on the top left corner. When I debugged, it was showing page size as 692*712 approximately.

class Program
{
    static string absoluteFile1Path = "filename1.pdf";
    static string absoluteFile2Path = "filename2.pdf";
    static string destination = "destinationfilename.pdf";

    static void Main(string[] args)
    {
        string[] filesPath = new string[] { absoluteFile1Path, absoluteFile2Path };

        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(destination));            

        PdfMerger merger = new PdfMerger(pdfDoc);

        PdfDocument srcDoc1 = new PdfDocument(new PdfReader(absoluteFile1Path));
        merger.Merge(srcDoc1, 1, srcDoc1.GetNumberOfPages());

        PdfDocument srcDoc2 = new PdfDocument(new PdfReader(absoluteFile2Path));
        merger.Merge(srcDoc2, 1, srcDoc2.GetNumberOfPages());

        pdfDoc.AddEventHandler(PdfDocumentEvent.END_PAGE, new WatermarkHandler());            

        srcDoc1.Close();
        srcDoc2.Close();

        pdfDoc.Close();

    }
}

public class WatermarkHandler : IEventHandler
{
    Color lime, blue;
    PdfFont helvetica;
    PdfFont font;

    public WatermarkHandler()
    {
        //helvetica = PdfFontFactory.CreateFont(FontConstants.HELVETICA);
        //lime = new DeviceCmyk(0.208f, 0, 0.584f, 0);
        //blue = new DeviceCmyk(0.445f, 0.0546f, 0, 0.0667f);
        font = PdfFontFactory.CreateFont(FontConstants.HELVETICA);
    }

    public void HandleEvent(Event @event)
    {
        PdfDocumentEvent docEvent = (PdfDocumentEvent)@event;

        PdfDocument pdf = docEvent.GetDocument();
        PdfPage page = docEvent.GetPage();
        int pageNumber = pdf.GetPageNumber(page);
        Rectangle pageSize = page.GetPageSize();

        PdfCanvas canvas = new PdfCanvas(page.NewContentStreamAfter(), page.GetResources(), pdf);
        PdfExtGState gs1 = new PdfExtGState().SetFillOpacity(0.9f).SetStrokeOpacity(0.3f);
        canvas.SetExtGState(gs1);
        new Canvas(canvas, pdf, new iText.Kernel.Geom.Rectangle(page.GetPageSize().GetWidth() / 2, page.GetPageSize().GetHeight() / 2))
                .SetFontColor(iText.Kernel.Colors.DeviceGray.LIGHT_GRAY)
                .SetFontSize(26)
                .SetFont(font)
                .ShowTextAligned(new Paragraph("FOR YOUR RECORDS ONLY: DO NOT SUBMIT"), 298, 375, pdf.GetPageNumber(page),
                        TextAlignment.CENTER, VerticalAlignment.MIDDLE, 45);

        canvas.Release();
    }
}
}
1

There are 1 answers

0
Samuel Huylebroeck On

Without having seen the pdf I can't be sure but this is most likely due to one of the pages being rotated (90 or 270 degrees if your 3rd page has its width and height flipped) and thus the absolute coordinates to show your watermark are a bit off on the rotated pages.

You can get the rotation of a page with PdfPage#getRotation() and write some additional logic for calculating the rotated x and y based on the retrieved value. There's some info on the coordinate system used in pdfs in chapter 2 of the iText7 jumpstart tutorial.