Gk_*_*999 4 converter pdfsharp
我想使用 PDFsharp 将文本文件转换为 PDF。应该采取什么方法?甚至有可能吗?我正在使用 C#.net 开发 Web 应用程序
方法是检查 PDFsharp 和 MigraDoc 的样本,然后决定使用哪个工具。
如果文本可能需要不止一页,我猜想 MigraDoc 将是更好的选择。
另见:http :
//pdfsharp.net/wiki/MigraDocSamples.ashx
小智 5
我为此写了一段代码。
最初,我为此使用 pdfsharp dll,但这对我不起作用,因为 pdfsharp 无法感知分页符,当我编写代码时,我看到只打印适合第一页的分页符。
然后我了解到 Migradoc 确实可以感知分页符并在需要时自动添加新页面。
这是我的方法,有两个参数:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using MigraDoc.DocumentObjectModel;
using MigraDoc.Rendering;
using System.IO;
void CreatePDFFileFromTxtFile(string textfilefullpath, string pdfsavefullpath)
{
//first read text to end add to a string list.
List<string> textFileLines = new List<string>();
using (StreamReader sr = new StreamReader(textfilefullpath))
{
while (!sr.EndOfStream)
{
textFileLines.Add(sr.ReadLine());
}
}
Document doc = new Document();
Section section = doc.AddSection();
//just font arrangements as you wish
MigraDoc.DocumentObjectModel.Font font = new Font("Times New Roman", 15);
font.Bold = true;
//add each line to pdf
foreach (string line in textFileLines)
{
Paragraph paragraph = section.AddParagraph();
paragraph.AddFormattedText(line,font);
}
//save pdf document
PdfDocumentRenderer renderer = new PdfDocumentRenderer();
renderer.Document = doc;
renderer.RenderDocument();
renderer.Save(pdfsavefullpath);
}
Run Code Online (Sandbox Code Playgroud)
并使用输入文本完整路径和输出 pdf 文件完整路径调用此方法来创建。
这有效。
| 归档时间: |
|
| 查看次数: |
6018 次 |
| 最近记录: |