PDF来自http get。 我在这里看了几个例子,并且无法压制这个bug。
注意:我只保存PDF用于调试目的,在我弄清楚它之后它将完全在内存中并将修改后的PDF发布到api as base64。
我做错了什么?
//get Invoice PDF
Byte[] pdf_bytes = Convert.FromBase64String(GetInvoicePDF(account, invoice, config));
//Save a Debug Copy
using( FileStream fs = File.Create("C:\\temp\\b4-invoice-" + invoice.id + ".pdf") )
{
fs.Write(pdf_bytes, 0, (int)pdf_bytes.Length);
fs.Flush();
fs.Close();
}
Document doc = new Document(PageSize.A4, 30, 30, 30, 30);
MemoryStream msPDF = new MemoryStream(pdf_bytes);
//Create Bitmap
System.Drawing.Bitmap Bmp = new System.Drawing.Bitmap(333, 195);
using( System.Drawing.Graphics gfx = System.Drawing.Graphics.FromImage(Bmp) )
using( System.Drawing.SolidBrush brush = new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(255, 255, 255)) )
{
gfx.FillRectangle(brush, 0, 0, 333, 195);
}
Image img = Image.GetInstance(Bmp, System.Drawing.Imaging.ImageFormat.Bmp);
img.SetAbsolutePosition(0, 0);
PdfWriter wri = PdfWriter.GetInstance(doc, msPDF);
doc.Open();//Open Document to write
doc.Add(img);
doc.Close();
doc.Dispose();
Byte[] pdf_bytes_out = msPDF.ToArray();
//Save a Debug Copy
using( FileStream fs = File.Create("C:\\temp\\invoice-" + invoice.id + ".pdf") )
{
fs.Write(pdf_bytes_out, 0, (int)pdf_bytes_out.Length);
fs.Flush();
fs.Close();
}
答案 0 :(得分:0)
我明白了。事实证明,我根本不需要使用doc对象。
//get Invoice PDF
Byte[] pdf_bytes = Convert.FromBase64String(GetInvoicePDF(account, invoice, config));
//Save a Debug Copy
using( FileStream fs = File.Create("C:\\temp\\b4-invoice-" + invoice.id + ".pdf") )
{
fs.Write(pdf_bytes, 0, (int)pdf_bytes.Length);
fs.Flush();
fs.Close();
}
MemoryStream msPDF = new MemoryStream();
PdfReader reader = new PdfReader(pdf_bytes);
Rectangle rect = reader.GetPageSize(1);
using( PdfStamper stamper = new PdfStamper(reader, msPDF) )
{
// modify the pdf content
PdfContentByte cb = stamper.GetOverContent(1);
//cb.SetColorStroke(iTextSharp.text.BaseColor.GREEN);
//cb.SetLineWidth(1f);
cb.SetColorFill(iTextSharp.text.BaseColor.WHITE);
cb.Rectangle(rect.GetLeft(0), rect.GetTop(0) - 250, 350, 250);
cb.Fill();
//cb.Stroke();
}
reader.Close();
Byte[] pdf_bytes_out = msPDF.GetBuffer();
using( FileStream fs = File.Create("C:\\temp\\invoice-" + invoice.id + ".pdf") )
{
fs.Write(pdf_bytes_out, 0, (int)pdf_bytes_out.Length);
fs.Flush();
fs.Close();
}