简体   繁体   中英

bootstrap css file makes smaller my document when i convert html to PDF with itextsharp in c# Winforms

I am trying to build a customized PDF export form with HTML and bootstrap to use in any c# project. I want to use bootstrap designs to make my custom PDF designs. There is no problem with my own custom css file. It is working fine. But when I add the css file of bootstrap, it makes my document zoom out and be much smaller too. I cannot figure out how to fix this. I just want to create an A4 paper size form in PDF and print it.

Here is what I get when I add the bootstrap css file: 在此处输入图片说明

using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.tool.xml;
using iTextSharp.tool.xml.html;
using iTextSharp.tool.xml.parser;
using iTextSharp.tool.xml.pipeline.css;
using iTextSharp.tool.xml.pipeline.end;
using iTextSharp.tool.xml.pipeline.html;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace pdf
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            byte[] result;
            Document documment = new Document(PageSize.A4);
            string bootstrapCssFilePath = @"C:/Users/sea_h/Desktop/cop/fulhtml/pdf/bootstrap.min.css";
            string customCssFilePath = @"C:/Users/sea_h/Desktop/cop/fulhtml/pdf/custom.css";
            string htmlText = @"
                <html>
                    <head>
                    </head>
                    <body>
                        <div class='container'>
                        <col-sm4>
                            <h1>Deneme H1</h1>
                        </col-sm4>
                        <col-sm4>
                            <h2>deneme h2</h2>
                        </col-sm4>
                        <col-sm4>
                            <h7>deneme h7</h7>
                        </col-sm4>
                        </div>
                    </body>
                </html>";            
            using (var ms=new MemoryStream())
            {
                PdfWriter writer = PdfWriter.GetInstance(documment, ms);
                writer.CloseStream = false;
                documment.Open();
                HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
                htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());

                ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false);
                cssResolver.AddCssFile(bootstrapCssFilePath, true);
                cssResolver.AddCssFile(customCssFilePath, true);
                IPipeline pipeLine = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(documment, writer)));

                XMLWorker worker = new XMLWorker(pipeLine, true);
                XMLParser xmlParser = new XMLParser(worker);
                xmlParser.Parse(new MemoryStream(Encoding.UTF8.GetBytes(htmlText)));
                documment.Close();
                result = ms.GetBuffer();
                string dest = @"C:\Users\sea_h\Desktop\deneme016.pdf";

                System.IO.File.WriteAllBytes(dest, result);
            }
        }
    }
}

My custom css is:

    h1{
    background-color:red;
    }    

There is no error message.

So i just figure out why this is happenning. Bootstrap css file using rem units for sizing. But itextsharp using 300ppi resulotion whic means A4 paper have 2480 px X 3508 px resulotion in 300ppi.And bootstrap sizing is so small for this resulotion.

So i can modify bootstrap css file with new higher sizes as manually to fix this problem. Or if it is possible, i can try the set itextsharp paper ppi for lower as like 70ppi.

I think there is no clear solution for this problem.

First you need a button or something to trigger the print job, then toss on some code like this, essentially this is just going pop a print menu and go ahead with print job when user hits submit (returns 1)

In the printImage method you are going to find the declarations for fonts etc you intend to use. I'm sure there are other ways, but I use rectangles to place my draw strings where I need them. tempPoint.X and tempPoint.Y are followed by rect.location = tempPoint, this allows you to move the rectangle around as needed and keep tracking of coordinates as you go. e.graphics.drawstring() is what actually writes the text, for more specifics I would go ahead and look up some further information. From this you can just keep replicating the tempPoint movement, rect location assignment, and drawstring to customize where things are placed in your print form. As far as turning it into a pdf, windows comes with tools that are in the print menu to automate that part of it all.

    private void Button1_Click(object sender, EventArgs e)
    {
        PrintDocument pd = new PrintDocument();
        pd.PrintPage += new PrintPageEventHandler(PrintImage);
        PrintDialog printdlg = new PrintDialog();

        /*preview the assigned document or you can create a different previewButton for it
        printPrvDlg.Document = pd;
        printPrvDlg.ShowDialog(); // this shows the preview and then show the Printer Dlg below
        */
        printdlg.Document = pd;

        if (printdlg.ShowDialog() == DialogResult.OK)
        {
            pd.Print();
        }
    }

void PrintImage(object o, PrintPageEventArgs e)
{
    const int ORIGIN = 150;
    var near = new StringFormat() { Alignment = StringAlignment.Near };
    var centered = new StringFormat() { Alignment = StringAlignment.Center };
    var far = new StringFormat() { Alignment = StringAlignment.Far };
    Point tempPoint = new Point();
    var rect = new RectangleF(0, 0, 0, 0);
    var headingRect = new RectangleF(0, 0, 0, 0);
    // Create font and brush.
    Font titleDrawFont = new Font("Times New Roman", 16, FontStyle.Bold);
    Font subtitleDrawFont = new Font("Times New Roman", 12);
    Font drawFont = new Font("Times New Roman", 8);
    SolidBrush drawBrush = new SolidBrush(Color.Black);
    Pen blackPen = new Pen(Color.Black, 1);

    // Draw string to screen.          
    //***************************************************************

    Image logo = Image.FromFile(imageLoc);
    e.Graphics.DrawImage(logo, (e.PageBounds.Width - logo.Width) / 2,
                                10, logo.Width, logo.Height); //Created Centered Image in original size
    rect.Width = 150;
    rect.Height = 20;
    headingRect.Width = e.PageBounds.Width;
    headingRect.Height = 40; //Set to 40 to avoid cut off with larger title text
    tempPoint.X = 0;
    tempPoint.Y = 110;
    headingRect.Location = tempPoint;
    e.Graphics.DrawString(lblTitle.Text, titleDrawFont, drawBrush, headingRect, centered);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM