Convert HTML to PDF - change font- margin and size

I am converting an hml string to PDF. the issues are:


1) ignoring margins when printing
2) ignoring font size
3) need to set to legal size paper

code

string pageSource="
REPORT NO. xxxxxxx Company Name RUN DATE 02/01/17 PAGE 1
PROG. ID xxxxxx XXXXX/XXXX XXX XXXXXXXXXXX XXXXXXX Statement RUN TIME 03:59:11
For Month Ending JAN 2017

XXXXXXXXXXX Company BCAG XXXXXXXXXXX COMPANY, LTD.
XXXXXXXXXXX ID No. I0001
XXXXXX No. 207139/XXXXX UNIVERSE

XXXXX_XXXX CARE NEW XXX XXXXX_XXXX CARE USED XXX NON-XXXXX XXX Grand Total
Current Calendar Year Inception Current Calendar Year Inception Current Calendar Year Inception Current Calendar Year Inception
Month Todate Todate Month Todate Todate Month Todate Todate Month Todate Todate

A. PREMIUM
1. XXXX XXXXXXX 10,403.25 10,403.25 10,403.25 3,546.00 3,546.00 3,546.00 .00 .00 .00 13,949.25 13,949.25 13,949.25
2. XXXXXXXX XXXX - CANC XXX .00 .00 .00 .00 .00 .00 .00 .00 .00 .00 .00 .00
3. NET XXXXXXX PREMIUM 10,403.25 10,403.25 10,403.25 3,546.00 3,546.00 3,546.00 .00 .00 .00 13,949.25 13,949.25 13,949.25

B. XXXXXX XXXX 520.16 520.16 520.16 177.30 177.30 177.30 .00 .00 .00 697.46 697.46 697.46

C. XXXX XXXXX .00 .00 .00 .00 .00 .00 .00 .00 .00 .00 .00 .00

D. XXXXXXXXXX (A3-B-C)=D 9,883.09 9,883.09 9,883.09 3,368.70 3,368.70 3,368.70 .00 .00 .00 13,251.79 13,251.79 13,251.79

E. XXXXXXXXXXXXXXXXXXXXX
1. UNEARNED PREMIUM .00 .00 .00 .00 .00 .00 .00 .00 .00 .00 .00 .00
2. INCURRED BUT NOT REPORTED .00 .00 .00 .00 .00 .00 .00 .00 .00 .00 .00 .00
3. XXXXXXXXXXXXXXXXXXXXXX .00 .00 .00 .00 .00 .00 .00 .00 .00 .00 .00 .00

F. XXXXXXXXXXXXXXXXXXXXXXXXXXXX
1. UNEARNED PREMIUM 10,399.58 10,399.58 10,399.58 3,506.59 3,506.59 3,506.59 .00 .00 .00 13,906.17 13,906.17 13,906.17
2. INCURRED BUT NOT REPORTED 1.12 1.12 1.12 11.84 11.84 11.84 .00 .00 .00 12.96 12.96 12.96
3. ENDING UNEARNED PREMIUM AND IBNR 10,400.70 10,400.70 10,400.70 3,518.43 3,518.43 3,518.43 .00 .00 .00 13,919.13 13,919.13 13,919.13

G. AMORTIZED CEDING FEE
1. XXXXXXXXXXXXXXXXXXXXXXXX .00 .00 .00 .00 .00 .00 .00 .00 .00 .00 .00 .00
2. ENDING DEFERRED CEDING FEE 519.83 519.83 519.83 175.31 175.31 175.31 .00 .00 .00 695.14 1,214.97 695.14
3. XXXXXXXXX (B+G1-G2) .33 .33 .33 1.99 1.99 1.99 .00 .00 .00 2.32 2.32 2.32

H. XXXXXXXXXXXXXXXXXX/(XXXXX)(A3+E3-F3)-(C+G3)=H 2.22 2.22 2.22 25.58 25.58 25.58 .00 .00 .00 27.80 27.80 27.80

