So I got a problem with saving my pdf.
I want to create an inventory based on a template, a template with no acrofields.
The template contains the companies logo and some data like the address.
What I am trying to do is copy the page from the template, paste it in a new document and then add a table to it (the table with the inventory).
The main problem I have is that i don’t manage to save or show the result. In all my previouse projects I used the memorystream but now this method doesn’t seem to work when using the pdfCopy instead of the pdfWriter.
Could anybody help me? this is my code.
public void PrintInventory(string path, MemoryStream ms)
{
VoorraadService vService = new VoorraadService();
PdfReader reader;
Document document;
PdfCopy copy;
try
{
var stock = vService.GetInventaris();
reader = new PdfReader(path + "Images/Inventaris_Template.pdf");
document = new Document(reader.GetPageSizeWithRotation(1));
copy = new PdfCopy(document, ms);
document.Open();
while(stock.Count >0)
{
copy.AddPage(copy.GetImportedPage(reader, 1));
PdfPTable table = new PdfPTable(6);
table.WidthPercentage = 100;
for (int i = 0; i < 15 && stock.Count > 0; i++)
{
table.AddCell(new Phrase(stock[0].ArtikelID));
table.AddCell(new Phrase(stock[0].ExternArtikelID));
table.AddCell(new Phrase(stock[0].Naam));
table.AddCell(new Phrase(stock[0].Aankoopprijs.ToString()));
table.AddCell(new Phrase(stock[0].Aantal.ToString()));
table.AddCell(new Phrase(stock[0].Totaal.ToString()));
stock.Remove(stock[0]);
}
table.CompleteRow();
copy.Add(table);
}
document.Close();
reader.Close();
}
catch (Exception e)
{ return; }
}
And the event on the print button:
protected void btnPrinten_Click(object sender, EventArgs e)
{
using (MemoryStream ms = new MemoryStream())
{
ms.Position = 0;
PdfGenerator pdf = new PdfGenerator();
pdf.PrintInventory(Request.Url.OriginalString.Replace("Applicatie/Voorraad/Inventory.aspx", ""), ms);
Response.ContentType = "application/pdf";
Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
Response.OutputStream.Flush();
Response.OutputStream.Close();
}
}
The problem is solved now.
My main problem was my Updatepanel, Because the printbutton wasn’t causing a real postback the saving of the file couldn’t be completed.
That’s why I didn’t get any result when i pressed the button.
Now that i used it as a postback trigger in my updatepanel all is solved.
You guys were also right about the pdfcopy I changed it to a writer and now everything works out like it should.