Free Support Forum - aspose.com

How can I adjust a page margin using Aspose.PDF.C++

Hello.
I want to adjust a page margin. so, I wrote a code below.

auto doc = MakeObject();
auto PageInfo = doc->get_PageInfo();
auto mg = MakeObject <Aspose::Pdf::MarginInfo>(5, 5, 5, 5);
PageInfo->set_Margin(mg);
doc->set_PageInfo(PageInfo);

But, generate a compile error
\aspose.pdf.cpp.20.2\codeporting.native.cs2cpp_vc14_20.2\include\system\smart_ptr.h(1106): error C2027: 정의되지 않은 형식 'Aspose::Pdf::MarginInfo’을(를) 사용했습니다.

I want your advice or a sample code. Thanks.

@kumdoin

We are checking it and will get back to you shortly.

@kumdoin

Would you kindly initialize Document object as following and if issue still persists, please let us know.

auto doc = MakeObject<Aspose::Pdf::Document>();

Yes, I make a code as you point out.
auto doc = MakeObject<Aspose::Pdf::Document>();

But,applied auto translation in this thread.
auto doc = MakeObject<Aspose::Pdf::Document>(); -> auto doc = MakeObject();

Rewrite my code below.

auto doc = MakeObject<Aspose::Pdf::Document>();
auto PageInfo = doc->get_PageInfo();
auto mg = MakeObject <Aspose::Pdf::MarginInfo>(5, 5, 5, 5);
PageInfo->set_Margin(mg);
doc->set_PageInfo(PageInfo);

I got same result.

@kumdoin

Possible variable name PageInfo may conflict with class name PageInfo. Pleaes try to rename variable to pageInfo or something like. If there is error still, please provide us with full example and compiler log file.

So, I request a sample code about custom margin’s page using Aspose.PDF for C++.
Thanks.

@kumdoin

We are checking it and will get back to you shortly.

@kumdoin

Please try to set margins on page level instead of setting them for whole document as following:

auto doc = MakeObject<Document>();
auto page = doc->get_Pages()->Add();
auto pageInfo = page->get_PageInfo();
auto mg = MakeObject<Aspose::Pdf::MarginInfo>(5, 5, 5, 5);
pageInfo->set_Margin(mg);
page->set_PageInfo(pageInfo);

Thanks you.

auto doc = MakeObjec<Document>();
auto page = doc->get_Pages()->Add();
auto pageInfo = doc->get_PageInfo();
auto mg = MakeObject<Aspose::Pdf::MarginInfo>(5, 5, 5, 5);
pageInfo->set_Margin(mg);
doc->set_PageInfo(pageInfo);

Compile your code. But continue errors.

1>c:\users\user\desktop\aspose.pdf.cpp.20.2\codeporting.native.cs2cpp_vc14_20.2\include\system\smart_ptr.h(1106): error C2027: 정의되지 않은 형식 'Aspose::Pdf::MarginInfo’을(를) 사용했습니다.
1>c:\users\user\desktop\aspose.pdf.cpp.20.2\include\aspose.pdf.cpp\table\cell.h(22): note: ‘Aspose::Pdf::MarginInfo’ 선언을 참조하십시오.
1>c:\users\user\desktop\aspose.pdf.cpp.20.2\example\examples1.cpp(945): note: 컴파일 중인 함수 템플릿 인스턴스화 'System::SmartPtr<Aspose::Pdf::MarginInfo> System::MakeObject<Aspose::Pdf::MarginInfo,int,int,int,int>(int &&,int &&,int &&,int &&)'에 대한 참조를 확인하십시오.
1>c:\users\user\desktop\aspose.pdf.cpp.20.2\codeporting.native.cs2cpp_vc14_20.2\include\system\smart_ptr.h(1106): error C2789: ‘object’: const 한정 형식의 개체를 초기화해야 합니다.
1>c:\users\user\desktop\aspose.pdf.cpp.20.2\codeporting.native.cs2cpp_vc14_20.2\include\system\smart_ptr.h(1106): note: ‘object’ 선언을 참조하십시오.

Is it necessary for any setting?

@kumdoin

Please try including below classes in your program:

#include "Aspose.PDF.Cpp\Generator\PageInfo.h"
#include "Aspose.PDF.Cpp\Generator\MarginInfo.h" 

Also, instead of doc->set_PageInfo(pageInfo);, please try using page->set_PageInfo(pageInfo);. Please let us know in case you face any issue.

Thanks!!. It worked well.

1 Like