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 Feb 01, 2016 6:36 am

Hi,
I have a huge text content which will fit usually in more than one page but when i use the below code, the text ends by the end of the first page and it doesn't create a second page. Is there a feature to do this automatically or this need to be handled grammatically and what is the best approach (depending on text length or !! )

Code: Select all
//Document is just internal class for my data
  private void AddContent(PdfPageBase page, Document document)
        {

            PdfBrush solidBrush = new PdfSolidBrush(Color.Black);
         
            System.Drawing.Font contentFont = new System.Drawing.Font("Arial", 12f, FontStyle.Regular);
            PdfTrueTypeFont contentTrueTypeFont = new PdfTrueTypeFont(contentFont);
            contentTrueTypeFont = new PdfTrueTypeFont(contentFont, true);

            RectangleF rctgContent = new RectangleF(page.Canvas.ClientSize.Width * 0.25f, 70, page.Canvas.ClientSize.Width *     0.75f, page.Canvas.ClientSize.Height);
            PdfStringFormat rightFormat = new PdfStringFormat(PdfTextAlignment.Right);

            rightFormat.RightToLeft = true;

            page.Canvas.DrawString(document.Content + document.Content, contentTrueTypeFont, solidBrush, rctgContent, rightFormat);

            RectangleF rctgImage = new RectangleF(0, 70, page.Canvas.ClientSize.Width * 0.25f, page.Canvas.ClientSize.Width * 0.25f);
            Image img = GetImage(document.PhotosUrls[0]);
            PdfImage image = PdfImage.FromImage(img);
            page.Canvas.DrawImage(image, rctgImage);
            page.Canvas.SetTransparency(1);

        }

jaddeveloper
 
Posts: 5
Joined: Mon Feb 01, 2016 6:23 am

Mon Feb 01, 2016 8:30 am

Hi,

Thanks for your posting and using our Spire.PDF component.
Please refer to the following solution to draw content into multi pages.
Code: Select all
PdfDocument doc = new PdfDocument();
            PdfSection section = doc.Sections.Add();
            PdfPageBase page = section.Pages.Add();
            PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 11);

            PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right);
            format.LineSpacing = 20f;
            format.RightToLeft = true;
            PdfBrush brush = PdfBrushes.Black;

            PdfTextWidget textWidget = new PdfTextWidget(text, font, brush);

            float y = 0;

            PdfTextLayout textLayout = new PdfTextLayout();

            textLayout.Break = PdfLayoutBreakType.FitPage;

            textLayout.Layout = PdfLayoutType.Paginate;

            RectangleF bounds = new RectangleF(new PointF(0, y), page.Canvas.ClientSize);

            textWidget.StringFormat = format;

            textWidget.Draw(page, bounds, textLayout);

            doc.SaveToFile("result.pdf", FileFormat.PDF);


Best Regards,
Amy
E-iceblue support team
User avatar

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

Mon Feb 01, 2016 8:57 am

Thanks for your fast reply, I got the following error
An error exist on this page. Acrobat may not display the page correctly. Please contact the person who created the PDF document.

Also the header disappear from the second page and footer got misaligned. Below how i create the header
Code: Select all
  private void AddHeader(PdfDocument doc, SizeF pageSize, PdfMargins margin, string documentTitle)
        {
            //set top page template
            PdfPageTemplateElement topSpace = new PdfPageTemplateElement(pageSize.Width, margin.Top);
            topSpace.Foreground = true;
            doc.Template.Top = topSpace;

            //drew an image in header with custom size
            PdfImage headerImage = PdfImage.FromFile("...");
            float width = 150;
            float height = 100;
            PointF pageLeftTop = new PointF(0, 0);
            topSpace.Graphics.DrawImage(headerImage, 0, 0, width / 2, height / 2);

            //drew an image in header with custom size
            PdfImage headerImage2 = PdfImage.FromFile("...");
            float width2 = 150;
            float height2 = 100;
            PointF pageRightTop = new PointF(PdfPageSize.A4.Width - width, 0);
            topSpace.Graphics.DrawImage(headerImage2, PdfPageSize.A4.Width - width2 / 2, 0, width2 / 2, height2 / 2);

            //draw text in header
            PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 14f, FontStyle.Bold), true);
            PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Center);
            format.RightToLeft = true;
            String label = documentTitle;
            //SizeF size = font1.MeasureString(label, format);
            float y = 15f;
            float x = (PdfPageSize.A4.Width / 2);
            topSpace.Graphics.DrawString(label, font1, PdfBrushes.Black, x, y, format);
        }


