wxPdfDC

Talk here about issues with one of the components hosted at wxCode, or suggest features for it.
Post Reply
rsb
I live to help wx-kind
I live to help wx-kind
Posts: 170
Joined: Fri May 29, 2015 7:26 pm

wxPdfDC

Post by rsb »

We're using the wxPdfDC class to create a pdf file. We use the same device context draw methods used to print to our
graphics screen but the pdf print out is not as clean. True type text is offset and lines get thicker as you zoom in.
Also, I'm not seeing how to fit the graphics into the Adobe screen. Can someone point me to some documentation
that explains how this works. The wxWidgets docs are not really helpful. Below is a snippet of code we're using.
Thanks very much.

wxPdfDC dc(&pdfDoc,17,22);
dc.SetMapModeStyle(wxPDF_MAPMODESTYLE_PDF);
dc.SetMapMode(wxMM_POINTS);
bool ok = dc.StartDoc(_("Printing ..."));
if( ok )
{
dc.StartPage();
Draw(dc);
dc.EndPage();
dc.EndDoc();
}

pdfDoc.SaveAsFile(fileName.GetFullPath()) ;
utelle
Moderator
Moderator
Posts: 1125
Joined: Tue Jul 05, 2005 10:00 pm
Location: Cologne, Germany
Contact:

Re: wxPdfDC

Post by utelle »

rsb wrote:We're using the wxPdfDC class to create a pdf file. We use the same device context draw methods used to print to our
graphics screen but the pdf print out is not as clean. True type text is offset and lines get thicker as you zoom in.
Also, I'm not seeing how to fit the graphics into the Adobe screen. Can someone point me to some documentation
that explains how this works. The wxWidgets docs are not really helpful. Below is a snippet of code we're using.
In your code you use wxPDF_MAPMODESTYLE_PDF. However, this style should only be used if you use wxPdfDC to print solely to PDF. If you use wxPDF_MAPMODESTYLE_STANDARD, results should be closer to what you get on screen. Regarding text output you might still need to take special measure. Please take a look at the sample (samples/pdfdc/printing.cpp, method MyFrame::Draw) coming with wxPdfDocument.

Regards,

Ulrich
rsb
I live to help wx-kind
I live to help wx-kind
Posts: 170
Joined: Fri May 29, 2015 7:26 pm

Re: wxPdfDC

Post by rsb »

Hello,

We're trying to use the wxPdfDocument class to display graphics in a pdf document.

Our graphics window size is: x = 948, y = 360.
The pdf window page size is: x = 558.8, y = 431.8.

I'm setting the scale using the code below. Can you let me know if I'm doing this correctly
because I'm not getting the results I was hoping for. Thank you very much.

wxPdfDocument pdfDoc ;
pdfDoc.AddPage(wxLANDSCAPE,wxPAPER_CSHEET) ;

double docWidth = pdfDoc.GetPageWidth() ;
double docHeight = pdfDoc.GetPageHeight() ;

// Get size of the graphics window
wxRect windowSize = pEditWindow->GetClientRect() ;

double windowWidth = windowSize.GetWidth() ;
double windowHeight = windowSize.GetHeight() ;

double xScale = docWidth / windowWidth ;
double yScale = docHeight / windowHeight ;
double scale = 0 ;

if( docWidth < docHeight )
scale = xScale ;
else
scale = yScale ;

pdfDoc.Scale(scale*100,scale*100) ;
utelle
Moderator
Moderator
Posts: 1125
Joined: Tue Jul 05, 2005 10:00 pm
Location: Cologne, Germany
Contact:

Re: wxPdfDC

Post by utelle »

rsb wrote:We're trying to use the wxPdfDocument class to display graphics in a pdf document.

Our graphics window size is: x = 948, y = 360.
The pdf window page size is: x = 558.8, y = 431.8.
If you are not using the wxWidgets printing framework, but just want to display graphics in a PDF document, then you use the wxPdfDocument template mechanism and let do wxPdfDocument the scaling.

Following a code snippet that might help you to get the idea:

Code: Select all

  wxPdfDocument pdfDoc;
  // ... 
  // Generate diagram as template
  // Disable auto page break
  double breakMargin = pdfDoc.GetBreakMargin();
  pdfDoc.SetAutoPageBreak(false);
  // Set width and height of the graphics window
  double width = 558.8;
  double w = 948;
  double h = 360;
  int tpl = pdfDoc.BeginTemplate(0, 0, w, h);
  {
    // Create a wxPdfDC instance in template mode
    wxPdfDC dc(&pdfDoc, w, h);
    bool ok = dc.StartDoc(_("Printing ..."));
    if (ok)
    {
      dc.StartPage();
      // Here you would insert your graphics drawing
      // ...
      dc.EndPage();
      dc.EndDoc();
    }
  }
  pdfDoc.EndTemplate();
  // Now insert the template at the desired position on the current page
  // "width" is the width you want to reserve on the PDF page for the graphics
  // The graphics will be scaled accordingly
  pdfDoc.UseTemplate(tpl, xPos, yPos, width);
  // Restore auto page break mode
  pdfDoc.SetAutoPageBreak(true, breakMargin);
  // ... 