I. LOSS RATIOS
1. XXXXXXXX(C+F2-E2) 1.12 1.12 1.12 11.84 11.84 11.84 .00 .00 .00 12.96 12.96 12.96
2. EARNED PREMIUMS (A3+E3-F3) 2.55 2.55 2.55 27.57 27.57 27.57 .00 .00 .00 30.12 30.12 30.12

J. XXXXXXXXXX
1. XXXXXX 17 17 17 6 6 6 0 0 0 23 23 23
2.XXXXX 0 0 0 0 0 0 0 0 0 0 0 0
3. NET 17 17 17 6 6 6 0 0 0 23 23 23

"

Aspose.Pdf.Document pdf;
Aspose.Pdf.HtmlLoadOptions htmlLoadOptions = new Aspose.Pdf.HtmlLoadOptions();
MemoryStream outputstream = new MemoryStream();
htmlLoadOptions.InputEncoding = “UTF-8”;
htmlLoadOptions.PageInfo.Margin.Left = 0;
htmlLoadOptions.PageInfo.Margin.Right = 0;
htmlLoadOptions.PageInfo.Margin.Top = 0;
htmlLoadOptions.PageInfo.Margin.Bottom = 0;
htmlLoadOptions.PageInfo.IsLandscape = true;
htmlLoadOptions.PageInfo.DefaultTextState.FontSize= 7;

using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(pageSource)))
{
pdf = new Aspose.Pdf.Document(stream, htmlLoadOptions);

}

Aspose.Pdf.Facades.PdfFileEditor pfe = new Aspose.Pdf.Facades.PdfFileEditor();
pfe.ResizeContents(pdf, Aspose.Pdf.Facades.PdfFileEditor.ContentsResizeParameters.PageResize(17 * 72, 11 * 72));



pdf.Save(outputstream);
byte[] bytes = outputstream.GetBuffer();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader(“content-disposition”, string.Format(“attachment; filename={0}”, “xxx_List.pdf”));
HttpContext.Current.Response.ContentType = “application/pdf”;
HttpContext.Current.Response.BinaryWrite(bytes);
// render the htmlwriter into the response
HttpContext.Current.Response.End();

Hi,


Thank you for contacting support. Kindly send us your input HTML string. We will investigate and share our findings with you. Your response is awaited.

string pageSource="
REPORT NO. xxxxxxx Company Name RUN DATE 02/01/17 PAGE 1
PROG. ID xxxxxx XXXXX/XXXX XXX XXXXXXXXXXX XXXXXXX Statement RUN TIME 03:59:11
For Month Ending JAN 2017

XXXXXXXXXXX Company BCAG XXXXXXXXXXX COMPANY, LTD.
XXXXXXXXXXX ID No. I0001
XXXXXX No. 207139/XXXXX UNIVERSE

XXXXX_XXXX CARE NEW XXX XXXXX_XXXX CARE USED XXX NON-XXXXX XXX Grand Total
Current Calendar Year Inception Current Calendar Year Inception Current Calendar Year Inception Current Calendar Year Inception
Month Todate Todate Month Todate Todate Month Todate Todate Month Todate Todate

A. PREMIUM
1. XXXX XXXXXXX 10,403.25 10,403.25 10,403.25 3,546.00 3,546.00 3,546.00 .00 .00 .00 13,949.25 13,949.25 13,949.25
2. XXXXXXXX XXXX - CANC XXX .00 .00 .00 .00 .00 .00 .00 .00 .00 .00 .00 .00
3. NET XXXXXXX PREMIUM 10,403.25 10,403.25 10,403.25 3,546.00 3,546.00 3,546.00 .00 .00 .00 13,949.25 13,949.25 13,949.25

B. XXXXXX XXXX 520.16 520.16 520.16 177.30 177.30 177.30 .00 .00 .00 697.46 697.46 697.46

C. XXXX XXXXX .00 .00 .00 .00 .00 .00 .00 .00 .00 .00 .00 .00

D. XXXXXXXXXX (A3-B-C)=D 9,883.09 9,883.09 9,883.09 3,368.70 3,368.70 3,368.70 .00 .00 .00 13,251.79 13,251.79 13,251.79