I have included the pdf result in the attachment.

jaddeveloper
 
Posts: 5
Joined: Mon Feb 01, 2016 6:23 am

Mon Feb 01, 2016 9:05 am

Hi,

Please share all your code and used text/image for investigation. It will help us work out solution to resolve the two issues soon.

Best Regards,
Amy
E-iceblue support team
User avatar

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

Mon Feb 01, 2016 9:23 am

Is there any email i can send the code to as i dont want to place it in public. Plus i cant send the whole text and images as most of the data are coming from web API so its not actually files

jaddeveloper
 
Posts: 5
Joined: Mon Feb 01, 2016 6:23 am

Tue Feb 02, 2016 1:29 am

Hi,

I have some sample code about drawing header in all pages, please refer to the following code.
Code: Select all
  PdfDocument doc = new PdfDocument();
            PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
            PdfMargins margin = new PdfMargins();
            margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Bottom = margin.Top;
            margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Right = margin.Left;

            AddHeader(doc, PdfPageSize.A4, margin, documentTitle);

            PdfSection section = doc.Sections.Add();
            section.PageSettings.Size = PdfPageSize.A4;
            section.PageSettings.Margins = new PdfMargins(0);
            PdfPageBase page = section.Pages.Add();

            PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 11);

            PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right);

            format.LineSpacing = 20f;
            format.RightToLeft = true;
            PdfBrush brush = PdfBrushes.Black;

            PdfTextWidget textWidget = new PdfTextWidget(text, font, brush);

            float y = 0;

            PdfTextLayout textLayout = new PdfTextLayout();

            textLayout.Break = PdfLayoutBreakType.FitPage;

            textLayout.Layout = PdfLayoutType.Paginate;

            RectangleF bounds = new RectangleF(new PointF(0, y), page.Canvas.ClientSize);

            textWidget.StringFormat = format;

            textWidget.Draw(page, bounds, textLayout); 
 doc.SaveToFile("TxtToPDf.pdf", Spire.Pdf.FileFormat.PDF);

If the above code doesn't help, please send all your code to [email protected].
Thank you.

Best Regards,
Amy
E-iceblue support team
User avatar

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

Tue Feb 02, 2016 7:07 am

Hi Amy,

AddHeader function is not the sample code, anyway i have sent all the code to your email. please check and let me know as header and footer is getting missed up.

jaddeveloper
 
Posts: 5
Joined: Mon Feb 01, 2016 6:23 am

Tue Feb 02, 2016 8:50 am

Hi,

