-4

From a pdf, I want to make a new pdf that I 'll add a watermark and then make each page an image page.

Is this possible with itext?

XristosK
  • 266
  • 4
  • 16
  • 1
    i googled this question and found this: https://itextpdf.com/en/resources/faq/technical-support/itext-7/how-watermark-pdfs-using-text-or-images – jazb Feb 01 '19 at 07:12
  • 1
    and i found this [link](https://stackoverflow.com/questions/34649115/watermark-text-or-image-in-existing-pdf) – magicalKhachapuri Feb 01 '19 at 07:19
  • 1
    I guess you can do this to add water mark using itext for pdf to image conversion you would need other library or way to do so for that you can get help from here https://social.msdn.microsoft.com/Forums/vstudio/en-US/447b4310-2d9f-4307-b648-775f6d091ab1/how-to-convert-pdf-to-tiff-through-c?forum=csharpgeneral – Usama Kiyani Feb 01 '19 at 07:29
  • I need to convert pdf to image so noone can be able to change it. The watermark will be the date th user requested this particular file. Pdf to image is the difficult part – XristosK Feb 01 '19 at 12:05

1 Answers1

1

I don't know how to convert it to images, but for the watermark, as said by Usama Kiyani in comments, you should consider using itextsharp which can be installed through nugget packages manager. I already used it to add a water mark to an existing pdf file.

Here's the code I used, it add a diagonal red watermark (which text is argument watermarkText) in the center of each page of an existing pdf file (sourceFile), then save this modified version at the given location (outputFile) :

public static void AddWatermarkTextC(string sourceFile, string outputFile, string watermarkText)
    {
        BaseFont tWatermarkFont = null;
        float tWatermarkFontSize = 48F;
        iTextSharp.text.BaseColor tWatermarkFontColor = null;
        float tWatermarkFontOpacity = 0.3F;
        float tWatermarkRotation = 45.0F;

        tWatermarkFont = iTextSharp.text.pdf.BaseFont.CreateFont(iTextSharp.text.pdf.BaseFont.HELVETICA, iTextSharp.text.pdf.BaseFont.CP1252, iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED);
        tWatermarkFontColor = iTextSharp.text.BaseColor.RED;
        AddWatermarkTextC(sourceFile, outputFile, watermarkText, tWatermarkFont, tWatermarkFontSize, tWatermarkFontColor, tWatermarkFontOpacity, tWatermarkRotation);
    }

public static void AddWatermarkTextC(string sourceFile, string outputFile, string watermarkText, iTextSharp.text.pdf.BaseFont watermarkFont, float watermarkFontSize, iTextSharp.text.BaseColor watermarkFontColor, float watermarkFontOpacity, float watermarkRotation)
    {

        iTextSharp.text.pdf.PdfReader reader = null;
        iTextSharp.text.pdf.PdfStamper stamper = null;
        iTextSharp.text.pdf.PdfGState gstate = null;
        iTextSharp.text.pdf.PdfContentByte underContent = null;
        iTextSharp.text.Rectangle rect = null;
        float currentY = 0.0F;
        float offset = 0.0F;
        int pageCount = 0;

        try
        {
            reader = new iTextSharp.text.pdf.PdfReader(sourceFile);
            rect = reader.GetPageSizeWithRotation(1);
            FileStream stream = new System.IO.FileStream(outputFile, System.IO.FileMode.Create);
            stamper = new iTextSharp.text.pdf.PdfStamper(reader, stream);

            if (watermarkFont == null)
            {
                watermarkFont = iTextSharp.text.pdf.BaseFont.CreateFont(iTextSharp.text.pdf.BaseFont.HELVETICA, iTextSharp.text.pdf.BaseFont.CP1252, iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED);
            }

            if (watermarkFontColor == null)
            {
                watermarkFontColor = iTextSharp.text.BaseColor.RED;
            }

            gstate = new iTextSharp.text.pdf.PdfGState();
            gstate.FillOpacity = watermarkFontOpacity;
            gstate.StrokeOpacity = watermarkFontOpacity;
            pageCount = reader.NumberOfPages;

            for (int i = 1; i <= pageCount; i++)
            {
                underContent = stamper.GetOverContent(i);
                underContent.SaveState();
                underContent.SetGState(gstate);
                underContent.SetColorFill(watermarkFontColor);
                underContent.BeginText();
                underContent.SetFontAndSize(watermarkFont, watermarkFontSize);
                underContent.SetTextMatrix(30, 30);

                currentY = (rect.Height / 2);

                underContent.ShowTextAligned(iTextSharp.text.Element.ALIGN_CENTER, watermarkText, rect.Width / 2, currentY - offset, watermarkRotation);

                underContent.EndText();
                underContent.RestoreState();
            }

            stamper.Close();
            reader.Close();
            stream.Close();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }  

I guess it's not really hard to change it to fit your need, but if there's is anything you need me to explain just ask for it.

Zoma
  • 321
  • 7
  • 20