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.
User avatar
cutecode
I live to help wx-kind
I live to help wx-kind
Posts: 181
Joined: Fri Dec 09, 2016 7:28 am
Contact:

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

Postby cutecode » Tue Apr 24, 2018 11:15 am

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?
regards,
Alexander Saprykin
www.wxwidgets.ru

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

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

Postby utelle » Tue Apr 24, 2018 1:08 pm

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
I live to help wx-kind
I live to help wx-kind
Posts: 181
Joined: Fri Dec 09, 2016 7:28 am
Contact:

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

Postby cutecode » Tue Apr 24, 2018 1:37 pm

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
regards,
Alexander Saprykin
www.wxwidgets.ru

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

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

Postby utelle » Tue Apr 24, 2018 2:56 pm

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
I live to help wx-kind
I live to help wx-kind
Posts: 181
Joined: Fri Dec 09, 2016 7:28 am
Contact:

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

Postby cutecode » Thu Apr 26, 2018 12:36 am

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
regards,
Alexander Saprykin
www.wxwidgets.ru


Return to “wxCode”

Who is online

Users browsing this forum: No registered users and 1 guest