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.

Tue Nov 20, 2018 2:30 pm

Hello, I am try to print some text in page Margins. But looks like both page.Canvas.DrawString and doc.Template.Bottom clip off any text which falls in margins. Can you guys tell me if library supports writing content in margins?

In addition whenever I try to print some text in templates my text is very blurry. I have attached sample images for you to see. How should I fix this?

Code: Select all
protected void AddPageHeaderAndFooter(PdfDocument doc, UserInfoModel userInfo, SizeF pageSize)
        {
            doc.Template.Bottom = new PdfPageTemplateElement(pageSize.Height, _margin.Bottom) { Foreground = true };

            for (var pageNumber = 1; pageNumber <= doc.Pages.Count; pageNumber++)
            {
                // Add Page Footer
                doc.Template.Bottom.Graphics.SetTransparency(0.75f);
                var footerFormat = new PdfStringFormat(PdfTextAlignment.Center) { LineAlignment = PdfVerticalAlignment.Middle };
                var pageCount = $"Page {pageNumber} of {doc.Pages.Count}";
                doc.Template.Bottom.Graphics.DrawString(
                    pageCount,
                    BodyFont,
                    PdfBrushes.Black,
                    pageSize.Height / 2 - BodyFont.MeasureString(pageCount).Width / 2,
                    doc.Template.Bottom.Height - BodyFont.Height,
                    footerFormat);

                //var format = new PdfStringFormat(PdfTextAlignment.Center) { LineAlignment = PdfVerticalAlignment.Middle };
                //var font = new PdfTrueTypeFont(new Font("Arial", 12f, FontStyle.Regular));
                //var pageNumberLabel = new PdfCompositeField
                //{
                //    AutomaticFields = new PdfAutomaticField[] { new PdfPageNumberField(), new PdfPageCountField()  },
                //    Brush = PdfBrushes.LightGray,
                //    Font = font,
                //    StringFormat = format,
                //    Text = "page {0} of {1}",
                //    Pen = new PdfPen(Color.LightGray, 0.75f)
                //};
                //pageNumberLabel.Draw(doc.Template.Bottom.Graphics, pageSize.Height / 2, font.Height);
            }
        }


BTW, I am not able to perfectly center my footer while using Automatic Fields. Also Using automatic fields method also results in blurry image

hemibu
 
Posts: 1
Joined: Mon Nov 19, 2018 9:41 am

Wed Nov 21, 2018 6:00 am

Hi hemibu,

Thanks for your inquiry.
Kindly note if you use PdfPageTemplateElement to add page number, each page will be changed automatically, and there is no need to use a loop. Considering your case, please use field. Below is my testing code:
Code: Select all
            PdfDocument doc = new PdfDocument();
            doc.Pages.Add();
            doc.Pages.Add();
            AddFooter15650(doc, doc.PageSettings.Size);
            doc.SaveToFile("15650.pdf", FileFormat.PDF);

            public static void AddFooter15650(PdfDocument doc, SizeF pageSize)
            {
                doc.Template.Bottom = new PdfPageTemplateElement(pageSize.Width, doc.PageSettings.Margins.Bottom) { Foreground = true };
                PdfPageNumberField number = new PdfPageNumberField();
                //Create page count automatic field
                PdfPageCountField count = new PdfPageCountField();
                //Add the fields in composite field
                PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 12f, FontStyle.Bold));
                PdfCompositeField compositeField = new PdfCompositeField(font1, PdfBrushes.Black, "Page {0} of {1}", number, count);
                //Align string of "Page {0} of {1}" to center
                compositeField.StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Top);
                compositeField.Bounds = doc.Template.Bottom.Bounds;
                //Draw composite field at footer space
                compositeField.Draw(doc.Template.Bottom.Graphics, -(doc.PageSettings.Margins.Left), 0);
            }

The result is correct, you could check the attachment. If you still have the issue, please share your input file(if any) and entire code for testing and further investigation.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Fri Nov 23, 2018 7:36 am

Hi hemibu,

Greetings from E-iceblue.
Did the code I provided help you solve your issue? Could you please give us some feedback at your convenience?

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Return to Spire.PDF