本文整理匯總了C#中iTextSharp.text.pdf.PdfStamper.GetUnderContent方法的典型用法代碼示例。如果您正苦於以下問題:C# PdfStamper.GetUnderContent方法的具體用法?C# PdfStamper.GetUnderContent怎麽用?C# PdfStamper.GetUnderContent使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類iTextSharp.text.pdf.PdfStamper
的用法示例。
在下文中一共展示了PdfStamper.GetUnderContent方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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: WatermarkPDF_SW
public bool WatermarkPDF_SW(string SourcePdfPath, string OutputPdfPath, string WatermarkImageUrl, int positionX, int positionY, int WatermarkHeight, int WatermarkWidth, out string msg)
{
try
{
PdfReader reader = new PdfReader(SourcePdfPath);
PdfStamper stamp = new PdfStamper(reader, new FileStream(OutputPdfPath, FileMode.Create));
int n = reader.NumberOfPages;
int i = 0;
PdfContentByte under;
WatermarkWidth = WatermarkWidth / n;
//����ط�Ҫע�⣬�Ǹ���ҳ������̬����ͼƬ��ַ���м�ҳ�ͼ��ؼ�ҳ��ͼƬ
string WatermarkPath = Server.MapPath(Request.ApplicationPath + "/HTProject/Pages/Images/��ͬ��������" + n + "/");
string WatermarkPath2 = "";
while (i < n)
{
i++;
WatermarkPath2 = WatermarkPath + i + ".gif";
iTextSharp.text.Image im = iTextSharp.text.Image.GetInstance(WatermarkPath2);
im.SetAbsolutePosition(positionX, positionY);
im.ScaleAbsolute(WatermarkWidth, WatermarkHeight);
under = stamp.GetUnderContent(i);
under.AddImage(im, true);
}
stamp.Close();
reader.Close();
}
catch (Exception ex)
{
msg = ex.Message;
return false;
}
msg = "��ˮӡ�ɹ���";
return true;
}
示例3: WatermarkPDF_SN
public bool WatermarkPDF_SN(string SourcePdfPath, string OutputPdfPath, string WatermarkPath, int positionX, int positionY, int WatermarkHeight, int WatermarkWidth, out string msg)
{
try
{
PdfReader reader = new PdfReader(SourcePdfPath);
PdfStamper stamp = new PdfStamper(reader, new FileStream(OutputPdfPath, FileMode.Create));
int n = reader.NumberOfPages;
int i = 0;
PdfContentByte under;
iTextSharp.text.Image im = iTextSharp.text.Image.GetInstance(WatermarkPath);
im.SetAbsolutePosition(positionX, positionY);
im.ScaleAbsolute(WatermarkWidth, WatermarkHeight);
while (i < n)
{
i++;
under = stamp.GetUnderContent(i);
under.AddImage(im, true);
}
stamp.Close();
reader.Close();
}
catch (Exception ex)
{
msg = ex.Message;
return false;
}
msg = "��ˮӡ�ɹ���";
return true;
}
示例4: ManipulatePdf
/// <summary>
/// Fills out and flattens a form with the name, company and country.
/// </summary>
/// <param name="src"> the path to the original form </param>
/// <param name="dest"> the path to the filled out form </param>
public void ManipulatePdf(String src, String dest)
{
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileStream(dest, FileMode.Create)); // create?
int n = reader.NumberOfPages;
Rectangle pagesize;
for (int i = 1; i <= n; i++)
{
PdfContentByte under = stamper.GetUnderContent(i);
pagesize = reader.GetPageSize(i);
float x = (pagesize.Left + pagesize.Right)/2;
float y = (pagesize.Bottom + pagesize.Top)/2;
PdfGState gs = new PdfGState();
gs.FillOpacity = 0.3f;
under.SaveState();
under.SetGState(gs);
under.SetRGBColorFill(200, 200, 0);
ColumnText.ShowTextAligned(under, Element.ALIGN_CENTER,
new Phrase("Watermark", new Font(Font.FontFamily.HELVETICA, 120)),
x, y, 45);
under.RestoreState();
}
stamper.Close();
reader.Close();
}
示例5: ManipulatePdf
// ---------------------------------------------------------------------------
/**
* Manipulates a PDF file src with the file dest as result
* @param src the original PDF
* @throws DocumentException
*/
public byte[] ManipulatePdf(byte[] src) {
PdfReader reader = new PdfReader(src);
using (MemoryStream ms = new MemoryStream()) {
using (PdfStamper stamper = new PdfStamper(reader, ms, '\0', true)) {
PdfContentByte cb = stamper.GetUnderContent(1);
cb.BeginText();
cb.SetFontAndSize(BaseFont.CreateFont(), 12);
cb.ShowTextAligned(Element.ALIGN_LEFT, "Hello People!", 36, 770, 0);
cb.EndText();
}
return ms.ToArray();
}
}
示例6: 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();
}
}
示例7: ManipulatePdf
/// <summary>
/// Fills out and flattens a form with the name, company and country.
/// </summary>
/// <param name="src"> the path to the original form </param>
/// <param name="dest"> the path to the filled out form </param>
public void ManipulatePdf(String src, String dest)
{
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileStream(dest, FileMode.Create));
PdfContentByte under = stamper.GetUnderContent(1);
PdfGState gs = new PdfGState();
gs.FillOpacity = 0.3f;
under.SaveState();
under.SetGState(gs);
under.SetRGBColorFill(200, 200, 0);
ColumnText.ShowTextAligned(under, Element.ALIGN_CENTER,
new Phrase("Watermark", new Font(Font.FontFamily.HELVETICA, 120)),
297, 421, 45);
under.RestoreState();
stamper.Close();
reader.Close();
}
示例8: ApplyWaterMark
private static void ApplyWaterMark(string filePath)
{
Logger.LogI("ApplyWatermark -> " + filePath);
var watermarkedFile = Path.GetFileNameWithoutExtension(filePath) + "-w.pdf";
using (var reader1 = new PdfReader(filePath))
{
using (var fs = new FileStream(watermarkedFile, FileMode.Create, FileAccess.Write, FileShare.None))
using (var stamper = new PdfStamper(reader1, fs))
{
var pageCount = reader1.NumberOfPages;
var layer = new PdfLayer("WatermarkLayer", stamper.Writer);
for (var i = 1; i <= pageCount; i++)
{
var rect = reader1.GetPageSize(i);
var cb = stamper.GetUnderContent(i);
cb.BeginLayer(layer);
cb.SetFontAndSize(BaseFont.CreateFont(
BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 50);
var gState = new PdfGState {FillOpacity = 0.25f};
cb.SetGState(gState);
cb.SetColorFill(BaseColor.BLACK);
cb.BeginText();
cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER,
"(c)2015 ScrapEra", rect.Width/2, rect.Height/2, 45f);
cb.EndText();
cb.EndLayer();
}
}
}
File.Delete(filePath);
}
示例9: LayerStampingTest
public void LayerStampingTest() {
String outPdf = DestFolder + "out3.pdf";
PdfReader reader =
new PdfReader(TestResourceUtils.GetResourceAsStream(TestResourcesPath, "House_Plan_Final.pdf"));
PdfStamper stamper = new PdfStamper(reader, File.Create(outPdf));
PdfLayer logoLayer = new PdfLayer("Logos", stamper.Writer);
PdfContentByte cb = stamper.GetUnderContent(1);
cb.BeginLayer(logoLayer);
Image iImage = Image.GetInstance(TestResourceUtils.GetResourceAsStream(TestResourcesPath, "Willi-1.jpg"));
iImage.ScalePercent(24f);
iImage.SetAbsolutePosition(100, 100);
cb.AddImage(iImage);
cb.EndLayer();
stamper.Close();
Assert.Null(new CompareTool().CompareByContent(outPdf, TestResourceUtils.GetResourceAsTempFile(TestResourcesPath, "cmp_House_Plan_Final.pdf"), DestFolder, "diff_"));
}
示例10: GetPageNumber
/// <summary>
/// Methode um Nummern und Projektnamen auf Pdf zu drucken
/// </summary>
///
/// Erstellt von Adrian Glasnek
public void GetPageNumber(SaveFileDialog save, int pageNumberBottomPosition)
{
try
{
byte[] bytes = File.ReadAllBytes(save.FileName);
Font BlackFont = FontFactory.GetFont("Arial", 9, Font.BOLD, BaseColor.BLACK);
using (MemoryStream stream = new MemoryStream())
{
PdfReader reader = new PdfReader(bytes);
using (PdfStamper stamper = new PdfStamper(reader, stream))
{
int pages = reader.NumberOfPages;
//Schleife um zu gewährleisten das jede Seite des Dokuments berücksichtigt wird
for (int i = 1; i <= pages; i++)
{
//Seitenzahl
ColumnText.ShowTextAligned(stamper.GetUnderContent(i), Element.ALIGN_RIGHT, new Phrase(i.ToString(), BlackFont), pageNumberBottomPosition, 15f, 0);
//Projekt Name
ColumnText.ShowTextAligned(stamper.GetUnderContent(i), Element.ALIGN_RIGHT, new Phrase(Project.Name.ToString(), BlackFont), 400, 15f, 0);
}
}
bytes = stream.ToArray();
}
File.WriteAllBytes(save.FileName, bytes);
}
catch (Exception) { throw new Exception("Bitte schließen Sie das geöffnete Pdf"); }
}
示例11: WriteToPdf
/// <summary>
/// Adds a watermark to all pages in the pdf, even takes into account orientation
/// </summary>
/// <remarks>
/// http://footheory.com/blogs/donnfelker/archive/2008/05/11/using-itextsharp-to-watermark-write-text-to-existing-pdf-s.aspx
/// </remarks>
/// <param name="sourceFile"></param>
/// <param name="stringToWriteToPdf"></param>
/// <returns></returns>
public byte[] WriteToPdf(byte[] pdf, string stringToWriteToPdf)
{
var reader = new PdfReader(pdf);
using (MemoryStream memoryStream = new MemoryStream())
{
//
// PDFStamper is the class we use from iTextSharp to alter an existing PDF.
//
PdfStamper pdfStamper = new PdfStamper(reader, memoryStream);
for (int i = 1; i <= reader.NumberOfPages; i++) // Must start at 1 because 0 is not an actual page.
{
//
// If you ask for the page size with the method getPageSize(), you always get a
// Rectangle object without rotation (rot. 0 degrees)—in other words, the paper size
// without orientation. That’s fine if that’s what you’re expecting; but if you reuse
// the page, you need to know its orientation. You can ask for it separately with
// getPageRotation(), or you can use getPageSizeWithRotation(). - (Manning Java iText Book)
//
//
Rectangle pageSize = reader.GetPageSizeWithRotation(i);
//
// Gets the content ABOVE the PDF, Another option is GetUnderContent(...)
// which will place the text below the PDF content.
//
PdfContentByte pdfPageContents = pdfStamper.GetUnderContent(i);
pdfPageContents.BeginText(); // Start working with text.
//
// Create a font to work with
//
BaseFont baseFont = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, Encoding.ASCII.EncodingName, false);
pdfPageContents.SetFontAndSize(baseFont, 100); // 40 point font
//pdfPageContents.SetRGBColorFill(255, 0, 0); // Sets the color of the font, RED in this instance
pdfPageContents.SetColorFill(BaseColor.LIGHT_GRAY);
//
// Angle of the text. This will give us the angle so we can angle the text diagonally
// from the bottom left corner to the top right corner through the use of simple trigonometry.
//
float textAngle =
(float) GetHypotenuseAngleInDegreesFrom(pageSize.Height, pageSize.Width);
//
// Note: The x,y of the Pdf Matrix is from bottom left corner.
// This command tells iTextSharp to write the text at a certain location with a certain angle.
// Again, this will angle the text from bottom left corner to top right corner and it will
// place the text in the middle of the page.
//
pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_CENTER, stringToWriteToPdf,
pageSize.Width/2,
pageSize.Height/2,
textAngle);
pdfPageContents.EndText(); // Done working with text
}
pdfStamper.FormFlattening = true; // enable this if you want the PDF flattened.
pdfStamper.Close(); // Always close the stamper or you'll have a 0 byte stream.
return memoryStream.ToArray();
}
}
示例12: AddWaterMark
private bool AddWaterMark()
{
PdfReader reader = new PdfReader(filePath + fileName);
string waterMarkedFileName = GetRandomFileName();
using (FileStream fileStream = new FileStream(filePath + waterMarkedFileName,FileMode.Create))
{
PdfStamper pdfStamper = new PdfStamper(reader, fileStream);
for (int i = 1; i <= reader.NumberOfPages; i++) // Must start at 1 because 0 is not an actual page.
{
//
// If you ask for the page size with the method getPageSize(), you always get a
// Rectangle object without rotation (rot. 0 degrees)—in other words, the paper size
// without orientation. That’s fine if that’s what you’re expecting; but if you reuse
// the page, you need to know its orientation. You can ask for it separately with
// getPageRotation(), or you can use getPageSizeWithRotation(). - (Manning Java iText Book)
//
//
Rectangle pageSize = reader.GetPageSizeWithRotation(i);
//
// Gets the content ABOVE the PDF, Another option is GetUnderContent(...)
// which will place the text below the PDF content.
//
PdfContentByte pdfPageContents = pdfStamper.GetUnderContent(i);
pdfPageContents.BeginText(); // Start working with text.
//
// Create a font to work with
//
BaseFont baseFont = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, Encoding.ASCII.EncodingName, false);
pdfPageContents.SetFontAndSize(baseFont, 120); // 80 point font
pdfPageContents.SetRGBColorFill(180, 180, 180); // Sets the color of the font, RED in this instance
//
// Angle of the text. This will give us the angle so we can angle the text diagonally
// from the bottom left corner to the top right corner through the use of simple trigonometry.
//
float textAngle = GetHypotenuseAngleInDegreesFrom(pageSize.Height, pageSize.Width);
//
// Note: The x,y of the Pdf Matrix is from bottom left corner.
// This command tells iTextSharp to write the text at a certain location with a certain angle.
// Again, this will angle the text from bottom left corner to top right corner and it will
// place the text in the middle of the page.
//
pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "Trial Version",
pageSize.Width / 2,
pageSize.Height / 2,
textAngle);
pdfPageContents.EndText(); // Done working with text
}
pdfStamper.FormFlattening = true; // enable this if you want the PDF flattened.
pdfStamper.Close(); // Always close the stamper or you'll have a 0 byte stream.
fileStream.Close();
File.Delete(filePath + fileName);
fileName = waterMarkedFileName;
//return memoryStream.ToArray();
}
return true;
}
示例13: SetBackground
public void SetBackground(string pdfFile, Stream stream, Drawing.Image image, int type = 0) {
PdfReader reader = null;
PdfStamper stamper = null;
try {
reader = new PdfReader(pdfFile);
stamper = new PdfStamper(reader, stream);
var totalPage = reader.NumberOfPages;
for (int current = 1; current <= totalPage; current++) {
var canvas = stamper.GetUnderContent(current);
var page = stamper.GetImportedPage(reader, current);
var img = Image.GetInstance(image, Drawing.Imaging.ImageFormat.Png);
switch (type) {
case 1: //top left
img.SetAbsolutePosition(0, page.Height - img.Height);
canvas.AddImage(img);
break;
case 2: //top center
img.SetAbsolutePosition((page.Width - img.Width) / 2, page.Height - img.Height);
canvas.AddImage(img);
break;
case 3: //top right
img.SetAbsolutePosition(page.Width - img.Width, page.Height - img.Height);
canvas.AddImage(img);
break;
case 4: //middle left
img.SetAbsolutePosition(0, (page.Height - img.Height) / 2);
canvas.AddImage(img);
break;
case 5: //middle center
img.SetAbsolutePosition((page.Width - img.Width) / 2, (page.Height - img.Height) / 2);
canvas.AddImage(img);
break;
case 6: //middle right
img.SetAbsolutePosition(page.Width - img.Width, (page.Height - img.Height) / 2);
canvas.AddImage(img);
break;
case 7: //bottom left
img.SetAbsolutePosition(0, 0);
canvas.AddImage(img);
break;
case 8: //bottom center
img.SetAbsolutePosition((page.Width - img.Width) / 2, 0);
canvas.AddImage(img);
break;
case 9: //bottom right
img.SetAbsolutePosition(page.Width - img.Width, 0);
canvas.AddImage(img);
break;
default: //平撲
int xRepeats = (int)((page.Width + img.Width - 1) / image.Width);
int yRepeats = (int)((page.Height + img.Height - 1) / image.Height);
for (int i = 0; i < xRepeats; i++) {
for (int j = 0; j < yRepeats; j++) {
img.SetAbsolutePosition(img.Width * i, image.Height * j);
canvas.AddImage(img);
}
}
break;
}
//img.SetAbsolutePosition(120, 120);
//canvas.AddImage(img);
//ColumnText.ShowTextAligned(canvas, Element.ALIGN_LEFT, new Phrase("Hello people!"), 36, 540, 0);
OnProgress(current, totalPage);
}
stamper.Close();
} catch (Exception ex) {
OnError(ex);
} finally {
stamper.Close();
reader.Close();
}
OnFinish();
}
示例14: WatermarkPDF
public bool WatermarkPDF(string SourcePdfPath, string OutputPdfPath, string WatermarkPath, int positionX, int positionY, int WatermarkHeight, int WatermarkWidth, out string msg, int ryCount)
{
try
{
PdfReader reader = new PdfReader(SourcePdfPath);
PdfStamper stamp = new PdfStamper(reader, new FileStream(OutputPdfPath, FileMode.Create));
int n = reader.NumberOfPages;
int i = 0;
PdfContentByte under;
while (i < n)
{
i++;
under = stamp.GetUnderContent(i);
iTextSharp.text.Image im = iTextSharp.text.Image.GetInstance(WatermarkPath);
im.ScaleAbsolute(WatermarkWidth, WatermarkHeight);
//im.SetAbsolutePosition(positionX, positionY);
float height = 841;
if (i == n)
{
string imgHYPath = Server.MapPath(Request.ApplicationPath + "/HTProject/Pages/Images/核驗章.png");
iTextSharp.text.Image imHY = iTextSharp.text.Image.GetInstance(imgHYPath);
imHY.ScaleAbsolute(100, 50);
if (i == 1)//說明就一頁
{
height = height - 230 - ryCount * 23 - 115;
//再加上已核驗的章
imHY.SetAbsolutePosition(200, height +50);
under.AddImage(imHY, true);
}
else
{
height = height - (ryCount - 18) * 23;
//height = 200;
//再加上已核驗的章
imHY.SetAbsolutePosition(200, height+50);
under.AddImage(imHY, true);
}
im.SetAbsolutePosition(positionX, height);
}
else//不是第一頁,也不是最後一頁
{
im.SetAbsolutePosition(positionX, positionY);
}
under.AddImage(im, true);
}
stamp.Close();
reader.Close();
}
catch (Exception ex)
{
msg = ex.Message;
return false;
}
msg = "加水印成功!";
return true;
}
示例15: WatermarkPDF
public bool WatermarkPDF(string SourcePdfPath, string OutputPdfPath, string WatermarkImageUrl, int positionX, int positionY, int WatermarkHeight, int WatermarkWidth, out string msg, string XMAdd)
{
try
{
PdfReader reader = new PdfReader(SourcePdfPath);
PdfStamper stamp = new PdfStamper(reader, new FileStream(OutputPdfPath, FileMode.Create));
int n = reader.NumberOfPages;
int i = 0;
PdfContentByte under;
int WatermarkWidth2 = WatermarkWidth;
WatermarkWidth = WatermarkWidth / n;
//這個地方要注意,是根據頁數來動態加載圖片地址,有幾頁就加載幾頁的圖片
string WatermarkPath = Server.MapPath(Request.ApplicationPath + "/HTProject/Pages/Images/合同備案公章" + n + "/");
string WatermarkPath2 = "";
while (i < n)
{
i++;
WatermarkPath2 = WatermarkPath + i + ".gif";
iTextSharp.text.Image im = iTextSharp.text.Image.GetInstance(WatermarkPath2);
im.SetAbsolutePosition(positionX, positionY);
im.ScaleAbsolute(WatermarkWidth, WatermarkHeight);
under = stamp.GetUnderContent(i);
under.AddImage(im, true);
//還要在最後一頁加蓋公章,如果是江陰、宜興的,要同時加蓋他們對應的公章,然後再加上市建設局的公章
if (i == n)//表示是最後一頁
{
string imgPath = "";
string imgHYPath = Server.MapPath(Request.ApplicationPath + "/HTProject/Pages/Images/核驗章.png");
iTextSharp.text.Image imHY = iTextSharp.text.Image.GetInstance(imgHYPath);
imHY.ScaleAbsolute(100, 50);
//江陰/宜興
if (XMAdd == "320281")
{
imgPath = Server.MapPath(Request.ApplicationPath + "/HTProject/Pages/Images/江陰合同備案公章.gif");
iTextSharp.text.Image imQX = iTextSharp.text.Image.GetInstance(imgPath);
imQX.SetAbsolutePosition(400, 350);
imQX.ScaleAbsolute(WatermarkWidth2, WatermarkHeight);
under = stamp.GetUnderContent(i);
under.AddImage(imQX, true);
//加核驗章
imHY.SetAbsolutePosition(200, 390);
under.AddImage(imHY, true);
}
else if (XMAdd == "320282")
{
imgPath = Server.MapPath(Request.ApplicationPath + "/HTProject/Pages/Images/宜興合同備案公章.gif");
iTextSharp.text.Image imQX = iTextSharp.text.Image.GetInstance(imgPath);
imQX.SetAbsolutePosition(400, 350);
imQX.ScaleAbsolute(WatermarkWidth2, WatermarkHeight);
under = stamp.GetUnderContent(i);
under.AddImage(imQX, true);
//加核驗章
imHY.SetAbsolutePosition(200, 390);
under.AddImage(imHY, true);
}
//無錫市
imgPath = Server.MapPath(Request.ApplicationPath + "/HTProject/Pages/Images/合同備案公章.gif");
iTextSharp.text.Image imWX = iTextSharp.text.Image.GetInstance(imgPath);
imWX.SetAbsolutePosition(400, 150);
imWX.ScaleAbsolute(WatermarkWidth2, WatermarkHeight);
under = stamp.GetUnderContent(i);
under.AddImage(imWX, true);
//加核驗章
imHY.SetAbsolutePosition(200, 190);
under.AddImage(imHY, true);
}
}
stamp.Close();
reader.Close();
}
catch (Exception ex)
{
msg = ex.Message;
return false;
}
msg = "加水印成功!";
return true;
}