我有一个不是使用Adobe LifeCycle Designer创建的PDF文档。我想要做的是预先填写文档中的常见字段。
我已经研究了使用iTextSharp和PDFSharp提供给我的许多选项,但是不确定如何正确使用它。
我最近碰到了这篇文章:FillPDF,它有一个很好的文档,但是与我创建的内容不匹配。
我也在阅读有关可以在VS中导入并使用的iTextSharp的信息,但是我不确定从哪里开始。我看到了很多教程,但是都没有描述如何开始。
请帮忙...
我最近与itextsharp合作完成了一个大型项目
http://www.mikesdotnetting.com/Article/88/iTextSharp-Drawing-shapes-and-Graphics http://www.mikesdotnetting.com/Article/81/iTextSharp-Working-with-Fonts
这是一些可以帮助您入门的东西
但就从pdf读取然后输出回来而言,您将需要一些正则表达式来帮助您。
这是我拥有的示例代码之一(这会在每个新页面事件上创建一个页眉或页脚)
using CMS;
using CMS.Tags;
using CMS.Pages;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Globalization;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iTextSharp.text;
using iTextSharp.text.html;
using iTextSharp.text.xml;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.draw;
using System.IO;
using System.Text.RegularExpressions;
using System.Text;
using System.util;
public class pdfPage : iTextSharp.text.pdf.PdfPageEventHelper
{
public override void OnEndPage(PdfWriter writer, Document doc)
{
PdfContentByte cb = writer.DirectContent;
cb.SetLineWidth(1f);
cb.SetCMYKColorStroke(66, 59, 57, 38);
cb.MoveTo(30, 55);
cb.LineTo(doc.PageSize.Width - 30 , 55);
cb.Stroke();
cb.MoveTo(185, 80);
cb.LineTo(185, 25);
cb.Stroke();
ColumnText ct = new ColumnText(cb);
BaseFont bfTimes = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, false);
Font times = new Font(bfTimes, 10);
times.SetColor(90, 90, 90);
ct.SetSimpleColumn(new Phrase("text text", times), 60, 60, 175, 78, 15, Element.ALIGN_MIDDLE);
ct.Go();
times.SetColor(1, 73, 144);
ct.SetSimpleColumn(new Phrase("text textn", times), 60, 38, 175, 55, 15, Element.ALIGN_MIDDLE);
ct.Go();
times.SetColor(90, 90, 90);
ct.SetSimpleColumn(new Phrase("text here", times), 190, 60, doc.PageSize.Width - 32 , 78, 15, Element.ALIGN_RIGHT);
ct.Go();
times.SetColor(90, 90, 90);
ct.SetSimpleColumn(new Phrase("text here", times), 190, 38, doc.PageSize.Width - 32 , 55, 15, Element.ALIGN_RIGHT);
ct.Go();
}
}
这是我的开始pdf的代码的一部分
using (var ms = new MemoryStream())
{
using (var doc = new Document(PageSize.LETTER, 220f, 30f, 115f, 100f)){
try
{
pdfPage page = new pdfPage();
PdfWriter writer = PdfWriter.GetInstance(doc, ms);
writer.PageEvent = page;
doc.Open();
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(RESOURCE);
img.ScalePercent(49f);
//img.Width = doc.PageSize.Width;
//img.Alignment = iTextSharp.text.Image.ALIGN_CENTER;
img.SetAbsolutePosition(-8,
doc.PageSize.Height - 180.6f);
doc.Add(img);
这是我的输出代码(作为直接从服务器创建的下载pdf *未保存在服务器上)
}
catch (Exception ex)
{
//Log error;
}
finally
{
doc.Close();
}
}
Response.Clear();
//Response.ContentType = "application/pdf";
Response.ContentType = "application/octet-stream";
Response.AddHeader("content-disposition", "attachment;filename= Company " + namefile + ".pdf");
Response.Buffer = true;
Response.Clear();
var bytes = ms.ToArray();
Response.OutputStream.Write(bytes, 0, bytes.Length);
Response.OutputStream.Flush();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句