Page 1 of 1

wxPdfDocument joining 2 PDF files. Is it possible?

Posted: Mon Jun 17, 2019 12:06 pm
by cutecode
hello

Can I import one PDF file at the end of the other using wxPdfDocument?


Thnk you

Re: wxPdfDocument joining 2 PDF files. Is it possible?

Posted: Mon Jun 17, 2019 12:55 pm
by Kvaz1r
No. From conversation in this issue:
wxPdfDocument is a component for generating PDF documents. It is not a component for parsing, displaying or printing PDF documents.

Re: wxPdfDocument joining 2 PDF files. Is it possible?

Posted: Mon Jun 17, 2019 3:56 pm
by utelle
Kvaz1r wrote: Mon Jun 17, 2019 12:55 pmNo.
This is not completely true.
Kvaz1r wrote: Mon Jun 17, 2019 12:55 pmFrom conversation in this issue:
wxPdfDocument is a component for generating PDF documents. It is not a component for parsing, displaying or printing PDF documents.
The mentioned issue is related to printing and previewing a PDF file. This is indeed not possible to do with wxPdfDocument.

However, wxPdfDocument allows to extract complete pages from existing PDF files and to insert them into new PDF documents. One of the samples coming with wxPdfDocument shows how to do it (see external templates).

Regards,

Ulrich

Re: wxPdfDocument joining 2 PDF files. Is it possible?

Posted: Mon Jun 17, 2019 4:10 pm
by Kvaz1r
utelle wrote: Mon Jun 17, 2019 3:56 pm However, wxPdfDocument allows to extract complete pages from existing PDF files and to insert them into new PDF documents. One of the samples coming with wxPdfDocument shows how to do it (see external templates).
Oh, great, I didn't know about that.
@cutecode ,sorry for inaccuracy, I should've taken a look on samples more carefully ;-)

Re: wxPdfDocument joining 2 PDF files. Is it possible?

Posted: Mon Jun 17, 2019 4:12 pm
by cutecode
hello, I looked at templates.cpp file before posting.
There are only 2 example functions templates1() and templates2()
and both functions only open PDF file, add new page and then draw some image/text on new page.

Could you, please, give some hint?

Re: wxPdfDocument joining 2 PDF files. Is it possible?

Posted: Mon Jun 17, 2019 8:22 pm
by utelle
cutecode wrote: Mon Jun 17, 2019 4:12 pm hello, I looked at templates.cpp file before posting.
There are only 2 example functions templates1() and templates2()
and both functions only open PDF file, add new page and then draw some image/text on new page.

Could you, please, give some hint?
Function templates2() extracts a page from an existing PDF file and places it on a page of the new PDF document.

Method SetSourceFile selects the existing PDF file from which pages will be extracted later on. You can extract pages from more than one PDF file, but only one such PDF file is the current one. Method SetSourceFile returns the number of pages the associated PDF file has.

Method ImportPage extracts a page from the current PDF source and returns the number of the template created from the imported page.

Method GetTemplateBBox allows to determine the bounding box the imported page. This information can then be used to place the template on a new page using method UseTemplate.

Whether these methods are suited to fulfill the task you have in mind, depends on the actual demands of your application. The template mechanism of wxPdfDocument works well for small to medium numbers of imported pages, but it is certainly not the best option for mass import of hundredths of pages.

If you only want to take several existing PDF files and use certain pages from each of them, combining them in a new order, there exist dedicated tools which are better suited for this kind of job.

Re: wxPdfDocument joining 2 PDF files. Is it possible?

Posted: Mon Jun 17, 2019 9:33 pm
by cutecode
thank you, I skoped to join 2 PDF files

Code: Select all

bool join_pdfs(LPCTSTR lpsz1, LPCTSTR lpszOut)
{
	int tpl;

	wxPdfDocument pdf1;

	pdf1.SetCreationDate(wxDateTime(1, wxDateTime::Jan, 2017));
	pdf1.SetCompression(true);

	int pages1 = pdf1.SetSourceFile(lpszOut);
	for (int i = 1; i <= pages1; i++)
	{
		double width = 0, height = 0;

		tpl = pdf1.ImportPage(i, wxPDF_PAGEBOX_MEDIABOX);

		pdf1.GetTemplateSize(tpl, width, height);

		if (height > width)
			pdf1.AddPage(wxPORTRAIT, width, height);
		else
			pdf1.AddPage(wxLANDSCAPE, height, width);

		pdf1.UseTemplate(tpl, 0, 0);
	}
	pdf1.Close();

	int pages2 = pdf1.SetSourceFile(lpsz1);
	for (int i = 1; i <= pages2; i++)
	{
		double width = 0, height = 0;

		tpl = pdf1.ImportPage(i, wxPDF_PAGEBOX_MEDIABOX);

		pdf1.GetTemplateSize(tpl, width, height);

		if (height > width)
			pdf1.AddPage(wxPORTRAIT, width, height);
		else
			pdf1.AddPage(wxLANDSCAPE, height, width);

		pdf1.UseTemplate(tpl, 0, 0);
	}

	pdf1.SaveAsFile(lpszOut);

	return true;
}