I need to open an existing pdf file.

Talk here about issues with one of the components hosted at wxCode, or suggest features for it.
dkaip
Experienced Solver
Experienced Solver
Posts: 72
Joined: Wed Jan 20, 2010 1:15 pm

I need to open an existing pdf file.

Postby dkaip » Fri Nov 03, 2017 3:26 pm

I need to open an existing pdf file with wxpdfdoc and insert pages in my own pdf file.
With PoDoFo i made it, but i dont know how with wxpdfdoc. An anvice of code how to?
Some code of PoDoFo, that i am learning now. But i think that i can made most easy with wxpdfdoc.
Thanks
Jim

Code: Select all

#include <iostream>
#include "cstring"
#include <podofo/podofo.h>
using namespace std;
int main()
{
    PoDoFo::PdfMemDocument existingDocument1;
    PoDoFo::PdfMemDocument existingDocument2;
    PoDoFo::PdfMemDocument existingDocument3;
    existingDocument1.Load("a.pdf");
    existingDocument2.Load("b.pdf");
    existingDocument3.InsertPages(existingDocument1,0,1);
    existingDocument3.Write("c.pdf");

    for (int pn = 0; pn < existingDocument1.GetPageCount(); ++pn)
    {
        PoDoFo::PdfPage* page = existingDocument1.GetPage(pn);
        PoDoFo::PdfContentsTokenizer tok(page);
        const char* token = nullptr;
        PoDoFo::PdfVariant var;
        PoDoFo::EPdfContentsType type;
        while (tok.ReadNext(type, token, var))
        {
            switch (type)
            {
            case PoDoFo::ePdfContentsType_Keyword:
                // process token: it contains the current command
                //   pop from var stack as necessary
                break;
            case PoDoFo::ePdfContentsType_Variant:
                // process var: push it onto a stack
                break;
            default:
                // should not happen!
                break;
            }
        }

    }
    return 0;
}

utelle
Moderator
Moderator
Posts: 767
Joined: Tue Jul 05, 2005 10:00 pm
Location: Cologne, Germany
Contact:

Re: I need to open an existing pdf file.

Postby utelle » Fri Nov 03, 2017 4:27 pm

dkaip wrote:I need to open an existing pdf file with wxpdfdoc and insert pages in my own pdf file.
With PoDoFo i made it, but i dont know how with wxpdfdoc. An anvice of code how to?
Some code of PoDoFo, that i am learning now. But i think that i can made most easy with wxpdfdoc.

It depends on your needs. If you just want to extract certain pages from existing PDF files and add them to a new PDF document, then wxPdfDocument offers a simple mechanism to do so.

Take a look at sample templates.cpp coming with wxPdfDocument. The function templates2 demonstrates how to reference an existing PDF file, to extract a page, and to add it to the resulting PDF document.

Currently, wxPdfDocument supports to reference one existing PDF file at a time. That is, if you need to process more than one existing PDF file, then you have to do it one after the other.

Regards,

Ulrich

dkaip
Experienced Solver
Experienced Solver
Posts: 72
Joined: Wed Jan 20, 2010 1:15 pm

Re: I need to open an existing pdf file.

Postby dkaip » Sat Nov 04, 2017 9:41 am

251030189.pdf.txt
(189.43 KiB) Downloaded 4 times
Thanks for reply. I am trying to learn library and i am new to all those new things.
Using code bellow i can't insert first page from 251030189.pdf to templates2.pdf.
I have error ..

ASSERT INFO:
../src/common/unichar.cpp(65): assert ""Assert failure"" failed in ToHi8bit(): character cannot be converted to single byte

BACKTRACE:
[1] wxUniChar::ToHi8bit(unsigned int) /home/a/wxWidgets/gtk-build/../src/common/unichar.cpp:65
[2] wxUniChar::To8bit(unsigned int) /home/a/wxWidgets/gtk-build/../include/wx/unichar.h:200
[3] wxUniChar::operator char() const /home/a/wxWidgets/gtk-build/../include/wx/unichar.h:121