Regards,

Ulrich
rsb
I live to help wx-kind
I live to help wx-kind
Posts: 170
Joined: Fri May 29, 2015 7:26 pm

Re: wxPdfDC

Post by rsb »

Hello,

Thanks very much for your reply.

I'm still getting poor results. I've attached the graphics (edit) window we're trying to convert to a PDF document and
the resulting PDF window, Also, the code snippet we're using.

Do you think this is the best that can be done. Thanks!

Here's the code snippet we're using which I copied mostly from your previous message:

Code: Select all

------------------------------------------------------

    itiPdfDocument pdfDoc ;
    pdfDoc.AddPage(wxLANDSCAPE,wxPAPER_CSHEET) ;

    double breakMargin = pdfDoc.GetBreakMargin() ;
    pdfDoc.SetAutoPageBreak(false) ;

    wxRect windowSize = pEditWindow->GetClientRect() ;
    double w = windowSize.GetWidth() ; // 948
    double h = windowSize.GetHeight() ; // 360

    double width = pdfDoc.GetPageWidth() ; // 558.8
    double height = pdfDoc.GetPageHeight() ; // 431.8
    {
        int tpl = pdfDoc.BeginTemplate(0, 0, w, h);
        {
            // Create a wxPdfDC instance in template mode
            wxPdfDC dc(&pdfDoc, w, h);
            bool ok = dc.StartDoc(_("Printing ..."));
            if( ok )
            {
                dc.StartPage();
                Draw(dc);
                dc.EndPage();
                dc.EndDoc();
            }
        }
        pdfDoc.EndTemplate() ;
        pdfDoc.UseTemplate(tpl, 5, 5, width);
        pdfDoc.SetAutoPageBreak(true, breakMargin);
    }

------------------------------------------------------
Attachments
EditWindow.PNG
EditWindow.PNG (15.69 KiB) Viewed 5625 times
PDFWindow.PNG
PDFWindow.PNG (15.74 KiB) Viewed 5625 times
Last edited by doublemax on Mon May 16, 2016 4:59 pm, edited 1 time in total.
Reason: Added code tags
utelle
Moderator
Moderator
Posts: 1125
Joined: Tue Jul 05, 2005 10:00 pm
Location: Cologne, Germany
Contact:

Re: wxPdfDC

Post by utelle »

rsb wrote:I'm still getting poor results. I've attached the graphics (edit) window we're trying to convert to a PDF document and
the resulting PDF window, Also, the code snippet we're using.

Do you think this is the best that can be done. Thanks!
Well, it is a bit difficult to tell from the screenshots you are showing. And it would be helpful, if you could describe in more detail what you intend to achieve. You may switch to private message to do this, if you prefer.

1) The graphics window seems to be much larger than the area that actually contains relevant drawing information. So probably you should restrict the drawing to the area of interest when writing to PDF. However, this depends on what exactly you want to achieve.

2) The screenshot of the PDF was taken at a zoom factor of 52%. So it is difficult to judge the quality. The proportions look right, however.

3) Usually you should set the width you use in the call to method UseTemplate to a value that fits on the page. You use an offset of 5, so the width should be reduced by 5 (or by 10 if you want a margin on both sides).

4) Is it intended that you have a black background in the PDF? Seems not very convenient if you intend to print the PDF.

Regards,

Ulrich
rsb
I live to help wx-kind
I live to help wx-kind
Posts: 170
Joined: Fri May 29, 2015 7:26 pm

Re: wxPdfDC

Post by rsb »

Hello,

Thanks very much for your responses.

I've attached a PDF that was created using a Bullzip PDF printer. This is what we would like to see.

1. Notice that the width of the drawing fits perfectly within the width of the PDF window.
2. The thickness of the lines in the drawing are the same everywhere. In the PDF document, the lines
get thicker as you zoom in.
3. The true-type font text is not clear. It is sometimes offset and distorted.

Thanks,
Robert.
Attachments
BullZip.PNG
BullZip.PNG (31.95 KiB) Viewed 5589 times
utelle
Moderator
Moderator
Posts: 1125
Joined: Tue Jul 05, 2005 10:00 pm
Location: Cologne, Germany
Contact:

Re: wxPdfDC

Post by utelle »

rsb wrote:I've attached a PDF that was created using a Bullzip PDF printer. This is what we would like to see.

1. Notice that the width of the drawing fits perfectly within the width of the PDF window.
I don't know how exactly you produced this Bullzip PDF. Somehow the information about the actual drawing area was passed to the printer driver, so that it could clip the irrelevant parts.

The problem with wxPdfDC is that it has no way to know the margins your code uses to draw the graphics on screen. Obviously your code uses some sort of offsets to position the graphics in the graphics window. If you use exactly the same drawing code with wxPdfDC you will get the graphics including the margins. That is, you will have to tell your drawing method that it should not use left and top margins when drawing to PDF.

Without seeing the actual code you use to produce the graphics, it is impossible to tell what you would have to change.
rsb wrote:2. The thickness of the lines in the drawing are the same everywhere. In the PDF document, the lines
get thicker as you zoom in.
Depending on line thickness used in the wxDC drawing methods you can easily get artifacts on screen that vanish if you print the PDF on paper.

