我想创建该项目,从Excel中读取并写入pdf并打印此pdf。 从Excel文件(从单元格)读取电脑或服务器上的原始pdf文件的目录,下一个单元格有信息在第二个pdf文件的顶部。如何保持itextSharp原始旋转页面(dll)
问题是在这里,原始pdf是水平的,风景,旋转和我的程序从原始pdf创建副本,并在复制PDF文件顶部从excel写入信息。但是,作为景观的pdf正在旋转270度。这是不行的。对于纵向旋转工作程序OK,复制OK并在副本顶部写入OK。 我的代码在哪里存在问题。
代码:
public int urediPDF(string inTekst)
{
if (inTekst != "0")
{
string pisava_arialBD = @"..\debug\arial.ttf";
string oldFile = null;
string inText = null;
string indeks = null;
//razbitje stringa
string[] vhod = inTekst.Split('#');
oldFile = vhod[0];
inText = vhod[1];
indeks = vhod[2];
string newFile = @"c:\da\2";
//odpre bralnik pdf
PdfReader reader = new PdfReader(oldFile);
Rectangle size = reader.GetPageSizeWithRotation(reader.NumberOfPages);
Document document = new Document(size);
//odpre zapisovalnik pdf
FileStream fs = new FileStream(newFile + "-" + indeks + ".pdf", FileMode.Create, FileAccess.Write);
PdfWriter writer = PdfWriter.GetInstance(document, fs);
//document.Open();
document.OpenDocument();
label2.Text = ("Status: " + reader.GetPageRotation(reader.NumberOfPages).ToString());
//določi sejo ustvarjanje pdf
PdfContentByte cb = writer.DirectContent;
//izbira pisave oblike
BaseFont bf = BaseFont.CreateFont(pisava_arialBD, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
cb.SetColorFill(BaseColor.RED);
cb.SetFontAndSize(bf, 8);
//pisanje teksta v pdf
cb.BeginText();
string text = inText;
//izbira koordinat za zapis pravilnega teksta v pdf (720 stopinj roatacija (ležeče) in 90 stopinj (pokončno))
if (reader.GetPageRotation(1) == 720) //ležeča postavitev
{
cb.ShowTextAligned(1, text, 10, 450, 0);
cb.EndText();
}
else //pokončna postavitev
{
cb.ShowTextAligned(1, text + " - pokončen", 10, 750, 0);
cb.EndText();
}
// create the new page and add it to the pdf
PdfImportedPage page = writer.GetImportedPage(reader, reader.NumberOfPages);
cb.AddTemplate(page, 0, 0);
// close the streams and voilá the file should be changed :)
document.Close();
fs.Close();
writer.Close();
reader.Close();
}
else
{
label2.Text = "Status: Končano zapisovanje";
return 0;
}
return 0;
}
图片假PDF:
您不幸发现不应该使用这种示例代码。如果你想在它上面添加PDF和图章,可以将代码基于[Web化iTextSharp示例](http://kuujinbo.info/iTextInAction2Ed/index.aspx)[StampText.cs](http:// kuujinbo。 info/iTextInAction2Ed/index.aspx?ch = Chapter06&ex = StampText)[iText in Action - 第2版] [http://www.manning.com/lowagie2/samplechapter6.pdf] /itextpdf.com/book/index.php)。而不是'PdfWriter'it使用'PdfStamper',它原样使用源页面。 – mkl 2013-03-08 11:01:53