E. XXXXXXXXXXXXXXXXXXXXX
1. UNEARNED PREMIUM .00 .00 .00 .00 .00 .00 .00 .00 .00 .00 .00 .00
2. INCURRED BUT NOT REPORTED .00 .00 .00 .00 .00 .00 .00 .00 .00 .00 .00 .00
3. XXXXXXXXXXXXXXXXXXXXXX .00 .00 .00 .00 .00 .00 .00 .00 .00 .00 .00 .00

F. XXXXXXXXXXXXXXXXXXXXXXXXXXXX
1. UNEARNED PREMIUM 10,399.58 10,399.58 10,399.58 3,506.59 3,506.59 3,506.59 .00 .00 .00 13,906.17 13,906.17 13,906.17
2. INCURRED BUT NOT REPORTED 1.12 1.12 1.12 11.84 11.84 11.84 .00 .00 .00 12.96 12.96 12.96
3. ENDING UNEARNED PREMIUM AND IBNR 10,400.70 10,400.70 10,400.70 3,518.43 3,518.43 3,518.43 .00 .00 .00 13,919.13 13,919.13 13,919.13

G. AMORTIZED CEDING FEE
1. XXXXXXXXXXXXXXXXXXXXXXXX .00 .00 .00 .00 .00 .00 .00 .00 .00 .00 .00 .00
2. ENDING DEFERRED CEDING FEE 519.83 519.83 519.83 175.31 175.31 175.31 .00 .00 .00 695.14 1,214.97 695.14
3. XXXXXXXXX (B+G1-G2) .33 .33 .33 1.99 1.99 1.99 .00 .00 .00 2.32 2.32 2.32

H. XXXXXXXXXXXXXXXXXX/(XXXXX)(A3+E3-F3)-(C+G3)=H 2.22 2.22 2.22 25.58 25.58 25.58 .00 .00 .00 27.80 27.80 27.80

I. LOSS RATIOS
1. XXXXXXXX(C+F2-E2) 1.12 1.12 1.12 11.84 11.84 11.84 .00 .00 .00 12.96 12.96 12.96
2. EARNED PREMIUMS (A3+E3-F3) 2.55 2.55 2.55 27.57 27.57 27.57 .00 .00 .00 30.12 30.12 30.12

J. XXXXXXXXXX
1. XXXXXX 17 17 17 6 6 6 0 0 0 23 23 23
2.XXXXX 0 0 0 0 0 0 0 0 0 0 0 0
3. NET 17 17 17 6 6 6 0 0 0 23 23 23

"



Thanks

Hi,


Thank you for the sending an HTML string. We have tested your scenario with the latest version 17.5 of Aspose.Pdf for .NET API and the modified code as narrated below. We have attached an output PDF to this reply. Please take a look over the output PDF and highlight the issues with the help of snapshots.

[.NET, C#]
<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>Aspose.Pdf.Document pdf;<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
Aspose.Pdf.HtmlLoadOptions htmlLoadOptions = <span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>new<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”> Aspose.Pdf.HtmlLoadOptions();<br style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”><span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>htmlLoadOptions.InputEncoding = “UTF-8”;<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
htmlLoadOptions.PageInfo.Margin.Left = 0;<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
htmlLoadOptions.PageInfo.Margin.Right = 0;<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
htmlLoadOptions.PageInfo.Margin.Top = 0;<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
htmlLoadOptions.PageInfo.Margin.Bottom = 0;<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
htmlLoadOptions.PageInfo.IsLandscape = <span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>true<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>;<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
htmlLoadOptions.PageInfo.DefaultTextState.FontSize = 7;<br style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”><br style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”><span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>string<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”> pageSource = File.ReadAllText(@“C:\Pdf\test79\InputHTML.html”);<br style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”><span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>using<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”> (MemoryStream stream = <span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>new<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”> MemoryStream(Encoding.UTF8.GetBytes(pageSource)))<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
{<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
pdf = <span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>new<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”> Aspose.Pdf.Document(stream, htmlLoadOptions);<br style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”><span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”> pdf.PageInfo.Height = 11 * 72;<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
pdf.PageInfo.Width = 17 * 72;<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
}<br style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”><span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>pdf.Save(@“C:\Pdf\test79\Output.pdf”);
<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>

Three Issues, once I print the document:

1) Font is always the same size once it is printed
2) Paper size should be legal ( see attached screenshot)
3) Margins left and right are not 0 once a document is printed



