wxPdfDocument - How to make a precise copy of PDF file?

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 - How to make a precise copy of PDF file?

Post by cutecode »

I use this code to import and save PDF file

Code: Select all

	int tpl;

	wxPdfDocument pdf;

	int pages = pdf.SetSourceFile(szInFile);

	for (int i=1; i<=pages; i++)
	{
		pdf.AddPage();
		tpl = pdf.ImportPage(i);
		pdf.UseTemplate(tpl);
	}

	pdf.SaveAsFile(szOutFile);
but size of output file is not the same as original,
and page orientation is always A4 portret

How to make a precise copy of PDF?
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 - How to make a precise copy of PDF file?

Post by utelle »

cutecode wrote:I use this code to import and save PDF file

Code: Select all

	int tpl;

	wxPdfDocument pdf;

	int pages = pdf.SetSourceFile(szInFile);

	for (int i=1; i<=pages; i++)
	{
		pdf.AddPage();
		tpl = pdf.ImportPage(i);
		pdf.UseTemplate(tpl);
	}

	pdf.SaveAsFile(szOutFile);
but size of output file is not the same as original, and page orientation is always A4 portret
How should wxPdfDocument guess what you intend to do? wxPdfDocument does what you tell it to do.

If you don't specify a page orientation and page size on instantiating a wxPdfDocument object, you get A4 portrait as the default. The page orientation and the page size can be specified either in the constructor as the default for the whole document or in method AddPage for each individual page.
cutecode wrote:How to make a precise copy of PDF?
Thinking heretically I'd say "Just copy the original PDF file to a new destination using the wxCopyFile function". :twisted:

If you want to extract pages from an existing PDF file and insert an exact copy into a new document, you have to take appropriate action:
  1. Use ImportPage with second parameter wxPDF_PAGEBOX_MEDIABOX to get the physical page
  2. Use GetTemplateSize to determine the width and height of the imported page
  3. If you know that all pages in the existing document have the same size, instantiate a wxPdfDocument with the page size determined in the previous step. Otherwise use AddPage with page size parameters for each page
  4. Finally use UseTemplate with explicit positioning to coordinate (0,0)
HTH

Regards,

Ulrich
User avatar
cutecode
Super wx Problem Solver
Super wx Problem Solver
Posts: 425
Joined: Fri Dec 09, 2016 7:28 am
Contact:

Re: wxPdfDocument - How to make a precise copy of PDF file?

Post by cutecode »

Hello, utelle

All I need is to insert an Image in a PDF file. And I don't know size and orientation of pages in this document

I didn't find any example in examples folder, how to insert image in existing PDF
There is example how to create NEW PDF and insert image, but not insert an image in existing PDF
I'll try your steps
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 - How to make a precise copy of PDF file?

Post by utelle »

cutecode wrote:All I need is to insert an Image in a PDF file. And I don't know size and orientation of pages in this document
After calling ImportPage you can use GetTemplateSize to determine the width and height of the imported page, and from width and height you can deduce the page orientation.
cutecode wrote:I didn't find any example in examples folder, how to insert image in existing PDF
There is example how to create NEW PDF and insert image, but not insert an image in existing PDF
I'll try your steps
Well, in fact wxPdfDocument is a library to generate new documents, not a tool to manipulate existing documents. As a compromise wxPdfDocument allows to import complete pages and to embed them as templates in new documents. However, analyzing the page content of existing PDF documents is beyond the scope of wxPdfDocument. The building blocks to get access to the page content of an existing PDF document are available in wxPdfDocument, but parsing the page content is not supported (and probably never will be).

Maybe a library like pdfedit would be better suited to fulfill your needs.

Regards,

Ulrich
User avatar
cutecode
Super wx Problem Solver
Super wx Problem Solver
Posts: 425
Joined: Fri Dec 09, 2016 7:28 am
Contact:

Re: wxPdfDocument - How to make a precise copy of PDF file?

Post by cutecode »

thank you. I soved it using

Code: Select all

if (height > width)
	pdf.AddPage(wxPORTRAIT, width, height);
else
	pdf.AddPage(wxLANDSCAPE, height, width);
I expect all my pages to be A4

once more great thanks
wx 3.1.6 win/mac/linux

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