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.

Thu Sep 21, 2017 8:18 am

hi,i want to convent a html page to a4 size pdf. But my html maybe too long and it generate a one page pdf (a4 width but not a4 height). how can i set the page setting to auto pagination? here is my code thanks for help

Code: Select all
            PdfDocument doc = new PdfDocument();
            PdfHtmlLayoutFormat htmlLayoutFormat = new PdfHtmlLayoutFormat();
            htmlLayoutFormat.IsWaiting = false;
            htmlLayoutFormat.Layout = PdfLayoutType.Paginate;           
            PdfPageSettings setting = new PdfPageSettings();
            setting.Size = PdfPageSize.A4;
            Thread thread = new Thread(() =>
            { doc.LoadFromHTML(real_all_content, false, setting, htmlLayoutFormat); });
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            thread.Join();

shing920826
 
Posts: 3
Joined: Thu Sep 21, 2017 2:03 am

Thu Sep 21, 2017 8:37 am

Hi,

Thanks for your inquiry.
Please set the second parameter "autoDetectPageBreak" in the LoadFromHTML method to be true, so that the auto pagination will make sense.
Code: Select all
doc.LoadFromHTML(real_all_content, true, setting, htmlLayoutFormat);


Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Thu Sep 21, 2017 9:21 am

Thank you so much, its work.
However, i have another question about the content, after i set header and footer to the page, and the bottom content will disappear and does not show in the next page. how can i solve this problem? Here is my code, Many thanks.

Code: Select all
            PdfMargins margin = new PdfMargins();
            PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
            margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Bottom = margin.Top;
            margin.Left = unitCvtr.ConvertUnits(4.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Right = margin.Left;
            SetDocumentTemplate(doc, PdfPageSize.A4, margin);

the function is
Code: Select all
 protected void SetDocumentTemplate(PdfDocument doc, SizeF pageSize, PdfMargins margin)
        {
            //set a top page template
            PdfPageTemplateElement topSpace = new PdfPageTemplateElement(pageSize.Width, margin.Top);
            PdfPageTemplateElement footerSpace = new PdfPageTemplateElement(pageSize.Width, margin.Bottom);
            topSpace.Foreground = true;
            doc.Template.Top = topSpace;
            footerSpace.Foreground = true;
            doc.Template.Bottom = footerSpace;
            //draw text header in template
            PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Trebuchet MS", 14f, FontStyle.Italic));
            PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right);

            float y = 0;
            float x = PdfPageSize.A4.Width;
           // topSpace.Graphics.DrawString(label, font1, PdfBrushes.PaleVioletRed, x, y, format);
            //drew an image header with custom size in template
            string hsrc =  Server.MapPath(ConfigurationSettings.AppSettings["IMAGE_FOLDER"]) + "billheader.jpg";
            string fsrc = Server.MapPath(ConfigurationSettings.AppSettings["IMAGE_FOLDER"]) + "billfooter.png";
            PdfImage headerImage = PdfImage.FromFile(hsrc);
            PdfImage footerImage = PdfImage.FromFile(fsrc);
            float hwidth = headerImage.Width;
            float hheight = headerImage.Height;
            float fwidth = footerImage.Width;
            float fheight = footerImage.Height;
            float z = x - hwidth;
            float c = 15f - fheight;
            PointF pageLeftTop = new PointF(0, 0);
            PdfPageNumberField number = new PdfPageNumberField();
            //Create page count automatic field
            PdfPageCountField count = new PdfPageCountField();
            //Add the fields in composite field
            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.Middle);
            compositeField.Bounds = footerSpace.Bounds;
            //Draw composite field at footer space
            topSpace.Graphics.DrawImage(headerImage, z, 0, hwidth, hheight);
            footerSpace.Graphics.DrawImage(footerImage, 0, c, fwidth, fheight);
            compositeField.Draw(footerSpace.Graphics);
         
        }

shing920826
 
Posts: 3
Joined: Thu Sep 21, 2017 2:03 am

Thu Sep 21, 2017 10:07 am

Hello,

Thanks for your inquiry.
I suggest you first using our latest version(Spire.PDF Pack(Hot Fix) Version:3.9.285) to have a try since the issue did not occur on my side.
If the issue still troubles you, please share the html string and images you use.

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Thu Sep 21, 2017 10:16 am

i have try the latest version, it is the same. my condition is i have add the header image and some of the content will miss. For example, i got 20 item, first page can show 1-8 item, and the second page will start with 12 and the 9-11 are missing.

shing920826
 
Posts: 3
Joined: Thu Sep 21, 2017 2:03 am

Fri Sep 22, 2017 3:26 am

Hi shing920826,

Thanks for your response.
Sorry I still couldn't reproduce your issue. To help us with a better investigation, please send the sample materials to us(support@e-iceblue.com), including the url, the image and the genreated pdf.

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Thu Mar 01, 2018 5:22 pm

Try using ZetPDF.com
It worked for me!

Best regards

Fred Green

fredgreen333
 
Posts: 1
Joined: Thu Mar 01, 2018 5:18 pm

Return to Spire.PDF