0

I am using iTextSharp in my code to resize PDF documents and it is working very well, as shown below:

    public byte[] ResizePdf(byte[] data) {
        PdfReader reader = new PdfReader(data);
        Document document = new Document(PageSize.A4);

        MemoryStream stream = new MemoryStream();
        PdfWriter writer = PdfWriter.GetInstance(document, stream);

        document.Open();
        PdfContentByte cb = writer.DirectContent;

        for (int i = 1; i <= reader.NumberOfPages; i++) {
            document.NewPage();
            PdfImportedPage page = writer.GetImportedPage(reader, i);
            cb.AddTemplate(page, 0.8, 0, 0, 0.8, 25f, 60f);
        }

        document.Close();
        return stream.ToArray();
    }

However, when a PDF document is signed, when I try to resize it, I lose the signature on my new resized document.

I've tried to create a copy of the document (follow the code) and it works fine, but I can't resize it.

    public byte[] Test(byte[] data) {
        PdfReader reader = new PdfReader(data);
        Document document = new Document(PageSize.A4);

        MemoryStream stream = new MemoryStream();
        PdfCopy copy = new PdfCopy(document, stream);

        document.Open();
        PdfContentByte cb = copy.DirectContent;

        for (int i = 1; i <= reader.NumberOfPages; i++) {
            document.NewPage();
            PdfImportedPage page = copy.GetImportedPage(reader, i);
            cb.AddTemplate(page, 0.8, 0, 0, 0.8, 0, 0);
            copy.AddPage(page);
        }

        document.Close();
        return stream.ToArray();
    }

Does anyone have any tips or anything that can help me? Thanks in advance!

  • 2
    You are aware that changing page sizes of a signed PDF is disallowed? See [this answer](https://stackoverflow.com/a/16711745/1729265). Thus, even if you preserve the signature, Adobe Reader will display it as invalidated. – mkl Dec 08 '20 at 18:10
  • It doesn't matter to me that the signature is invalidated, as long as it visually continues to appear there – Felipe Salazar Dec 08 '20 at 18:34
  • In that case I'd propose you flatten the PDF in a first step (i.e. merge all annotations into the static page content; the signature visualization is a widget annotation) and then use your `PdfWriter` based approach. – mkl Dec 09 '20 at 08:20

0 Answers0