Невозможно открыть PDF-файл, созданный с использованием кода print to pdf, написанного на C # - PullRequest
0 голосов
/ 29 мая 2018

Я использовал C # для печати файла в PDF, используя принтер Microsoft Print to PDF.Файл был успешно создан.Но я не могу открыть это, потому что Adobe Reader говорит, что файл поврежден.Это код

PrintDocument pd = new PrintDocument
{
    PrinterSettings = new PrinterSettings
    {
        PrinterName = "Microsoft Print to PDF (redirected 2)",
        PrintToFile = true,
        PrintFileName = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/test.pdf"
    }
};
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
pd.Print();

Но если я использую тот же код без PrinterSettings, он запрашивает место назначения и имя файла.Если я укажу оба, то он создаст файл PDF.Это я могу открыть с помощью Adobe Reader.Код показан ниже

PrintDocument pd = new PrintDocument(); 
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
pd.Print();

Не уверен, что мне не хватает в первом подходе.Пожалуйста помоги.Приведенная ниже часть является реализацией для pd_PrintPage

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 = null;

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

    // Iterate over the file, printing each line.
    while (count < linesPerPage &&
       ((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;
}

PS: я ссылался на Как программно печатать в файл PDF без запроса имени файла в C # с помощью принтера Microsoft Print To PDF, который поставляется с Windows 10 для кода.

1 Ответ

0 голосов
/ 31 мая 2018

Вот мой код, который работает нормально.Надеюсь, это поможет.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing.Printing;
using System.IO;
using System.Drawing;

namespace ConsoleApp1
{
    class Program
    {
        private static Font printFont;
        private static StreamReader streamToPrint;

        static void Main(string[] args)
        {
            // generate a file name as the current date/time in unix timestamp format
            string fileName = (string)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds.ToString();

            // the directory to store the output.
            string directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            printFont = new Font("Arial", 10);

            try
            {
                streamToPrint = new StreamReader
                    ("C:\\Users\\RaikolAmaro\\Desktop\\lachy.txt");

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

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

                        // set the filename to whatever you like (full path)
                        PrintFileName = Path.Combine(directory, fileName + ".pdf"),
                    }
                };

                doc.PrintPage += new PrintPageEventHandler(pd_PrintPage);
                doc.Print();
            }
            finally
            {
                streamToPrint.Close();
            }
        }

        private static 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 = null;

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

            // Iterate over the file, printing each line.
            while (count < linesPerPage &&
                   ((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;
        }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...