我有一些代码用于在浏览器中显示来自ftp的pdf文件。我必须为来自ftp的pdf添加一些文本。我从antoher项目中找到了一些工作代码,但它从iis本地路径写入pdf。
//** these are my working code that show pdf files in browser from ftp. **//
string filename = Request.QueryString["view"];
string ftpServerIP = Session["Host"].ToString();
string ftpUserName = Session["user"].ToString();
string ftpPassword = Session["pssw"].ToString();
FileInfo objFile = new FileInfo(filename);
System.Net.FtpWebRequest request = (System.Net.FtpWebRequest)WebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + objFile.Name));
request.Credentials = new NetworkCredential(SftpUserName, ftpPassword);
request.Method = WebRequestMethods.Ftp.GetFileSize;
System.Net.FtpWebResponse response = (System.Net.FtpWebResponse)request.GetResponse();
int length = (int)(response.ContentLength * 1.1);
request = (System.Net.FtpWebRequest)WebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + objFile.Name));
request.Credentials = new NetworkCredential(ftpUserName, ftpPassword);
request.Method = WebRequestMethods.Ftp.DownloadFile;
response = (System.Net.FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
BinaryReader br = new BinaryReader(responseStream);
byte[] buffer = br.ReadBytes(length);
//** Can I work the itextsharp codes near here for adding text, without saving local, before download? **//
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.ContentEncoding = reader.CurrentEncoding;
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "inline; filename=" + Request.QueryString["name"]);
Response.BinaryWrite(buffer);
//** these are another code which I need to implement my code for adding some text in pdf files.**//
//** these are not my actual codes, just from another project's and I have to use them in my project.**//
//** The difference is in my project download pdf directly from an ftp, other one reads pdf in local files.**//
private void WriteToPdf(FileInfo sourceFile, string outbut_File, string stringToWriteToPdf)
{
PdfReader reader = new PdfReader(sourceFile.FullName);
PdfStamper pdfStamper = new PdfStamper(reader, new System.IO.FileStream(outbut_File, FileMode.Create, FileAccess.Write, FileShare.Write));
iTextSharp.text.pdf.PdfGState Gstates = new iTextSharp.text.pdf.PdfGState();
Gstates.StrokeOpacity = 0.3f;
Gstates.FillOpacity = 0.3f;
for (int i = 1; i <= reader.NumberOfPages; i++)
{
iTextSharp.text.Rectangle pageSize = reader.GetPageSizeWithRotation(i);
PdfContentByte pdfPageContents = pdfStamper.GetOverContent(i);
pdfPageContents.BeginText();
BaseFont baseFont__1 = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
//BaseFont.CreateFont("", BaseFont.WINANSI, BaseFont.NOT_EMBEDDED)
pdfPageContents.SetFontAndSize(baseFont__1, 15);
pdfPageContents.SetRGBColorFill(255, 0, 0);
pdfPageContents.SaveState();
float textAngle = 0;
pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, stringToWriteToPdf, 5, 5, textAngle);
pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, stringToWriteToPdf, pageSize.Width, pageSize.Height, textAngle);
pdfPageContents.EndText();
}
pdfStamper.FormFlattening = true;
pdfStamper.Close();
reader.Close();
Stream s = File.OpenRead(outbut_File);
byte[] buffer = new byte[s.Length + 1];
try
{
s.Read(buffer, 0, (Int32)s.Length);
}
finally
{
s.Close();
}
Response.ClearContent();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "inline; filename=" + Request.QueryString["name"]);
Response.WriteFile(outbut_File);
Response.End();
}
如何通过下载ftp并添加文本然后在浏览器中显示来自定义这些代码?