当我在 c# Winforms 中使用 itextsharp 将 html 转换为 PDF 时,bootstrap css 文件使我的文档变小

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

我正在尝试使用 HTML 和 bootstrap 构建自定义 PDF 导出表单以在任何 c# 项目中使用。我想使用 bootstrap 设计来制作我的自定义 PDF 设计。我自己自定义的css文件没有问题。它工作正常。但是当我添加 bootstrap 的 css 文件时,它使我的文档缩小并且也小得多。我不知道如何解决这个问题。我只想创建一个 A4 纸大小的 PDF 表格并打印它。

这是我添加 bootstrap css 文件时得到的结果:

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);
            }
        }
    }
}

我的习惯css是:

    h1{
    background-color:red;
    }    

没有错误信息。

所以我只是弄清楚为什么会这样。 Bootstrap css 文件使用 rem 单位调整大小。 但是 itextsharp 使用 300ppi 分辨率,这意味着 A4 纸在 300ppi.And bootstrap 中有 2480 px X 3508 px 分辨率对于这个分辨率来说尺寸太小了。

所以我可以手动修改 bootstrap css 文件以解决这个问题。 或者,如果可能的话,我可以尝试设置 itextsharp paper ppi 以降低 70ppi。

我认为这个问题没有明确的解决方案。

首先你需要一个按钮或其他东西来触发打印作业,然后扔一些这样的代码,本质上这只是弹出一个打印菜单并在用户点击提交时继续打印作业(returns 1)

在 printImage 方法中,您将找到您打算使用的字体等的声明。我确信还有其他方法,但我使用矩形将我的绘图字符串放在我需要的地方。 tempPoint.X 和 tempPoint.Y 后跟 rect.location = tempPoint,这允许您根据需要四处移动矩形并在移动时跟踪坐标。 e.graphics.drawstring() 是实际编写文本的内容,有关更多细节,我会继续查找更多信息。由此,您可以继续复制 tempPoint 移动、矩形位置分配和拉绳,以自定义在打印表单中放置内容的位置。至于将其转换为 pdf,windows 带有打印菜单中的工具,可以自动执行这部分操作。

    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);