ItextSharp 将行添加到 PDF 并转换为字节而不保存。
ItextSharp Add lines to PDF and convert into bytes without saving it.
我有一种方法可以获取 pdf 并在该 pdf 中添加行,然后创建一个新的 pdf 并将其保存在给定的路径中。现在我不想将该新文件保存在路径中而是我想将其转换为字节并发送电子邮件我该怎么做我尝试了几种方法但它所有的方式都在寻找路径说没有在这里找到路径是编码。
protected void CreatePDF()
{
string Oldfile = @"C:\BlankPDFt.pdf"; // Gets the Template / The actualy agreement Letter
// (new FileInfo("C:/Documents/Docs.pdf")).Directory.Create(); // Go create this folder if it's not there
string NewFile = "C:/Documents/Docs.pdf";
PdfReader reader = new PdfReader(Oldfile);
iTextSharp.text.Rectangle Size = reader.GetPageSizeWithRotation(1);
Document document = new Document(Size);
FileStream fs = new FileStream(NewFile , FileMode.Create, FileAccess.Write); // This is where it all ways look for new file path
i dont want it to save instead convert to bytes but wont work.
PdfWriter weiter = PdfWriter.GetInstance(document, fs);
document.Open();
PdfContentByte cb = weiter.DirectContent;
PdfImportedPage page = weiter.GetImportedPage(reader, 1);
cb.AddTemplate(page, 0, 0);
BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb.SetColorFill(BaseColor.BLACK);
cb.SetFontAndSize(bf, 12);
cb.BeginText();
// string Signatur = "Some texts"; // adds this text to that pdf
cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "Here", 335, 90, 0); // Insert the signature
cb.EndText();
}
PdfWriter.GetInstance()
方法接受一个 InputStream
作为它的第二个参数。您应该传递 MemoryStream
.
而不是传递 FileStream
MemoryStream memory_stream = new MemoryStream();
PdfWriter.GetInstance(document, memory_stream );
...
document.Close();
byte[] pdf_bytes = memory_stream.ToArray();
我有一种方法可以获取 pdf 并在该 pdf 中添加行,然后创建一个新的 pdf 并将其保存在给定的路径中。现在我不想将该新文件保存在路径中而是我想将其转换为字节并发送电子邮件我该怎么做我尝试了几种方法但它所有的方式都在寻找路径说没有在这里找到路径是编码。
protected void CreatePDF()
{
string Oldfile = @"C:\BlankPDFt.pdf"; // Gets the Template / The actualy agreement Letter
// (new FileInfo("C:/Documents/Docs.pdf")).Directory.Create(); // Go create this folder if it's not there
string NewFile = "C:/Documents/Docs.pdf";
PdfReader reader = new PdfReader(Oldfile);
iTextSharp.text.Rectangle Size = reader.GetPageSizeWithRotation(1);
Document document = new Document(Size);
FileStream fs = new FileStream(NewFile , FileMode.Create, FileAccess.Write); // This is where it all ways look for new file path
i dont want it to save instead convert to bytes but wont work.
PdfWriter weiter = PdfWriter.GetInstance(document, fs);
document.Open();
PdfContentByte cb = weiter.DirectContent;
PdfImportedPage page = weiter.GetImportedPage(reader, 1);
cb.AddTemplate(page, 0, 0);
BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb.SetColorFill(BaseColor.BLACK);
cb.SetFontAndSize(bf, 12);
cb.BeginText();
// string Signatur = "Some texts"; // adds this text to that pdf
cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "Here", 335, 90, 0); // Insert the signature
cb.EndText();
}
PdfWriter.GetInstance()
方法接受一个 InputStream
作为它的第二个参数。您应该传递 MemoryStream
.
FileStream
MemoryStream memory_stream = new MemoryStream();
PdfWriter.GetInstance(document, memory_stream );
...
document.Close();
byte[] pdf_bytes = memory_stream.ToArray();