我想通过将HTML内容传递给函数来生成PDF.我已经为此使用了iTextSharp,但是当它遇到表格并且布局变得混乱时它表现不佳.
有没有更好的办法?
我有这个iTextSharp的演示代码
Document document = new Document();
try
{
PdfWriter.GetInstance(document, new FileStream("Chap0101.pdf", FileMode.Create));
document.Open();
document.Add(new Paragraph("Hello World"));
}
catch (DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch (IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
document.Close();
Run Code Online (Sandbox Code Playgroud)
如何让控制器将pdf文档返回到浏览器?
编辑:
运行此代码确实打开Acrobat但我收到一条错误消息"文件已损坏且无法修复"
public FileStreamResult pdf()
{
MemoryStream m = new MemoryStream();
Document document = new Document();
PdfWriter.GetInstance(document, m);
document.Open();
document.Add(new Paragraph("Hello World"));
document.Add(new Paragraph(DateTime.Now.ToString()));
m.Position = 0;
return File(m, "application/pdf");
}
Run Code Online (Sandbox Code Playgroud)
任何想法为什么这不起作用?
我在这里问过几个问题,但我仍然遇到问题.如果你能在我的代码中告诉我我做错了什么,我将不胜感激.我从ASP.Net页面运行上面的代码并获得"无法访问封闭的流".
var doc = new Document();
MemoryStream memoryStream = new MemoryStream();
PdfWriter.GetInstance(doc, memoryStream);
doc.Open();
doc.Add(new Paragraph("First Paragraph"));
doc.Add(new Paragraph("Second Paragraph"));
doc.Close(); //if I remove this line the email attachment is sent but with 0 bytes
MailMessage mm = new MailMessage("username@gmail.com", "username@gmail.com")
{
Subject = "subject",
IsBodyHtml = true,
Body = "body"
};
mm.Attachments.Add(new Attachment(memoryStream, "test.pdf"));
SmtpClient smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
Credentials = new NetworkCredential("username@gmail.com", "my_password")
};
smtp.Send(mm); //the "Cannot Access …
Run Code Online (Sandbox Code Playgroud) 如何使用带有Pdfreader类的itextsharp读取PDF内容.我的PDF可能包含纯文本或文本图像.
我想使用iTextSharp将以下HTML转换为PDF,但不知道从哪里开始:
<style>
.headline{font-size:200%}
</style>
<p>
This <em>is </em>
<span class="headline" style="text-decoration: underline;">some</span>
<strong>sample<em> text</em></strong>
<span style="color: red;">!!!</span>
</p>
Run Code Online (Sandbox Code Playgroud) 我想知道ITextSharp是否具有将HTML转换为PDF的能力.我将转换的所有内容都只是纯文本,但遗憾的是ITextSharp上几乎没有文档,所以我无法确定这对我来说是否是一个可行的解决方案.
如果它不能这样做,有人可以指向一些好的,免费的.net库,可以采用简单的纯文本HTML文档并将其转换为PDF格式吗?
TIA.
我有一个现有的PDF,我可以使用FdFWriter输入到文本框.它运作良好.现在我有了一张图片.我已阅读文档并查看了许多示例,但它们都创建了新文档并插入了图像.我想拍摄现有的PDF并将图像插入图像字段或按钮的图标图像.我试过但它破坏了文件.
我需要能够获取现有文档并在其上放置图像.我不想打开,阅读,替换和删除原始文件.此原始更改和名称"原始"仅表示此上下文中的源文件.像这样的许多PDF文件需要图像.
感谢您的任何帮助.
编辑 - 我非常感谢下面的代码.它工作得很好,但对我来说问题是现有的PDF上有数字签名.当像这样复制文档(到result.pdf)时,这些签名虽然仍然存在,但具有不同的字节数或其他已损坏的项.这意味着签名虽然出现在result.pdf上,但它们旁边有一个图标,表示"无效签名".
如果它很重要我使用Topaz签名板来创建我的签名,这有自己的安全性.仅仅复制PDF不会破坏它,但下面的过程将会.
我试图将图像放在现有文档上,而不是它的副本,在这种情况下很重要.
此外,签名,我的意思是手写,而不是密码.
再次感谢你.
编辑 - 可以使用PdfSignatureAppearance吗?
编辑 - 我似乎能够做到:
var stamper = new PdfStamper(reader,outputPdfStream,'1',true);
好吧,我正在尝试将多个PDF合并为一个.
编译时我没有错误.我试图首先合并文档但出错了因为我正在使用表格.
这是asp.net代码隐藏
if (Button.Equals("PreviewWord")) {
String eventTemplate = Server.MapPath("/ERAS/Badges/Template/EventTemp" + EventName + ".doc");
String SinglePreview = Server.MapPath("/ERAS/Badges/Template/PreviewSingle" + EventName + ".doc");
String PDFPreview = Server.MapPath("/ERAS/Badges/Template/PDFPreviewSingle" + EventName + ".pdf");
String previewPDFs = Server.MapPath("/ERAS/Badges/Template/PreviewPDFs" + EventName + ".pdf");
if (System.IO.File.Exists((String)eventTemplate))
{
if (vulGegevensIn == true)
{
//This creates a Worddocument and fills in names etc from database
CreateWordDocument(vulGegevensIn, eventTemplate, SinglePreview, false);
//This saves the SinglePreview.doc as a PDF @param place of PDFPreview
CreatePDF(SinglePreview, PDFPreview);
//Trying to merge
String[] previewsSmall=new …
Run Code Online (Sandbox Code Playgroud) 如何使用iTextSharp隐藏表格边框.我使用以下代码生成文件:
var document = new Document(PageSize.A4, 50, 50, 25, 25);
// Create a new PdfWriter object, specifying the output stream
var output = new MemoryStream();
var writer = PdfWriter.GetInstance(document, output);
document.Open();
PdfPTable table = new PdfPTable(3);
var bodyFont = FontFactory.GetFont("Arial", 10, Font.NORMAL);
PdfPCell cell = new PdfPCell(new Phrase("Header spanning 3 columns"));
cell.Colspan = 3;
cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
table.AddCell(cell);
Font arial = FontFactory.GetFont("Arial", 6, BaseColor.BLUE);
cell = new PdfPCell(new Phrase("Font test is here ", arial));
cell.PaddingLeft = 5f; …
Run Code Online (Sandbox Code Playgroud) itextsharp ×10
c# ×8
pdf ×5
.net ×2
asp.net ×1
asp.net-mvc ×1
email ×1
html ×1
html-to-pdf ×1
image ×1
merge ×1
vb.net ×1
xmlworker ×1