Thanks for your response.
Please refer to the following solution to add header and footer.
Code: Select all
 
            PdfSection section = doc.Sections.Add();
            PdfPageBase page = section.Pages.Add();

            //set margin   
            PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
            PdfMargins margin = new PdfMargins();
            margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Bottom = margin.Top;
            margin.Left = unitCvtr.ConvertUnits(4.50f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Right = margin.Left;
            SetDocumentTemplate(doc, PdfPageSize.A4, margin);
static void SetDocumentTemplate(PdfDocument doc, SizeF pageSize, PdfMargins margin,string label)
        {
            PdfPageTemplateElement leftSpace
                = new PdfPageTemplateElement(margin.Left, pageSize.Height);
            doc.Template.Left = leftSpace;

            PdfPageTemplateElement topSpace
                = new PdfPageTemplateElement(pageSize.Width, margin.Top);
            topSpace.Foreground = true;
            doc.Template.Top = topSpace;
            PdfImage headerImage = PdfImage.FromFile(@"..\\..\\logo1.png");
            float width = 150;
            float height = 100;
            PointF pageLeftTop = new PointF(0, 0);
            topSpace.Graphics.DrawImage(headerImage, 0, 0, width / 2, height / 2);

            //drew an image in header with custom size
            PdfImage headerImage2 = PdfImage.FromFile(@"..\\..\\logo2.png");
            float width2 = 150;
            float height2 = 100;
            PointF pageRightTop = new PointF(PdfPageSize.A4.Width - width, 0);
            topSpace.Graphics.DrawImage(headerImage2, PdfPageSize.A4.Width - width2 / 2, 0, width2 / 2, height2 / 2);
            //draw header label
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Italic));
            PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right);
            format.RightToLeft = true;
            SizeF size = font.MeasureString(label, format);
            float y = topSpace.Height - font.Height - 1;
            PdfPen pen = new PdfPen(Color.Black, 0.75f);
            topSpace.Graphics.SetTransparency(0.5f);
            topSpace.Graphics.DrawLine(pen, 0, y, pageSize.Width, y);
            y = y - 1 - size.Height;
            topSpace.Graphics.DrawString(label, font, PdfBrushes.Black, pageSize.Width / 2 - size.Width / 2 + topSpace.Width,y, format);

            PdfPageTemplateElement rightSpace
                = new PdfPageTemplateElement(margin.Right, pageSize.Height);
            doc.Template.Right = rightSpace;

            PdfPageTemplateElement bottomSpace
                = new PdfPageTemplateElement(pageSize.Width, margin.Bottom);
            bottomSpace.Foreground = true;
            doc.Template.Bottom = bottomSpace;

            //draw footer label
            y = font.Height + 1;
            bottomSpace.Graphics.SetTransparency(0.5f);
            bottomSpace.Graphics.DrawLine(pen, 0, y, pageSize.Width, y);
            y = y + 1;
            bottomSpace.Graphics.DrawString("Confidential", font, PdfBrushes.Black,0,y, new PdfStringFormat(PdfTextAlignment.Left));
         
            PdfPageNumberField pageNumber = new PdfPageNumberField();
            PdfPageCountField pageCount = new PdfPageCountField();
            PdfCompositeField pageNumberLabel = new PdfCompositeField();
            pageNumberLabel.AutomaticFields
                = new PdfAutomaticField[] { pageNumber, pageCount };
            pageNumberLabel.Brush = PdfBrushes.Gray;
            pageNumberLabel.Font = font;
            pageNumberLabel.StringFormat = format;
            pageNumberLabel.Text = "page {0} of {1}";
         
            pageNumberLabel.Draw(bottomSpace.Graphics, pageSize.Width - margin.Right, y);
            bottomSpace.Graphics.SetTransparency(1f);
        }


There is a full sample on our website, http://www.e-iceblue.com/Tutorials/Spir ... B.NET.html.

Best Regards,
Amy
E-iceblue support team
User avatar

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

Thu Feb 04, 2016 8:50 am

Hi,

Has your issue been resolved?
Thanks for your feedback.

Best Regards,
Amy
E-iceblue support team
User avatar

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

Mon Feb 15, 2016 10:46 am

Hi Amy,

Yeah I was able to resolve by fixing some text on the header which seems were affecting the whole document. Thanks for your help.

jaddeveloper
 
Posts: 5
Joined: Mon Feb 01, 2016 6:23 am

Tue Feb 16, 2016 1:38 am

Hi,

Thanks for your feedback.
Welcome to feel free to write to us again if you have further problems.

Best Regards,
Amy
E-iceblue support team
User avatar

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

Return to Spire.PDF