C++: Change the Margins of a PDF Document

Margins are the empty and unused spaces between the document's content and edges. The margins generally don't contain any text or image, and their primary purpose is to prevent text from colliding with document boundaries. Depending on your needs, you can change the margins to become wider or narrower. In this article, you will learn how to increase or decrease the page margins of a PDF document in C++ using Spire.PDF for C++.

Install Spire.PDF for C++

There are two ways to integrate Spire.PDF for C++ into your application. One way is to install it through NuGet, and the other way is to download the package from our website and copy the libraries into your program. Installation via NuGet is simpler and more recommended. You can find more details by visiting the following link.

Integrate Spire.PDF for C++ in a C++ Application

Increase the Margins of a PDF Document in C++

The way to enlarge the margins of a PDF document is to create a new PDF that has a larger page size, and then draw the source page on the large page at the appropriate location. The following are the steps to increase the margins of a PDF document using Spire.PDF for C++.

  • Load the original PDF document while initialing the PdfDocument object.
  • Create another PdfDocument object, which is used to create a new PDF document that has a larger page size.
  • Set the increasing values of the margins.
  • Calculate the page size of the new PDF document.
  • Loop through the pages in the original document, and create a template based on a specific page using PdfPageBase->CreateTemplate() method.
  • Add a page to the new PDF document using PdfDocument->GetPages()->Add() method.
  • Draw the template on the page at the coordinate (0, 0) using PdfTemplate->Draw() method.
  • Save the new PDF document to file using PdfDocument->SaveToFile() method.
  • C++
#include "Spire.Pdf.o.h";

using namespace Spire::Pdf;

int main() {

    //Load the original PDF document
    PdfDocument* originalPdf = new PdfDocument(L"C:\\Users\\Administrator\\Desktop\\sample.pdf");

    //Get the first page
    PdfPageBase* firstPage = originalPdf->GetPages()->GetItem(0);

    //Create a new PdfDocument object
    PdfDocument* newPdf = new PdfDocument();

    //Set increasing value of the margins
    PdfMargins* margins = newPdf->GetPageSettings()->GetMargins();
    margins->SetTop(40);
    margins->SetBottom(40);
    margins->SetLeft(40);
    margins->SetRight(40);

    //Calculate the new page size
    SizeF* sizeF = new SizeF(firstPage->GetSize()->GetWidth() + margins->GetLeft() + margins->GetRight(), firstPage->GetSize()->GetHeight() + margins->GetTop() + margins->GetBottom());

    //Loop through the pages in the original document
    for (size_t i = 0; i < originalPdf->GetPages()->GetCount(); i++) 
    {
        //Create a template based on a spcific page
        PdfTemplate* pdfTemplate = originalPdf->GetPages()->GetItem(i)->CreateTemplate();

        //Add a page to the new PDF
        PdfPageBase* page = newPdf->GetPages()->Add(sizeF);

        //Draw template on the page
        pdfTemplate->Draw(page, 0.0f, 0.0f);
    }

    //Save the new document
    newPdf->SaveToFile(L"Output/IncreaseMargins.pdf", FileFormat::PDF);
    newPdf->Close();
    delete originalPdf;
    delete newPdf;
}

C++: Change the Margins of a PDF Document

Decrease the Margins of a PDF Document in C++

The way to decrease the margins of a PDF is to create a new PDF that has a smaller page size, and then draw the source page on the small page at a specified coordinate. The following are the steps to decrease the margins of a PDF document using Spire.PDF for C++.

  • Load the original PDF document while initialing the PdfDocument object.
  • Create another PdfDocument object, which is used to create a new PDF document that has a smaller page size.
  • Set the decreasing values of the margins.
  • Calculate the page size of the new PDF document.
  • Loop through the pages in the original document, and create a template based on a specific page using PdfPageBase->CreateTemplate() method.
  • Add a page to the new PDF document using PdfDocument->GetPages()->Add() method.
  • Draw the template on the page at a specified coordinate using PdfTemplate->Draw() method.
  • Save the new PDF document to file using PdfDocument->SaveToFile() method.
  • C++
#include "Spire.Pdf.o.h";

using namespace Spire::Pdf;

int main() {

    //Load the original PDF document
    PdfDocument* originalPdf = new PdfDocument(L"C:\\Users\\Administrator\\Desktop\\sample.pdf");

    //Get the first page
    PdfPageBase* firstPage = originalPdf->GetPages()->GetItem(0);

    //Create a new PdfDocument object
    PdfDocument* newPdf = new PdfDocument();

    //Set decreasing value
    double left = -20;
    double right = -20;
    double top = -20;
    double bottom = -20;

    //Calculate the new page size
    SizeF* sizeF = new SizeF(firstPage->GetSize()->GetWidth() + left + right, firstPage->GetSize()->GetHeight() + top + bottom);

    //Loop through the pages in the original document
    for (size_t i = 0; i < originalPdf->GetPages()->GetCount(); i++)
    {
        //Create a template based on a specific page
        PdfTemplate* pdfTemplate = originalPdf->GetPages()->GetItem(i)->CreateTemplate();

        //Add a page to the new PDF
        PdfPageBase* page = newPdf->GetPages()->Add(sizeF, new PdfMargins(0));

        //Draw template on the page
        pdfTemplate->Draw(page, left, top);
    }

    //Save the new document
    newPdf->SaveToFile(L"Output/DecreaseMargins.pdf", FileFormat::PDF);
    newPdf->Close();
    delete originalPdf;
    delete newPdf;
}

C++: Change the Margins of a PDF Document

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.