Shrink PDF page and insert into existing full size PDF page


#1

Hi

I’m trying to take a single page PDF, reduce it in size and then insert that page into another PDF but that new PDF page should be full size with the shrunk content on it. Idea is that I may want to add my own pagination footer and want it clear of anything which might already exist at the bottom of the page.

I can shrink the page but the page is still shrunk on the new document so it’s just adding the page to the document rather than adding the page to the page, if that makes sense, see code below and attached pdf page.

    static void Main(string[] args)
    {
        Aspose.Pdf.License pdfLicense = new Aspose.Pdf.License();
        pdfLicense.SetLicense("Aspose.Total.lic");

        string file = @"C:\temp\lorem.pdf";
        var pageDoc = new Aspose.Pdf.Document(file);
        var pdfPageHeight = pageDoc.PageInfo.Height;
        var pdfPageWidth = pageDoc.PageInfo.Width;

        var newPdf = new Document();
        newPdf.PageInfo.Margin.Bottom = 50;
        newPdf.PageInfo.Margin.Top = 0;
        newPdf.PageInfo.Margin.Left = 0;
        newPdf.PageInfo.Margin.Right = 0;
        newPdf.PageInfo.Height = Aspose.Pdf.PageSize.A4.Height;
        newPdf.PageInfo.Width = Aspose.Pdf.PageSize.A4.Width;

        Aspose.Pdf.Facades.PdfFileEditor pfe = new Aspose.Pdf.Facades.PdfFileEditor();

        // Squash the page a bit so we can fit our own footer on
        pfe.ResizeContents(pageDoc, Aspose.Pdf.Facades.PdfFileEditor.ContentsResizeParameters.PageResize(pdfPageWidth, pdfPageHeight-50));

        newPdf.Pages.Add(pageDoc.Pages);

        var pageNumberStamp = new Aspose.Pdf.PageNumberStamp();
        pageNumberStamp.Background = false;
        pageNumberStamp.BottomMargin = 10;
        pageNumberStamp.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;
        pageNumberStamp.TextState.Font = Aspose.Pdf.Text.FontRepository.FindFont("Arial");
        pageNumberStamp.TextState.FontSize = 8;
        pageNumberStamp.Format = $"Page 1 of 1";
        newPdf.Pages[1].AddStamp(pageNumberStamp);

        newPdf.Save(@"C:\temp\newPDF.pdf");
    }

As you’ll see the pagination stamp isn’t in a clear area at the bottom which is what I wanted.

Cheers

Si
lorem.pdf (53.2 KB)


#2

@simon.fairey

Thank you for contacting support.

You can resize page contents with ResizeContents method as in the code snippet below:

        Document doc = new Document(dataDir + "lorem.pdf");
        var fileEditor = new Aspose.Pdf.Facades.PdfFileEditor();
        var parameters = new Aspose.Pdf.Facades.PdfFileEditor.ContentsResizeParameters(Aspose.Pdf.Facades.PdfFileEditor.ContentsResizeValue.Units(0), null, Aspose.Pdf.Facades.PdfFileEditor.ContentsResizeValue.Units(0), Aspose.Pdf.Facades.PdfFileEditor.ContentsResizeValue.Units(0), null, Aspose.Pdf.Facades.PdfFileEditor.ContentsResizeValue.Units(20));
        fileEditor.ResizeContents(doc, parameters);
        var pageNumberStamp = new Aspose.Pdf.PageNumberStamp();
        pageNumberStamp.Background = false;
        pageNumberStamp.BottomMargin = 10;
        pageNumberStamp.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;
        pageNumberStamp.TextState.Font = Aspose.Pdf.Text.FontRepository.FindFont("Arial");
        pageNumberStamp.TextState.FontSize = 8;
        pageNumberStamp.Format = "Page 1 of 1";
        doc.Pages[1].AddStamp(pageNumberStamp);            
        doc.Save(dataDir + "Resize_18.7.pdf");

We have also attached generated PDF file for your kind reference Resize_18.7.pdf. We hope this will be helpful. Please feel free to contact us if you need any further assistance.


split this topic #3

5 posts were split to a new topic: Shrink JPG and TIFF files