Hi,


Thank you for the details.
rongoal:
1) Font is always the same size once it is printed
When we render your source HTML in the browser, it shows all text with the same font size. However, it appears as you are looking to downgrade the font size up to 7 in the whole PDF. If so, then please modify your code as below:

[.NET, C#]
<span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>using<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”> (MemoryStream stream = <span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>new<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”> MemoryStream(Encoding.UTF8.GetBytes(pageSource)))<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
{<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
pdf = <span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>new<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”> Aspose.Pdf.Document(stream, htmlLoadOptions);<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
<span class=“rem” style=“color: rgb(0, 128, 0); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>// Set the page size as Legal (8.5 in x 14.0 in). In Aspose.Pdf, 1 inch = 72 points<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
pdf.Pages[1].SetPageSize(612, 1008);<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>

<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”> <span class=“rem” style=“color: rgb(0, 128, 0); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>// Search text fragments <span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>
TextFragmentAbsorber absorber = <span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>new<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”> TextFragmentAbsorber();<br style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”><span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”> <span class=“rem” style=“color: rgb(0, 128, 0); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>//accept the absorber for all the pages<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>
pdf.Pages.Accept(absorber);<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>
<span class=“rem” style=“color: rgb(0, 128, 0); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>// traverse through all the TextFragments<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>
<span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>foreach<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”> (TextFragment textFragment <span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>in<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”> absorber.TextFragments)<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>
textFragment.TextState.FontSize = 7;<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>
<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>}
rongoal:
Paper size should be legal ( see attached screenshot)
Aspose.Pdf for .NET API is changing the page size, however the advance print properties are showing the page size as the Letter. We have logged this issue under the ticket ID PDFNET-42865 in our bug tracking system. We have linked your post to this ticket and will keep you informed regarding any available updates.
rongoal:
Margins left and right are not 0 once a document is printed
You can observe zero margins in the output PDF which we shared in our earlier post. It appears as the margins are being applied through the printer settings. Please make sure that the margins under the page setup are also zero.

once I updated my code to


Aspose.Pdf.Document pdf;
Aspose.Pdf.HtmlLoadOptions htmlLoadOptions = new Aspose.Pdf.HtmlLoadOptions();
MemoryStream outputstream = new MemoryStream();
htmlLoadOptions.InputEncoding = “UTF-8”;
htmlLoadOptions.PageInfo.Margin.Left = 0;
htmlLoadOptions.PageInfo.Margin.Right = 0;
htmlLoadOptions.PageInfo.Margin.Top = 0;
htmlLoadOptions.PageInfo.Margin.Bottom = 0;
htmlLoadOptions.PageInfo.IsLandscape = true;
htmlLoadOptions.PageInfo.DefaultTextState.FontSize= 7;

using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(pageSource)))
{
pdf = new Aspose.Pdf.Document(stream, htmlLoadOptions);
// Search text fragments
pdf.Pages[1].SetPageSize(612, 1008);
Aspose.Pdf.Text.TextFragmentAbsorber absorber = new Aspose.Pdf.Text.TextFragmentAbsorber();
//accept the absorber for all the pages
pdf.Pages.Accept(absorber);
// traverse through all the TextFragments
foreach (Aspose.Pdf.Text.TextFragment textFragment in absorber.TextFragments)
textFragment.TextState.FontSize = 7;

}

