gpt4 book ai didi

c# - 无法打开使用 C# 编写的 print to pdf 代码生成的 PDF

转载 作者:太空宇宙 更新时间:2023-11-03 14:54:02 37 4
gpt4 key购买 nike

我使用 C# 使用 Microsoft Print to PDF 打印机将文件打印为 PDF。文件生成成功。但是我无法打开它,因为 Adob​​e 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 文件。这个,我可以用 Adob​​e 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:我引用了How to programmatically print to PDF file without prompting for filename in C# using the Microsoft Print To PDF printer that comes with Windows 10获取代码。

最佳答案

这是我的代码,运行正常。希望对您有所帮助。

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

关于c# - 无法打开使用 C# 编写的 print to pdf 代码生成的 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50586880/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com