Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
To create PDF files, I am using the PDFSharp Library, the output which I get varies in position of the rectangle when compared to the output when I draw manually on the page.

Below is the sample code.

I have tried drawing the rectangle at (0,0), in this case, both the outputs are the same. But when I am drawing at specific given location, the output varies.

Please help!

What I have tried:

VB.NET
Imports PdfSharp.Pdf
Imports PdfSharp.Drawing

Public Class Form1
 
    Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        e.Graphics.DrawRectangle(Pens.Black, 567, 280, 86, 17)
    End Sub
    Private Sub PrintDocument1_PrintPagex(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        Dim doc As New PdfDocument
        Dim page As PdfPage = doc.AddPage
 
        Dim gfx As XGraphics = XGraphics.FromPdfPage(page, XGraphicsUnit.Point)
 
        gfx.DrawRectangle(XPens.Black, 567, 280, 86, 17)
 
        Dim filename As String = "D:\Hello.pdf"
        doc.Save(filename)
 
    End Sub
End Class
Posted
Updated 29-Aug-23 2:18am
v3

1 solution

The Graphics instance uses GraphicsUnit.Display as the PageUnit. And for printers, this is 1/100 inch for printers, i.e. 100dpi.
The current implementation of PDFsharp has only one layout of the graphics context. The origin (0, 0) is top left and coordinates grow right and down. The unit of measure is always point (1/72 inch).
Clearly, 567 × 1/100" is not the same size as 567 × 1/72".

If you want the same size in both outputs, then you'll need to scale your coordinates and sizes appropriately.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900