大家好,感谢您的帮助。我正在尝试创建一个Web应用程序,利用iTextSharp导入多页pdf,不同页面上的地方文本,换句话说,第1页的某个文本,页面的不同文本等等。我已经成功了导入pdf,但文本正在逃避我。这是我的代码:
PdfReader reader = null;
Document sourceDocument = null;
PdfCopy pdfCopyProvider = null;
PdfImportedPage importedPage = null;
int startPage = 1;
int endPage = 2;
try
{
string sourcePdfPath = Server.MapPath(path);
// Intialize a new PdfReader instance with the contents of the source Pdf file:
reader = new PdfReader(sourcePdfPath);
// For simplicity, I am assuming all the pages share the same size
// and rotation as the first page:
sourceDocument = new Document(reader.GetPageSizeWithRotation(startPage));
// Initialize an instance of the PdfCopyClass with the source
// document and an output file stream:
//
MemoryStream ms = new MemoryStream();
pdfCopyProvider = new PdfCopy(sourceDocument, ms);
//
//pdfCopyProvider = new PdfCopy(sourceDocument,
// new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create));
sourceDocument.Open();
// Walk the specified range and add the page copies to the output file:
for (int i = startPage; i <= endPage; i++)
{
importedPage = pdfCopyProvider.GetImportedPage(reader, i);
pdfCopyProvider.AddPage(importedPage);
}
PdfContentByte cb = pdfCopyProvider.DirectContent;
PdfReader reader = null;
Document sourceDocument = null;
PdfCopy pdfCopyProvider = null;
PdfImportedPage importedPage = null;
int startPage = 1;
int endPage = 2;
try
{
string sourcePdfPath = Server.MapPath(path);
// Intialize a new PdfReader instance with the contents of the source Pdf file:
reader = new PdfReader(sourcePdfPath);
// For simplicity, I am assuming all the pages share the same size
// and rotation as the first page:
sourceDocument = new Document(reader.GetPageSizeWithRotation(startPage));
// Initialize an instance of the PdfCopyClass with the source
// document and an output file stream:
//
MemoryStream ms = new MemoryStream();
pdfCopyProvider = new PdfCopy(sourceDocument, ms);
//
//pdfCopyProvider = new PdfCopy(sourceDocument,
// new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create));
sourceDocument.Open();
// Walk the specified range and add the page copies to the output file:
for (int i = startPage; i <= endPage; i++)
{
importedPage = pdfCopyProvider.GetImportedPage(reader, i);
pdfCopyProvider.AddPage(importedPage);
}
PdfContentByte cb = pdfCopyProvider.DirectContent;
//select the font properties
BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb.SetColorFill(BaseColor.DARK_GRAY);
cb.SetFontAndSize(bf, 12);
//write the Patient Name
cb.BeginText();
//put the alignment and coordinates here
cb.ShowTextAligned(3, "Hello", 200, 200, 0);
cb.EndText();
sourceDocument.Close();
reader.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=file.pdf");
Response.Buffer = true;
Response.Clear();
Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
Response.OutputStream.Flush();
Response.End();
}
catch (Exception ex)
{
throw ex;
}
}
sourceDocument.Close();
reader.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=file.pdf");
Response.Buffer = true;
Response.Clear();
Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
Response.OutputStream.Flush();
Response.End();
}
catch (Exception ex)
{
throw ex;
}
}
任何帮助都将不胜感激。