pdf.Save(outputstream);
byte[] bytes = outputstream.GetBuffer();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader(“content-disposition”, string.Format(“attachment; filename={0}”, “List.pdf”));
HttpContext.Current.Response.ContentType = “application/pdf”;
HttpContext.Current.Response.BinaryWrite(bytes);
// render the htmlwriter into the response
HttpContext.Current.Response.End();

I still have the following issues:

1) pdf.Pages[1].SetPageSize(612, 1008); does not work, document still not printing legal size. (it print letter size) and I need it for all pages not just one page

2) Yes margins are showing 0 , however, once I select a printer the margins are overwritten to 1 , see screenshot

3) foreach (Aspose.Pdf.Text.TextFragment textFragment in absorber.TextFragments)
textFragment.TextState.FontSize = 7; is working and font is changing , however, loop takes more than 4 minutes to finish which is too long for the user to wait. is there more efficient and faster way?





Hi,


Thank you for the details. We are testing your scenario in our environment and will get back to you soon.

Hi,


Thank you for the details.
rongoal:
1) pdf.Pages[1].SetPageSize(612, 1008); does not work, document still not printing legal size. (it print letter size) and I need it for all pages not just one page
You are right, we have added this information to the ticket ID PDFNET-42865 in our bug tracking system.
rongoal:
2) Yes margins are showing 0 , however, once I select a printer the margins are overwritten to 1 , see screenshot
In the print dialog box, please select “Actual size” under “Size Options.” Click the “Page Setup” button. If your printer supports borderless printing, the Margins section of the dialog box will be editable, change each margin setting to “0” and click “OK.”
rongoal:
3) foreach (Aspose.Pdf.Text.TextFragment textFragment in absorber.TextFragments)
textFragment.TextState.FontSize = 7; is working and font is changing , however, loop takes more than 4 minutes to finish which is too long for the user to wait. is there more efficient and faster way?
It is taking one and half minute in our environment. We have logged an enhancement to improve the performance under the ticket ID PDFNET-42875 in our bug tracking system. We have linked your post to this ticket and will keep you informed regarding any available updates.

Thanks Imran, Considering the tight timelines we have what is the work around? Do you think creating a word doc template then converting the template to PDF will resolve the issue? If yes, can you help as we already wasted more than three weeks trying to find a solution for it?(went through many docs before I even posted my issue) I really need a solution ASAP as this is for a critical project we have?


Key thing for us

Legal size paper without it the whole project won’t work. also performance is important, since this is a web application one and a half minute is a very long time for users.

Thanks

Hi,


Thank you for the inquiry. You can load an HTML to a Document class instance of Aspose.Words API as well as set the page margins by using DocumentBuilder.PageSetup property. You can also iterate through the Run nodes to downgrade the font of text items with the code example below. We have attached an output PDF to this reply. Kindly download and try the latest version 17.6 of Aspose.Words for .NET API.

