int iNbFichier = 0;
while (File.Exists(folderName + "/" + newFile + ((iNbFichier > 0) ? "_" + iNbFichier.ToString() : "") + ext))
{
iNbFichier++;
}
// open the reader
PdfReader reader = new PdfReader(oldFile);
document = new Document(PageSize.A4, 5, 5, 120, 60);
// open the writer
FileStream fs = new FileStream(folderName + "/" + newFile + ((iNbFichier > 0) ? "_" + iNbFichier.ToString() : "") + ext, FileMode.Create, FileAccess.Write);
PdfWriter writer = PdfWriter.GetInstance(document, fs);
document.Open();
int numberPage = reader.NumberOfPages;
// the pdf content
PdfContentByte cb = writer.DirectContent;
for (int i = 1; i <= reader.NumberOfPages; i++)
{
// create the new page and add it to the pdf
PdfImportedPage page = writer.GetImportedPage(reader, i);
cb.AddTemplate(page, 0, 0);
if(i != numberPage) document.NewPage();
cb = writer.DirectContent;
}
if (availableSpace < 90)
{
writer.NewPage();
cb = writer.DirectContent;
}
// select the font properties
BaseFont bf = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb.SetColorFill(BaseColor.BLACK);
cb.SetFontAndSize(bf, 8);
// write the text in the pdf content
cb.BeginText();
string text = "Le " + DateTime.Now.ToString().Substring(0, 10);
// put the alignment and coordinates here
cb.ShowTextAligned(1, text, 455, 750, 90);
cb.EndText();
// write the text in the pdf content
cb.BeginText();
text = salarie.NOM + " " + salarie.PRENOM;
// put the alignment and coordinates here
cb.ShowTextAligned(1, text, 465, 750, 90);
cb.EndText();
// write the text in the pdf content
cb.BeginText();
text = "Signature :";
// put the alignment and coordinates here
cb.ShowTextAligned(1, text, 475, 750, 90);
cb.EndText();
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(globale.EncodePhoto(this.signature));
//img.SetAbsolutePosition(485, 750);
img.Border = 0;
//Resize picture to fit 380x150 ratio if its bigger
int intMaxWidth = 100;
int intMaxHeight = 50;
BitmapImage bmpMain = this.signature;
if (bmpMain.Height > intMaxHeight || bmpMain.Width > intMaxWidth)
{
double dblHeightRatio = Convert.ToDouble(intMaxHeight) / Convert.ToDouble(bmpMain.Height);
double dblWidthRatio = Convert.ToDouble(intMaxWidth) / Convert.ToDouble(bmpMain.Width);
double dblScaleRatio;
//Use the smaller ratio
if (dblHeightRatio > dblWidthRatio)
{
dblScaleRatio = dblWidthRatio;
}
else
{
dblScaleRatio = dblHeightRatio;
}
int intNewHeight = Convert.ToInt32(bmpMain.Height * dblScaleRatio);
int intNewWidth = Convert.ToInt32(bmpMain.Width * dblScaleRatio);
img.ScaleAbsolute((float)intNewWidth,
(float)intNewHeight);
}
img.SetAbsolutePosition(480, 700);
img.RotationDegrees = 90;
cb.AddImage(img);
// close the streams and voilá the file should be changed :)
document.Close();
fs.Close();
writer.Close();
reader.Close();
如果我的表格和页脚的可用空间是&lt;到90后我想创建一个新页面并将签名放在最后创建的页面上。
但我的代码不起作用。有人可以帮我解决这个问题吗?
我的申请是针对平板电脑的,因此复制处理需要太多资源才能在这种情况下使用。
答案 0 :(得分:0)
string oldFile = "Tmp.pdf";
string newFile = nomFichier = DateTime.Now.ToString().
Substring(0,10).Replace('/','-') +
"_" + salarie.NOM + "_" + salarie.PRENOM;
string ext = ".pdf";
string folderName = "FICHE_EPI";
//Si le tableau est trop bas pour faire passer la signature
if (availableSpace < 90)
{
var tempFileLocation = @"Tmp2.pdf";
var bytes = File.ReadAllBytes(@"Tmp.pdf");
using (var reader1 = new PdfReader(bytes))
{
var numberofPages = reader1.NumberOfPages;
var pages = 1;
if (pages != 0)
{
using (var fileStream = new FileStream(
tempFileLocation,
FileMode.Create,
FileAccess.Write))
{
using (var stamper = new PdfStamper(reader1, fileStream))
{
var rectangle = reader1.GetPageSize(1);
for (var i = 1; i <= pages; i++)
stamper.InsertPage(numberofPages + i, rectangle);
}
}
}
}
File.Delete(@"Tmp.pdf");
File.Move(tempFileLocation, @"Tmp.pdf");
这解决了我的问题。