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

当我在c#Winforms中使用itextsharp将html转换为PDF时,引导css文件使我的文档变小,c#,html,twitter-bootstrap,pdf,itext,C#,Html,Twitter Bootstrap,Pdf,Itext,我正在尝试用HTML和bootstrap构建一个定制的PDF导出表单,以便在任何c#项目中使用。我想使用引导设计,使我的自定义PDF设计。我自己的自定义css文件没有问题。它工作得很好。但是,当我添加bootstrap的css文件时,它使我的文档缩小,并且变得更小。我想不出怎么解决这个问题。我只想创建一个A4纸大小的PDF格式的表单并打印出来 以下是添加引导css文件时得到的结果: 没有错误消息。所以我只是想知道为什么会发生这种情况。 使用rem单位调整大小的引导css文件。 但itextsh

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

以下是添加引导css文件时得到的结果:


没有错误消息。

所以我只是想知道为什么会发生这种情况。 使用rem单位调整大小的引导css文件。 但itextsharp使用300ppi结果,这意味着A4纸的300ppi结果为2480px 3508px,而引导大小对于这个结果来说太小了

因此,我可以手动修改引导css文件的新的更大的大小来解决这个问题。 或者,如果可能的话,我可以尝试将itextsharp纸张ppi设置为70ppi


我认为这个问题没有明确的解决办法。

所以我只是想知道为什么会发生这种情况。 使用rem单位调整大小的引导css文件。 但itextsharp使用300ppi结果,这意味着A4纸的300ppi结果为2480px 3508px,而引导大小对于这个结果来说太小了

因此,我可以手动修改引导css文件的新的更大的大小来解决这个问题。 或者,如果可能的话,我可以尝试将itextsharp纸张ppi设置为70ppi


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

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

在printImage方法中,您将找到要使用的字体等的声明。我肯定还有其他的方法,但是我用矩形把我的画线放在我需要的地方。tempPoint.X和tempPoint.Y后面紧跟着rect.location=tempPoint,这允许您根据需要移动矩形,并在移动过程中跟踪坐标。e、 graphics.drawstring()实际上是编写文本的,有关更多细节,我将继续查找更多信息。从这里,您可以继续复制tempPoint移动、rect位置分配和drawstring,以自定义在打印表单中放置内容的位置。至于将其转换为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);

首先,您需要一个按钮或一些东西来触发打印作业,然后抛出一些类似这样的代码,本质上这只是弹出一个打印菜单,当用户点击提交时继续打印作业(返回1)

在printImage方法中,您将找到要使用的字体等的声明。我肯定还有其他的方法,但是我用矩形把我的画线放在我需要的地方。tempPoint.X和tempPoint.Y后面紧跟着rect.location=tempPoint,这允许您根据需要移动矩形,并在移动过程中跟踪坐标。e、 graphics.drawstring()实际上是编写文本的,有关更多细节,我将继续查找更多信息。从这里,您可以继续复制tempPoint移动、rect位置分配和drawstring,以自定义在打印表单中放置内容的位置。至于将其转换为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);

我个人没有使用过Itextsharp,但每当我需要从winforms导出pdf时,我都会创建一个打印对象,并使用drawstring()和其他所需工具手动创建打印页面布局。否则,它将采用用于在屏幕上显示的分辨率,与打印页面相比,该分辨率实际上非常小。如果您决定走这条路,我可以提供一些示例代码,说明我是如何做到这一点的。@SomeNerdAtWork我希望这样做。我可以从您那里学到一些东西。我个人没有使用过Itextsharp,但每当我需要从winforms导出pdf时,我都会创建一个打印对象,并使用drawstring()手动创建打印页面布局以及所需的任何其他工具。否则,它将采用用于在屏幕上显示的分辨率,与打印页面相比,该分辨率实际上非常小。如果你决定走这条路,我可以提供一些示例代码,说明我是如何做到这一点的。@SomeNerdAtWork我想这样做。我可以从你那里学到一些东西。