[.NET, C#]
<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>Aspose.Words.Document doc = <span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>new<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”> Aspose.Words.Document(@“C:\Pdf\test79\InputHTML.html”);
<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>DocumentBuilder builder = <span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>new<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”> DocumentBuilder(doc);<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
PageSetup pageSetup = builder.PageSetup;<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
pageSetup.LeftMargin = 0;<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
pageSetup.RightMargin = 0;<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
pageSetup.TopMargin = 0;<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
pageSetup.BottomMargin = 0;<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
pageSetup.PaperSize = PaperSize.Legal;
<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>Paragraph paragraph = (Paragraph)doc.GetChild(NodeType.Paragraph, 0, <span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>true<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>);<br style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”><span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>NodeCollection children = paragraph.ChildNodes;<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
<span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>foreach<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”> (Node child <span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>in<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”> children)<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
{<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
<span class=“rem” style=“color: rgb(0, 128, 0); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>// Paragraph may contain children of various types such as runs, shapes and so on.<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
<span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>if<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”> (child.NodeType.Equals(NodeType.Run))<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
{<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
<span class=“rem” style=“color: rgb(0, 128, 0); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>// Say we found the node that we want, do something useful.<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
Run run = (Run)child;<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
run.Font.Size = 7;<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
}<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
}

Same issue Imran,When I print the document you have attached, it does not come out with Legal size paper. I still don’t understand why Legal size paper gets overwritten to Letter size when print?

Also when you print the document you have attached does it come out legal size?

Hi there,

Thanks for your inquiry.

rongoal:

  1. ignoring margins when printing

  2. ignoring font size

  3. need to set to legal size paper

Please note that Aspose.Words mimics the same behavior as MS Word does. If you open the HTML document in MS Word, set the paper size and margins and print it, you will get the same output. Please use the following code example to change the paper size and margins and print it.

Document doc = new Document(MyDir + @"input.html");

foreach (Section section in doc.Sections)

{

    PageSetup pageSetup = section.PageSetup;

    pageSetup.LeftMargin = 0;

    pageSetup.RightMargin = 0;

    pageSetup.TopMargin = 0;

    pageSetup.BottomMargin = 0;

    pageSetup.PaperSize = PaperSize.Legal;

}

doc.UpdatePageLayout();

doc.Print();

//Save the document to PDF

doc.Save(MyDir + @"output.pdf", SaveFormat.Pdf);

rongoal:

Same issue Imran,When I print the document you have attached, it does not come out with Legal size paper. I still don’t understand why Legal size paper gets overwritten to Letter size when print?

Also when you print the document you have attached does it come out legal size?

Your query is related to PDF viewer. Please note that you can not change the PDF viewer’s default settings of printer using Aspose.Words. This setting is related to PDF viewer. If you convert the HTML document to PDF using MS Word and print it using PDF viewer, you will get the same output.

Moreover, you can print the document after changing the paper size and margin using Aspose.Words. Document.Print prints the whole document to the default printer.

Thanks Tahir,

Based on Imran’s pervious comments Legal size does not work with PDF and he created a ticket to fix this bug. So we went through the word doc option to see if I can get it to print it in legal size. Based on the code you have provided, It was able to automatically set paper size to Legal, which is good. However, once this line is executed doc.Save(MyDir + @“output.pdf”, SaveFormat.Pdf); the document is no longer in legal size.

My questions is can I do something like this (PDF) without the word doc rout.

This code is not completly working due to some errors that I can’t figure out

Aspose.Pdf.Document doc;

Aspose.Pdf.HtmlLoadOptions htmlLoadOptions = new HtmlLoadOptions();

MemoryStream outputstream = new MemoryStream();

htmlLoadOptions.InputEncoding = “UTF - 8”;

htmlLoadOptions.PageInfo.Margin.Left = 0;

htmlLoadOptions.PageInfo.Margin.Right = 0;

htmlLoadOptions.PageInfo.Margin.Top = 0;

htmlLoadOptions.PageInfo.Margin.Bottom = 0;

htmlLoadOptions.PageInfo.IsLandscape = true;

htmlLoadOptions.PageInfo.DefaultTextState.FontSize = 7;

using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(pageSource)))

{

    doc = new Aspose.Pdf.Document(stream, htmlLoadOptions);

    foreach (Aspose.Pdf.Generator.Section section in doc.)

    {

        pageSetup = section.PageSetup;

        pageSetup.Margin.Left = 0;

        pageSetup.Margin.Right = 0;

        pageSetup.Margin.Top = 0;

        pageSetup.Margin.Bottom = 0;

        pageSetup.p = PaperSize.Legal;

    }

    doc.UpdatePageLayout();

    // doc.Print();

    //Save the document to PDF

    doc.Save(“C:\xxx.docx”);
}

also changing the font size is very important

Hi,


Thank you for the inquiry. Aspose.Pdf.Generator namespace is an old legacy approach and we recommend our clients to migrate from Aspose.Pdf.Generator approach to Aspose.Pdf for .NET DOM approach. Kindly see developer’s guide section about the DOM approach: Working with Aspose.Pdf

You are mixing code of two different approaches. We have already logged a ticket PDFNET-42865 to investigate and fix the problem of page size. The Pdf document properties show the page size as legal, but the print properties shows the page size as letter. Please be patient and spare us a little time. We will let you know when a significant progress has been made in this regard.

