(C#Windows窗体)使用Microsoft Print to PDF将文件转换并保存为PDF

时间:2018-08-14 07:04:16

标签: c# pdf printing

我用C#做了一个Windows窗体,关于使用Microsoft Print to PDF将文件转换为PDF。我已经提到过许多编码,其中之一来自Unable to open the PDF, which was generated using print to pdf code written in C#

以下是我进行的一些小更改。

USD

但是,根据我所做的(上面的代码),我成功地将文件转换为pdf,但无法打开。在更改上面的代码之前,我做了以下事情:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//using Microsoft.Office.Interop.Word;
//using Microsoft.Office.Interop.Excel;
//using Spire.Pdf;

namespace fileConversion
{
    public partial class Form1 : Form
    {
        private System.Drawing.Font printFont;
        private StreamReader streamToPrint;

        public Form1()
        {
            InitializeComponent();
        }

        private void selectFile_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                label1.Text = openFileDialog1.FileName; //text label1 tu pegang file name yang kita select
            }
        }

        private void convertFile_Click(object sender, EventArgs e)
        {
             string FileName = label1.Text;
             // the directory to store the output.
             //string directoryPath = System.IO.Path.GetDirectoryName(FileName);

            // initialize PrintDocument object
            PrintDocument doc = new PrintDocument()
             {
                PrinterSettings = new PrinterSettings()
                {
                   // set the printer to 'Microsoft Print to PDF'
                   PrinterName = "Microsoft Print to PDF",

                    // set the filename to whatever you like (full path)
                    PrintFileName = System.IO.Path.GetFullPath(@"" + FileName + ".pdf"),

                    // tell the object this document will print to file
                    PrintToFile = true,
                }
             };

             doc.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
             doc.Print();
        }

        private void pd_PrintPage(object sender, PrintPageEventArgs ev)
        {
            float linesPerPage = 0;
            float yPos = 0;
            int count = 0;
            float leftMargin = ev.MarginBounds.Left;
            float topMargin = ev.MarginBounds.Top;
            string line = File.ReadAllText(label1.Text);

           // Calculate the number of lines per page.
           linesPerPage = ev.MarginBounds.Height /
              printFont.GetHeight(ev.Graphics);

            // Print each line of the file.
            while ((line = streamToPrint.ReadLine()) != null)
            {
                yPos = topMargin + (count *
                   printFont.GetHeight(ev.Graphics));
                ev.Graphics.DrawString(line, printFont, Brushes.Black,
                   leftMargin, yPos, new StringFormat());
                count++;
            }

            // If more lines exist, print another page.
            if (line != null)
                ev.HasMorePages = true;
            else
                ev.HasMorePages = false;
        }
    }
}

尽管从文件到pdf的转换成功,但是不显示pdf文件中的内容。只有空白的pdf文件。

,这是我更正后的新代码。

private void convertFile_Click(object sender, EventArgs e)
        {
             string FileName = label1.Text;
             // the directory to store the output.
             //string directoryPath = System.IO.Path.GetDirectoryName(FileName);

            // initialize PrintDocument object
            PrintDocument doc = new PrintDocument()
             {
                PrinterSettings = new PrinterSettings()
                {
                   // set the printer to 'Microsoft Print to PDF'
                   PrinterName = "Microsoft Print to PDF",

                    // set the filename to whatever you like (full path)
                    PrintFileName = System.IO.Path.GetFullPath(@"" + FileName + ".pdf"),

                    // tell the object this document will print to file
                    PrintToFile = true,
                }
             };

             doc.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
             doc.Print();
        }

0 个答案:

没有答案