Your ExampleSimple does not compile

Complete newbie here. Trying your “hello world” example - I did the NuGet install of Apose.PDF.Cpp and then try your ExampleSimple code – but, of course its full of undefines. So, I probably need to add a *.h file? or? You don’t mention anything in your getting started doc. Help! Thanks

@Tullhead

Cause

You are encountering compilation issues with the “Hello World” example in Aspose.PDF for C++. This is likely due to missing header files or an incorrect project setup.

Solution

To resolve this, ensure that you have included the necessary header files in your project. The example code you are trying to compile requires specific headers from the Aspose.PDF library.

Here’s a checklist to help you get started:

  1. Include Headers: Make sure to include the required headers at the beginning of your source file. For the “Hello World” example, you typically need:

    #include <aspose/pdf/document.h>
    #include <aspose/pdf/page.h>
    #include <aspose/pdf/text_fragment.h>
    
  2. Project Configuration: Ensure that your project is correctly configured to link against the Aspose.PDF library. Check your project settings to confirm that the library path is included.

  3. NuGet Package: Since you installed the package via NuGet, ensure that the package is properly restored. You can do this by right-clicking on your project in Visual Studio and selecting “Manage NuGet Packages” to verify the installation.

Code

Here’s a complete example that should compile without issues:

#include <aspose/pdf/document.h>
#include <aspose/pdf/page.h>
#include <aspose/pdf/text_fragment.h>

void ExampleSimple()
{
    String outputFileName;
    String _dataDir("C:\\Samples\\");
    String filename("HelloWorld_out.pdf");

    auto document = MakeObject<Document>();
    auto page = document->get_Pages()->Add();

    auto text = MakeObject<TextFragment>(u"Hello world!");
    auto paragraphs = page->get_Paragraphs();
    paragraphs->Add(text);

    outputFileName = _dataDir + filename;
    document->Save(outputFileName);
}

Next Steps

  1. Add the necessary header files as shown above.
  2. Ensure your project settings are correct and that the Aspose.PDF library is linked properly.
  3. Try compiling the code again.

If you continue to experience issues, please provide specific error messages, and I will assist you further.

Let me know if this resolves your issue!

Regarding those 3 include files – I don’t see any folder on my system called “pdf” that contains those three files. For example, I can hunt around and find a file “TextFragment.h” – but not “text_fragment.h” – so do I need to hunt around and guess at name changes like that, or…?

To guess at the include files, with the appropriate include path defined, these files are found:

#include <document.h>
#include <page.h>
#include <Text/TextFragment.h>

So, I’m going with that for now, unless you tell me something is wrong…

I made sure that the *.lib file is included in the Link.

But, I get:

1>C:\repos\M10\PDF.cpp(64,18): error C2065: ‘MakeObject’: undeclared identifier
1>C:\repos\M10\PDF.cpp(64,29): error C2065: ‘Document’: undeclared identifier

… and more stuff. So, if these are not in the *.lib, where are they supposed to come from?

Adding this:

using namespace System;
using namespace Aspose::Pdf;

helps a bit – now MakeObject seems to be defined, but still other stuff missing. For example:
error C2027: use of undefined type ‘Aspose::Pdf::PageCollection’

#include <PageCollection.h>

seems needed…

so your “Hello World” example needs more includes than you’re telling me… and still missing something… argh…

Here is my current attempt. Current problem is that it says TextFragment is undefined.

#include “Aspose.PDF.Cpp/document.h”
#include “Aspose.PDF.Cpp/page.h”
#include “Aspose.PDF.Cpp/PageCollection.h”
#include “Aspose.PDF.Cpp/Text/TextFragment.h”

using namespace System;
using namespace Aspose::Pdf;

void ExampleSimple()
{
CString outputFileName;
CString _dataDir(L"C:\Samples\“);
CString filename(L"HelloWorld_out.pdf”);

auto document = MakeObject<Document>();
auto page = document->get_Pages()->Add();

auto text = MakeObject<TextFragment>(u"Hello world!");
auto paragraphs = page->get_Paragraphs();
paragraphs->Add(text);

outputFileName = _dataDir + filename;
document->Save(outputFileName);

}

This is latest (had Strings wrong in last)

#include “Aspose.PDF.Cpp/document.h”
#include “Aspose.PDF.Cpp/page.h”
#include “Aspose.PDF.Cpp/PageCollection.h”
#include “Aspose.PDF.Cpp/Text/TextFragment.h”

using namespace System;
using namespace Aspose::Pdf;

void ExampleSimple()
{
String outputFileName;
String _dataDir(“C:\Samples\”);
String filename(“HelloWorld_out.pdf”);

auto document = MakeObject<Document>();
auto page = document->get_Pages()->Add();

auto text = MakeObject<TextFragment>(u"Hello world!");
auto paragraphs = page->get_Paragraphs();
paragraphs->Add(text);

outputFileName = _dataDir + filename;
document->Save(outputFileName);

}

@Tullhead

We are going through the details and will get back to you shortly.

@Tullhead The right full name is Aspose::Pdf::Text::TextFragment.
You should add

using namespace Aspose::Pdf::Text;

or add prefix Text

auto text = MakeObject<Text::TextFragment>(u"Hello world!");

The right code of your example is here

#include "Aspose.PDF.Cpp/Document.h"
#include "Aspose.PDF.Cpp/Page.h"
#include "Aspose.PDF.Cpp/PageCollection.h"
#include "Aspose.PDF.Cpp/Text/TextFragment.h"
#include "Aspose.PDF.Cpp/Generator/Paragraphs.h"

using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Text;

void ExampleSimple()
{
    String outputFileName;
    String _dataDir(u"C:\\Samples\\");
    String filename(u"HelloWorld_out.pdf");

    auto document = MakeObject<Document>();
    auto page = document->get_Pages()->Add();

    auto text = MakeObject<TextFragment>(u"Hello world!");
    auto paragraphs = page->get_Paragraphs();
    paragraphs->Add(text);

    outputFileName = _dataDir + filename;
    document->Save(outputFileName);
}

int main()
{
    ExampleSimple();
}