Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Mon Jun 20, 2016 11:48 pm

It seems like DrawImage uses pdfmargins to calculate x,y but DrawString disregards margins.
Is that true?
My code has this:
Pages.Add(PdfPageSize.Letter, new PdfMargins(leftRightMarginPoints,topBottomMarginPoints));
Canvas.DrawImage(pdfImage, 0, 0, widthPoints, heightPoints);
Canvas.DrawString(fieldValue, font, brush, textBoundingRectanglePoints, stringFormatAlignment);
My image gets positioned at left and top margin, but my text goes exactly to textBoundingRectanglePoints no matter what pdfmargins are.

A few hours later... I see my real problem! Can someone explain this?
PdfDocument doc = new PdfDocument();
PdfPageBase page1 = doc.Pages.Add(PdfPageSize.Letter, new PdfMargins(leftRightMarginPoints,topBottomMarginPoints));

I expect (page1 == doc.Pages[0]) to be true; but it is false!!!
page knows about my margins; but doc.Pages[0] does not know about my margins.
Why is doc.Pages[0] not equal to page1? I pass doc to other methods and assume doc.Pages[0] will give me page1.
Is there a way to get page1 if you have doc?

kensmith38
 
Posts: 9
Joined: Sun Oct 12, 2014 6:01 pm

Tue Jun 21, 2016 6:51 am

Hi,

Sorry for the delayed response.
I did a testing with Spire.PDF Pack(Hot Fix) Version:3.7.91 that DrawString didn't disregard margins and doc.Pages[0] is equal to page1.
This is the pdf result, I did some marks.
pdf result.png

Here is my testing code.
Code: Select all
 PdfDocument doc = new PdfDocument();
            float leftRightMarginPoints = 40;
            float topBottomMarginPoints = 40;
            PdfPageBase page1 = doc.Pages.Add(PdfPageSize.Letter, new PdfMargins(leftRightMarginPoints, topBottomMarginPoints));
            string text=null;
            if (page1 == doc.Pages[0])
            {
                text = "Hello";
            }
            page1.Canvas.DrawImage(PdfImage.FromFile("C:\\Icon2.png"), 0, 0, 30, 40);
            PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 8f, PdfFontStyle.Italic);
            PdfSolidBrush brush = new PdfSolidBrush(System.Drawing.Color.Blue);
            PdfStringFormat stringFormatAlignment = new PdfStringFormat();
            stringFormatAlignment.Alignment = PdfTextAlignment.Left;
            RectangleF textBoundingRectanglePoints = new RectangleF(0, 50, 30, 20);
            page1.Canvas.DrawString(text, font, brush, textBoundingRectanglePoints, stringFormatAlignment);
            textBoundingRectanglePoints = new RectangleF(80, 0, 30, 20);
            page1.Canvas.DrawString(text, font, brush, textBoundingRectanglePoints, stringFormatAlignment);
            string result = "7902.pdf";
            doc.SaveToFile(result);
            System.Diagnostics.Process.Start(result);


Best Regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2774
Joined: Wed Jun 27, 2012 8:50 am

Tue Jun 21, 2016 2:49 pm

Hi Amy,
I am using Free Spire. The dll is version 3.2.52.56040. Maybe an upgrade to the newer version would fix my problem.
Can the hot fix you referenced be installed in my Free Spire environment? Note that we do not see any warning messages about a free trial, and such a warning would intimidate end users.

kensmith38
 
Posts: 9
Joined: Sun Oct 12, 2014 6:01 pm

Wed Jun 22, 2016 2:19 am

Hi,

Thanks for your reply.
I have forwarded the issue of Free Edition to our dev team. We will inform you when new version of Free Edition is released but we only maintain our free version when we have enough time and sorry that it may needs some time to release.
We can offer you a 1-month free license to remove the warning message. Please contact our sales team(sales@e-iceblue.com) if you needs it.

Best Regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2774
Joined: Wed Jun 27, 2012 8:50 am

Return to Spire.PDF

cron