wxPdfDocument joining 2 PDF files. Is it possible?

Talk here about issues with one of the components hosted at wxCode, or suggest features for it.
Post Reply
User avatar
cutecode
Super wx Problem Solver
Super wx Problem Solver
Posts: 425
Joined: Fri Dec 09, 2016 7:28 am
Contact:

wxPdfDocument joining 2 PDF files. Is it possible?

Post by cutecode »

hello

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


Thnk you
wx 3.1.6 win/mac/linux

regards,
Alexander Saprykin
https://v2.dental-soft.ru
Kvaz1r
Super wx Problem Solver
Super wx Problem Solver
Posts: 357
Joined: Tue Jun 07, 2016 1:07 pm

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

Post 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.
utelle
Moderator
Moderator
Posts: 1125
Joined: Tue Jul 05, 2005 10:00 pm
Location: Cologne, Germany
Contact:

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

Post 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
Kvaz1r
Super wx Problem Solver
Super wx Problem Solver
Posts: 357
Joined: Tue Jun 07, 2016 1:07 pm

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

Post 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 ;-)
User avatar
cutecode
Super wx Problem Solver
Super wx Problem Solver
Posts: 425
Joined: Fri Dec 09, 2016 7:28 am
Contact:

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

Post 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?
wx 3.1.6 win/mac/linux

regards,
Alexander Saprykin
https://v2.dental-soft.ru
utelle
Moderator
Moderator
Posts: 1125
Joined: Tue Jul 05, 2005 10:00 pm
Location: Cologne, Germany
Contact:

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

Post 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.
User avatar
cutecode
Super wx Problem Solver
Super wx Problem Solver
Posts: 425
Joined: Fri Dec 09, 2016 7:28 am
Contact:

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

Post 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;
}
wx 3.1.6 win/mac/linux

regards,
Alexander Saprykin
https://v2.dental-soft.ru
Post Reply