本文整理匯總了C#中iTextSharp.text.pdf.PdfStamper.InsertPage方法的典型用法代碼示例。如果您正苦於以下問題:C# PdfStamper.InsertPage方法的具體用法?C# PdfStamper.InsertPage怎麽用?C# PdfStamper.InsertPage使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類iTextSharp.text.pdf.PdfStamper
的用法示例。
在下文中一共展示了PdfStamper.InsertPage方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: Make_pdf
private void Make_pdf()
{
//第一頁
int ti = Convert.ToInt32(times.Text);
int he = Convert.ToInt32(head.Text);
string nu;
if (he / 10 == 0) nu = "0000000" + he.ToString();
else if (he / 100 == 0) nu = "000000" + he.ToString();
else if (he / 1000 == 0) nu = "00000" + he.ToString();
else if (he / 10000 == 0) nu = "0000" + he.ToString();
else if (he / 10000 == 0) nu = "000" + he.ToString();
else if (he / 100000 == 0) nu = "00" + he.ToString();
else if (he / 1000000 == 0) nu = "0" + he.ToString();
else nu = he.ToString();
PdfReader reader = new PdfReader(@"data\Doc1.pdf");
//
//將範本檔按丟入stream並給PdfStamper
//PdfStamper stamper = new PdfStamper(reader, stream);
PdfStamper stamper = new PdfStamper(reader, new FileStream([email protected]"\"[email protected]".pdf", FileMode.Create));
stamper.Writer.CloseStream = false;
AcroFields acroFields = stamper.AcroFields;
//BaseFont bfkaiu = BaseFont.CreateFont("C:\\WINDOWS\\Fonts\\kaiu.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//BaseFont bftimes = BaseFont.CreateFont("C:\\WINDOWS\\Fonts\\times.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//重設pdf樣式
BaseFont bf = BaseFont.CreateFont("C:/WINDOWS/Fonts/kaiu.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
acroFields.SetFieldProperty("Text1", "textfont", bf, null);
//將資料放進pdf內
acroFields.SetField("Text1", nu);
//infile.Flush();
int i=1;
int insPage = 2;
//第n頁
while (i < ti)// 若 page-i,i比page小 代表還有頁數還沒完成
{
i++; he++;
string nu2;
if (he / 10 == 0) nu2 = "0000000" + he.ToString();
else if (he / 100 == 0) nu2 = "000000" + he.ToString();
else if (he / 1000 == 0) nu2 = "00000" + he.ToString();
else if (he / 10000 == 0) nu2 = "0000" + he.ToString();
else if (he / 10000 == 0) nu2 = "000" + he.ToString();
else if (he / 100000 == 0) nu2 = "00" + he.ToString();
else if (he / 1000000 == 0) nu2 = "0" + he.ToString();
else nu2 = he.ToString();
//將Sample PDF檔丟入PdfReader物件
MemoryStream stream2 = new MemoryStream();
PdfReader reader2 = new PdfReader(@"data\Doc1.pdf");
//將範本檔按丟入stream並給PdfStamper
PdfStamper stamper2 = new PdfStamper(reader2, stream2);
stamper2.Writer.CloseStream = false;
AcroFields acroFields2 = stamper2.AcroFields;
//BaseFont bfkaiu2 = BaseFont.CreateFont("C:\\WINDOWS\\Fonts\\kaiu.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//BaseFont bftimes2 = BaseFont.CreateFont("C:\\WINDOWS\\Fonts\\times.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//重設pdf樣式
//BaseFont bf2 = BaseFont.CreateFont("C:/WINDOWS/Fonts/kaiu.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
it.Font textFont2 = new it.Font(bf, 12);
acroFields2.SetFieldProperty("Text1", "textfont", bf, null);
//將資料放進pdf內
acroFields2.SetField("Text1", nu2);
stamper2.FormFlattening = true;
stamper2.Close();
reader2.Close();
//將第n頁裝進第一頁後麵
MemoryStream rm = new MemoryStream(stream2.GetBuffer(), 0, stream2.GetBuffer().Length);
PdfReader tesrreader = new PdfReader(rm);
stamper.InsertPage(insPage, reader.GetPageSize(1));
stamper.GetUnderContent(insPage).AddTemplate(stamper.GetImportedPage(tesrreader, 1), 0, 0);
insPage += 1;//插入變數++
}
//第n頁END
stamper.FormFlattening = true;
stamper.Close();
reader.Close();
//PDF結束
//stamper.Flush();
Dispose();
}
示例2: Index
public ActionResult Index(IEnumerable<HttpPostedFileBase> files)
{
// image file extension check ignore for brevity
var fileList = files.Where(h => h != null && h.ContentLength > 0);
if (fileList.Count() < 1)
throw new Exception("no files uploaded");
var absoluteX = 20;
var absoluteY = 800;
using (var stream = new MemoryStream())
{
using (var stamper = new PdfStamper(Helpers.Pdf.GetTestReader(), stream))
{
var pageOneSize = stamper.Reader
.GetPageSize(APPEND_NEW_PAGE_NUMBER - 1);
foreach (var file in fileList)
{
using (var br = new BinaryReader(file.InputStream))
{
var imageBytes = br.ReadBytes(file.ContentLength);
var image = Image.GetInstance(imageBytes);
// still not sure if you want to add a new blank page, but
// here's how
stamper.InsertPage(
APPEND_NEW_PAGE_NUMBER,
pageOneSize
);
// image absolute position
image.SetAbsolutePosition(
absoluteX, absoluteY - image.Height
);
// scale image if needed
// image.ScaleAbsolute(...);
// PAGE_NUMBER => add image to specific page number
stamper.GetOverContent(APPEND_NEW_PAGE_NUMBER)
.AddImage(image);
}
++APPEND_NEW_PAGE_NUMBER;
}
}
return File(stream.ToArray(), "application/pdf", "test.pdf");
}
}
示例3: ManipulatePdf
// ---------------------------------------------------------------------------
/**
* Manipulates a PDF file src
* @param src the original PDF
* @param stationery the resulting PDF
*/
public byte[] ManipulatePdf(byte[] src, byte[] stationery) {
ColumnText ct = new ColumnText(null);
string SQL =
@"SELECT country, id FROM film_country
ORDER BY country
";
using (var c = AdoDB.Provider.CreateConnection()) {
c.ConnectionString = AdoDB.CS;
using (DbCommand cmd = c.CreateCommand()) {
cmd.CommandText = SQL;
c.Open();
using (var r = cmd.ExecuteReader()) {
while (r.Read()) {
ct.AddElement(new Paragraph(
24, new Chunk(r["country"].ToString())
));
}
}
}
}
// Create a reader for the original document and for the stationery
PdfReader reader = new PdfReader(src);
PdfReader rStationery = new PdfReader(stationery);
using (MemoryStream ms = new MemoryStream()) {
// Create a stamper
using (PdfStamper stamper = new PdfStamper(reader, ms)) {
// Create an imported page for the stationery
PdfImportedPage page = stamper.GetImportedPage(rStationery, 1);
int i = 0;
// Add the content of the ColumnText object
while(true) {
// Add a new page
stamper.InsertPage(++i, reader.GetPageSize(1));
// Add the stationary to the new page
stamper.GetUnderContent(i).AddTemplate(page, 0, 0);
// Add as much content of the column as possible
ct.Canvas = stamper.GetOverContent(i);
ct.SetSimpleColumn(36, 36, 559, 770);
if (!ColumnText.HasMoreText(ct.Go()))
break;
}
}
return ms.ToArray();
}
}