All i need is just want to extract certain pages from existing PDF files and add them to a new PDF document.
Jim

Code: Select all

 int rc = 0;
    if (wxFileName::IsFileReadable(wxS("251030189.pdf")))
    {
        double x, y, w, h;
        wxPdfDocument pdf;
        if (true)
        {
            pdf.SetCreationDate(wxDateTime(1, wxDateTime::Jan, 2017));
            pdf.SetCompression(false);
        }
        pdf.AddPage();
        pdf.SetTextColour(wxPdfColour(wxString(wxS("black"))));
        int pages = pdf.SetSourceFile(wxS("251030189.pdf"));

        // Get the document information from the imported PDF file
        wxPdfInfo info;
        pdf.GetSourceInfo(info);

        int tpl = pdf.ImportPage(1);

        // Add some extra white space around the template
        pdf.GetTemplateBBox(tpl, x, y, w, h);
        pdf.SetTemplateBBox(tpl, x-10, y-10, w+20, h+20);
        pdf.UseTemplate(tpl, 20, 20, 160);

        // Draw a rectangle around the template
        w = 160;
        h = 0;
        pdf.GetTemplateSize(tpl, w, h);
        pdf.Rect(20, 20, w, h);

        pdf.SetXY(30,30+h);
        pdf.SetFont(wxS("Helvetica"),wxS(""), 10);
        pdf.SetLeftMargin(30);
        pdf.Cell(0, 5, wxString(wxS("Title: "))+info.GetTitle());
        pdf.Ln(5);
        pdf.Cell(0, 5, wxString(wxS("Creator: "))+info.GetCreator());
        pdf.Ln(5);
        pdf.Cell(0, 5, wxString(wxS("Producer: "))+info.GetProducer());

        pdf.SaveAsFile(wxS("templates2.pdf"));

    }


utelle
Moderator
Moderator
Posts: 767
Joined: Tue Jul 05, 2005 10:00 pm
Location: Cologne, Germany
Contact:

Re: I need to open an existing pdf file.

Postby utelle » Sat Nov 04, 2017 6:18 pm

dkaip wrote:Using code bellow i can't insert first page from 251030189.pdf to templates2.pdf.
I have error ..

ASSERT INFO:
../src/common/unichar.cpp(65): assert ""Assert failure"" failed in ToHi8bit(): character cannot be converted to single byte

BACKTRACE:
[1] wxUniChar::ToHi8bit(unsigned int) /home/a/wxWidgets/gtk-build/../src/common/unichar.cpp:65
[2] wxUniChar::To8bit(unsigned int) /home/a/wxWidgets/gtk-build/../include/wx/unichar.h:200
[3] wxUniChar::operator char() const /home/a/wxWidgets/gtk-build/../include/wx/unichar.h:121

All i need is just want to extract certain pages from existing PDF files and add them to a new PDF document.

I could reproduce the problem. It is related to the call of method GetSourceInfo. The handling of Unicode strings changed from wxWidgets version 2.8.x to version 3.x. If the PDF source information like title or author contain non-ASCII characters (as is the case for your sample PDF file), this causes the conversion assertion you observed.

I made a quick fix to the PDF parser (file pdfparser.cpp) to resolve the issue. Please clone or download the latest version of the wxPdfDocument github repository again.

With this fix accessing the source information and importing pages from your sample PDF file worked for me.

Regards,

Ulrich

dkaip
Experienced Solver
Experienced Solver
Posts: 72
Joined: Wed Jan 20, 2010 1:15 pm

Re: I need to open an existing pdf file.

Postby dkaip » Sat Nov 04, 2017 7:00 pm

Now works fine. Thank you very mush.
Jim


Return to “wxCode”

Who is online

Users browsing this forum: No registered users and 1 guest