Hi there,

Thanks for your inquiry.
rongoal:
Based on the code you have provided, It was able to automatically set paper size to Legal, which is good. However, once this line is executed
As per my understanding, you are printing the PDF using PDF viewer and checking the paper size in PDF viewer's printer settings. If this is the case, this issue is not related to Aspose.Words.

Could you please share how are you checking the paper size of PDF? We will then provide you more information about your query.

Thanks Tahir
No, I dont use PDF Viewer settings. I just print the document using the last code you sent me. If you just print it as word document it prints legal size, If i convert the word doc to pdf then print , it does not print legal size. I go to the PDF Settings just to see if it sat paper size to legal, which it does not. Also if I just generate PDF without conversion (see Imran's explanation) Legal size ignored.

Hi there,

Thanks for your inquiry. We have tested the scenario using latest version of Aspose.Words for .NET 17.6 with following code example and have not found the shared issue. The output PDF have paper size legal. Please check the attached image for detail. We have attached the output PDF with this post for your kind reference.

rongoal:

If i convert the word doc to pdf then print , it does not print legal size. I go to the PDF Settings just to see if it sat paper size to legal, which it does not.

Legal size ignored.

Could you please share the steps how are you checking the “PDF Settings”?

Document doc = new Document(MyDir + @“input.html”);

foreach (Section section in doc.Sections)

{

    PageSetup pageSetup = section.PageSetup;

    pageSetup.LeftMargin = 0;

    pageSetup.RightMargin = 0;

    pageSetup.TopMargin = 0;

    pageSetup.BottomMargin = 0;

    pageSetup.PaperSize = PaperSize.Legal;

}

doc.UpdatePageLayout();

doc.Print();

//Save the document to PDF

doc.Save(MyDir + @“output.pdf”, SaveFormat.Pdf);

@rongoal

Initial investigation shows that most of time consumed by font size changing operation. We have already implemented function for changing font size of all text fragments on the page at once. It works faster.

Please consider the following code:

Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
Aspose.Pdf.Document pdf;
Aspose.Pdf.HtmlLoadOptions htmlLoadOptions = new Aspose.Pdf.HtmlLoadOptions();
htmlLoadOptions.InputEncoding = "UTF-8";
htmlLoadOptions.PageInfo.Margin.Left = 0;
htmlLoadOptions.PageInfo.Margin.Right = 0;
htmlLoadOptions.PageInfo.Margin.Top = 0;
htmlLoadOptions.PageInfo.Margin.Bottom = 0;
htmlLoadOptions.PageInfo.IsLandscape = true;
htmlLoadOptions.PageInfo.DefaultTextState.FontSize = 7;

string pageSource = File.ReadAllText(dataDir + @"InputHTML.html");
using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(pageSource)))
{
    pdf = new Aspose.Pdf.Document(stream, htmlLoadOptions);
    // Set the page size as Legal (8.5 in x 14.0 in). In Aspose.Pdf, 1 inch = 72 points
    pdf.Pages[1].SetPageSize(612, 1008);

    // Search text fragments and set edit option as remove unused fonts
    TextFragmentAbsorber absorber = new TextFragmentAbsorber();

    //accept the absorber for all the pages
    pdf.Pages.Accept(absorber);

    //use mass operation instead of looping through the TextFragments
    absorber.ApplyForAllFragments(7);
}
pdf.Save(dataDir + @"42875_mass_out_20.01.pdf");
stopWatch.Stop();
// Get the elapsed time as a TimeSpan value.
TimeSpan ts = stopWatch.Elapsed;

// Format and display the TimeSpan value.
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
    ts.Hours, ts.Minutes, ts.Seconds,
    ts.Milliseconds / 10);
Console.WriteLine("RunTime " + elapsedTime);

On our observation it decreases processing time from 36 to 23 seconds on our test system. Please use suggested code snippet with Aspose.PDF for .NET 20.2 and in case you face any issue, please feel free to let us know.