PDFsharp & MigraDoc Forum
https://forum.pdfsharp.net/

Graphicsobject is not positioningto center for this PDF file
https://forum.pdfsharp.net/viewtopic.php?f=2&t=4729
Page 1 of 1

Author:  hsikkandar [ Wed Nov 20, 2024 2:18 pm ]
Post subject:  Graphicsobject is not positioningto center for this PDF file

Hi All,
I am trying to draw a string in the center of the page. The below code is working for almost all the file. for the specific file attached its not positioning the graphics object correctly. Its drawing the string somewhere. Any help would be appreciated.

The below is the code snippet:
====================
Code:
  XFont font = new XFont(fontFamilyName, FontSize, XFontStyleEx.Regular); //"Times New Roman" ,22 is fontsize
 PdfDocument doc = PdfReader.Open(sourceFileName, PdfDocumentOpenMode.Modify);
  XBrush brush = new XSolidBrush(XColor.FromArgb(128, 127, 127, 127));  //50%
  foreach (PdfPage page in doc.Pages)
   {
                     using (XGraphics graphics = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Append))
                    {
                        double dwPageWidthInPoints = mediabox.Width;
                        double dwpageHeightInPoints = mediabox.Height;

                        // Convert dimensions to inches
                        double dwpageWidthInInches = dwPageWidthInPoints / 72;
                        double dwpageHeightInInches = dwpageHeightInPoints / 72;
                        double centerX = dwPageWidthInPoints / 2;
                        double centerY = dwpageHeightInPoints / 2;
        graphics.DrawString("CONFIDENTIAL",, font, brush, new XPoint(centerX, centerY));
                     }

Author:  hsikkandar [ Wed Nov 20, 2024 2:31 pm ]
Post subject:  Re: Graphicsobject is not positioningto center for this PDF

Sorry , i could not able to attach the file here as it is size is >256KB, How to send the file

Author:  () => true [ Wed Nov 20, 2024 2:42 pm ]
Post subject:  Re: Graphicsobject is not positioningto center for this PDF

hsikkandar wrote:
Sorry , i could not able to attach the file here as it is size is >256KB, How to send the file
See here:
https://docs.pdfsharp.net/General/Issue-Reporting.html

Bear in mind that MediaBox has four members. Your code only uses 2.

You may have to take the clipping box or other boxes into account.

Author:  hsikkandar [ Wed Nov 20, 2024 3:16 pm ]
Post subject:  Re: Graphicsobject is not positioningto center for this PDF

Thanks Thomas. I will check the option you mentioned. Regarding the sample file, i have sent the same file to "issues@pdfsharp.net" for the other topics last week(Nov 14, 2024) with the subject "Error in opening file"(Forwarded again today also). It was UTF16-LE file. I will also try to upload the issue submission form.

Author:  () => true [ Thu Nov 21, 2024 8:48 am ]
Post subject:  Re: Graphicsobject is not positioningto center for this PDF

() => true wrote:
You may have to take the clipping box or other boxes into account.
Take the values of the TrimBox into account.
viewtopic.php?p=14569#p14569

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/