Could you please provide the PDF files themselves for comparison?
rsb wrote:3. The true-type font text is not clear. It is sometimes offset and distorted.
Again, I would need to see the PDF files. The low-resolution screenshots don't help to further investigate the issue. I know that there are a few issues with text positioning in wxPdfDC due to completely different handling of text in wxDC on screen and in PDF. However, you should not get distorted text.

Could you provide the source code of your application and sample data for one of the graphics? I fear that otherwise this discussion will go on for quite some time without getting you anywhere. If you prefer, we can use private mail for further discussions.

Regards,

Ulrich
rsb
I live to help wx-kind
I live to help wx-kind
Posts: 170
Joined: Fri May 29, 2015 7:26 pm

Re: wxPdfDC

Post by rsb »

Hello,

Based on your last message, I realized what I was doing wrong. I added the wxPdfPrinter class to print
and that did it.

However, we still have a problem with rotated text. I've attached two images, the first is from the screen and the
second is from the PDF viewer. The text in the PDF viewer is shifted to the left and down. You mentioned that there
were still a few issues with text positioning in the wxPdfDC class. From the two images, can you tell if what you see
is a result of the text positioning issues or something else?

Thanks very much for all your help!

Robert
Attachments
90DegreeTextScreen.PNG
90DegreeTextScreen.PNG (10.32 KiB) Viewed 5534 times
90DegreeTextPDF.PNG
90DegreeTextPDF.PNG (9.24 KiB) Viewed 5534 times
utelle
Moderator
Moderator
Posts: 1125
Joined: Tue Jul 05, 2005 10:00 pm
Location: Cologne, Germany
Contact:

Re: wxPdfDC

Post by utelle »

rsb wrote:Based on your last message, I realized what I was doing wrong. I added the wxPdfPrinter class to print
and that did it.
Good to hear.
rsb wrote:However, we still have a problem with rotated text. I've attached two images, the first is from the screen and the
second is from the PDF viewer. The text in the PDF viewer is shifted to the left and down. You mentioned that there
were still a few issues with text positioning in the wxPdfDC class. From the two images, can you tell if what you see
is a result of the text positioning issues or something else?
This problem might be related to the map mode style. Which map mode style do you use?

However, there might also still be a bug in the wxPdfDC code for drawing rotated text. I remember that there was a bug report regarding the rotation center. The issue was fixed for wxWidgets 2.9 or higher (file wxpdfdc29.inc), but not for older versions (file wxpdfdc28.inc).

May I ask which version of wxPdfDocument and which version of wxWidgets you are using?

Regards,

Ulrich
rsb
I live to help wx-kind
I live to help wx-kind
Posts: 170
Joined: Fri May 29, 2015 7:26 pm

Re: wxPdfDC

Post by rsb »

Hello,

The map mode style is wxMM_TEXT.

wxWidgets version is 3.0.2 and wxPdfDocument version is 3.0.

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

Re: wxPdfDC

Post by utelle »

rsb wrote:The map mode style is wxMM_TEXT.
This is the common wxDC map mode. Additionally wxPdfDC has a special method for setting a so called map mode style (wxPdfDC::SetMapModeStyle(wxPdfMapModeStyle style)). The default is wxPDF_MAPMODESTYLE_STANDARD, which should be fine for your purpose. However, there is also a special map mode style wxPDF_MAPMODESTYLE_PDF that has an effect on text positioning.
rsb wrote:wxWidgets version is 3.0.2 and wxPdfDocument version is 3.0.
There is no version 3.0 of wxPdfDocument. The latest released version is 0.9.5 (https://github.com/utelle/wxpdfdoc/releases/tag/v0.9.5). Please check the version of wxPdfDocument you are using. If it is not 0.9.5, please upgrade, since the problem with rotated text in wxPdfDC was fixed in version 0.9.5 only.

Regards,

Ulrich
rsb
I live to help wx-kind
I live to help wx-kind
Posts: 170
Joined: Fri May 29, 2015 7:26 pm

Re: wxPdfDC

Post by rsb »

Hello,

Thanks, that did it. I was looking at the DLL/lib names "wxcode_msw30ud_pdfdoc" and thought
that 3.0 was the version. The Map Mode Style worked using the default value but not the ...PDF
value.

Thanks again for your help.

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

Re: wxPdfDC

Post by utelle »

rsb wrote:Thanks, that did it.
Glad to hear that.
rsb wrote:I was looking at the DLL/lib names "wxcode_msw30ud_pdfdoc" and thought that 3.0 was the version.
No, this is the wxWidgets version.
rsb wrote:The Map Mode Style worked using the default value but not the ...PDF value.
The ...PDF value is meant for applications were solely PDF output is produced. If you want to print screen drawings to PDF via wx[Pdf]DC, the standard map mode style is usually the right value. On rare occasions one has to use one of the platform dependent values.
rsb wrote:Thanks again for your help.
You are welcome.

Regards,

Ulrich
Post Reply