Code:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MigraDoc.DocumentObjectModel;
using MigraDoc.Rendering;
using MigraDoc.Rendering.Printing;
using System.Diagnostics;
using WindowsFormsApplication1;
public partial class ProductDetails : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
PopulateControls();
string currentPageFileName = new FileInfo(this.Request.Url.LocalPath).Name;
HyperLink hylnk = (HyperLink)this.Master.FindControl("HyperLink1");
hylnk.NavigateUrl = (currentPageFileName + "?DepartmentID=" + Request.QueryString["DepartmentID"] + "&CategoryID=" + Request.QueryString["CategoryID"] + "&ProductID=" + Request.QueryString["ProductID"]);
HyperLink hylnk2 = (HyperLink)this.Master.FindControl("HyperLink2");
hylnk2.NavigateUrl = (currentPageFileName + "?DepartmentID=" + Request.QueryString["DepartmentID"] + "&CategoryID=" + Request.QueryString["CategoryID"] + "&ProductID=" + Request.QueryString["ProductID"] + "&Lang=GER");
HyperLink hylnk3 = (HyperLink)this.Master.FindControl("HyperLink3");
hylnk3.NavigateUrl = (currentPageFileName + "?DepartmentID=" + Request.QueryString["DepartmentID"] + "&CategoryID=" + Request.QueryString["CategoryID"] + "&ProductID=" + Request.QueryString["ProductID"] + "&Lang=ESP");
HyperLink hylnk4 = (HyperLink)this.Master.FindControl("HyperLink4");
hylnk4.NavigateUrl = (currentPageFileName + "?DepartmentID=" + Request.QueryString["DepartmentID"] + "&CategoryID=" + Request.QueryString["CategoryID"] + "&ProductID=" + Request.QueryString["ProductID"] + "&Lang=FR");
}
private void PopulateControls()
{
// Retrieve DepartmentID from the query string
string departmentId = Request.QueryString["DepartmentID"];
// Retrieve CategoryID from the query string
string categoryId = Request.QueryString["CategoryID"];
// Retrieve Page from the query string
string page = Request.QueryString["Page"];
if (page == null) page = "1";
// Retrieve Search string from query string
string searchString = Request.QueryString["Search"];
// How many pages of products?
int howManyPages = 1;
// pager links format
string firstPageUrl = "";
string pagerFormat = "";
// If performing a product search
if (searchString != null)
{
// Retrieve AllWords from query string
string allWords = Request.QueryString["AllWords"];
// Perform search
list.DataSource = CatalogAccess.Search(searchString, allWords,
page, out howManyPages);
list.DataBind();
// Display pager
firstPageUrl = Link.ToSearch(searchString, allWords.ToUpper() == "TRUE", "1");
pagerFormat = Link.ToSearch(searchString, allWords.ToUpper() == "TRUE", "{0}");
}
// If browsing a category...
else if (categoryId != null)
{
// Retrieve list of products in a category
list.DataSource =
CatalogAccess.GetProductsInCategory(categoryId, page, out howManyPages);
list.DataBind();
// get first page url and pager format
firstPageUrl = Link.ToCategory(departmentId, categoryId, "1");
pagerFormat = Link.ToCategory(departmentId, categoryId, "{0}");
}
else if (departmentId != null)
{
// Retrieve list of products on department promotion
list.DataSource = CatalogAccess.GetProductsOnDeptPromo
(departmentId, page, out howManyPages);
list.DataBind();
// get first page url and pager format
firstPageUrl = Link.ToDepartment(departmentId, "1");
pagerFormat = Link.ToDepartment(departmentId, "{0}");
}
else
{
// Retrieve list of products on catalog promotion
list.DataSource =
CatalogAccess.GetProductsOnFrontPromo(page, out howManyPages);
list.DataBind();
// have the current page as integer
int currentPage = Int32.Parse(page);
}
// Display pager controls
// topPager.Show(int.Parse(page), howManyPages, firstPageUrl, pagerFormat, false);
// bottomPager.Show(int.Parse(page), howManyPages, firstPageUrl, pagerFormat, true);
}
// Executed when each item of the list is bound to the data source
protected void list_ItemDataBound(object sender, DataListItemEventArgs e)
{
// obtain the attributes of the product
DataRowView dataRow = (DataRowView)e.Item.DataItem;
string productId = dataRow["ProductID"].ToString();
DataTable attrTable = CatalogAccess.GetProductAttributes(productId);
// get the attribute placeholder
PlaceHolder attrPlaceHolder = (PlaceHolder)e.Item.FindControl("attrPlaceHolder");
// temp variables
string prevAttributeName = "";
string attributeName, attributeValue, attributeValueId;
// current DropDown for attribute values
Label attributeNameLabel;
DropDownList attributeValuesDropDown = new DropDownList();
// read the list of attributes
foreach (DataRow r in attrTable.Rows)
{
// get attribute data
attributeName = r["AttributeName"].ToString();
attributeValue = r["AttributeValue"].ToString();
attributeValueId = r["AttributeValueID"].ToString();
// if starting a new attribute (e.g. Color, Size)
if (attributeName != prevAttributeName)
{
prevAttributeName = attributeName;
attributeNameLabel = new Label();
attributeNameLabel.Text = attributeName + ": ";
attributeValuesDropDown = new DropDownList();
attrPlaceHolder.Controls.Add(attributeNameLabel);
attrPlaceHolder.Controls.Add(attributeValuesDropDown);
}
// add a new attribute value to the DropDownList
attributeValuesDropDown.Items.Add(new ListItem(attributeValue, attributeValueId));
}
}
public void LinkButton1_Click(object sender, EventArgs e)
{
CataloguePageGenerator generator = new CataloguePageGenerator();
try
{
string productId = Request.QueryString["ProductID"].ToString();
string categoryId = Request.QueryString["CategoryID"].ToString();
DataTable dtPSDDetails = new DataTable();
dtPSDDetails = GetGeneric.GetInstance<PDFHelper>().GetALLPDFDetails(productId, categoryId);
if (dtPSDDetails != null && dtPSDDetails.Rows.Count > 0)
{
//generator.Author = dtPSDDetails.Rows[0]["????"].ToString(); <-- get the name of the row returned by the stored procedure
generator.Author = "BBP Plastics";
generator.Image = @".\ProductImages\" + dtPSDDetails.Rows[0]["Image"].ToString();
generator.Subject = "BBP Plastics Product data";
generator.Title = dtPSDDetails.Rows[0]["FPN"].ToString();
generator.CartonDimensions =
"L: " + dtPSDDetails.Rows[0]["CartonLengthmm"].ToString() + " W: " + dtPSDDetails.Rows[0]["CartonWidthmm"].ToString() + " H: " + dtPSDDetails.Rows[0]["CartonHeightmm"].ToString();
generator.FooterImg = @".\..\footer.png";
generator.PackagingSpec =
"Weight of products: " + dtPSDDetails.Rows[0]["WeightOfProductsKilos"].ToString() + "\n\r" +
"Weight of bags: " + dtPSDDetails.Rows[0]["WeightOfBagsKilos"].ToString() + "\n\r" +
"Weight of cardboard: " + dtPSDDetails.Rows[0]["WeightOfCardboardKilos"].ToString() + "\n\r" +
"Total weight of 1 box: " + dtPSDDetails.Rows[0]["Total_WeightKilos"].ToString();
generator.PalletQuantityAndDimensions =
"Cartons per pallet:" + dtPSDDetails.Rows[0]["CartonQuantity"].ToString() + "\n\r" +
"L: " + dtPSDDetails.Rows[0]["FullPalletLengthmm"].ToString() + " W: " + dtPSDDetails.Rows[0]["FullPalletWidthmm"].ToString() + " H: " + dtPSDDetails.Rows[0]["FullPalletHeightmm"].ToString();
generator.ProductDetails =
"Code: " + dtPSDDetails.Rows[0]["ProductCode"].ToString() + "\n\r" +
"Carton quantity: " + dtPSDDetails.Rows[0]["CartonQuantity"].ToString() + "\n\r" +
"Manufactured in: " + dtPSDDetails.Rows[0]["ManufacturedIn"].ToString() + "\n\r" +
"Material: " + dtPSDDetails.Rows[0]["Material"].ToString() + "\n\r" +
"Strength: " + dtPSDDetails.Rows[0]["Strength"].ToString() + "\n\r" +
"Glass washer safe: " + dtPSDDetails.Rows[0]["GlassWasherSafe"].ToString() + "\n\r" +
"Life span: " + dtPSDDetails.Rows[0]["LifeSpan"].ToString() + "\n\r" +
"Top width: " + dtPSDDetails.Rows[0]["TopWidth"].ToString() + "\n\r" +
"Bottom width: " + dtPSDDetails.Rows[0]["BottomWidth"].ToString() + "\n\r" +
"Overall height: " + dtPSDDetails.Rows[0]["OverallHeight"].ToString() + "\n\r" +
"Weight: " + dtPSDDetails.Rows[0]["WeightOfComponentsGrams"].ToString() + "\n\r" +
"Capacity: " + dtPSDDetails.Rows[0]["Capacity"].ToString() + "\n\r" +
"CE Marked: " + dtPSDDetails.Rows[0]["CEMarked"].ToString() + "\n\r" +
"Colour: " + dtPSDDetails.Rows[0]["Colour"].ToString() + "\n\r" +
"Colour Options: " + "\n\r" +
"" + dtPSDDetails.Rows[0]["OtherColoursAvailable"].ToString() + "\n\r" +
"Printable: " + dtPSDDetails.Rows[0]["Printable"].ToString() + "\n\r" +
"Reusable: " + dtPSDDetails.Rows[0]["Reusable"].ToString() + "\n\r" +
"Recyclable: " + dtPSDDetails.Rows[0]["Recyclable"].ToString() + "\n\r" +
"Size Options:" + "\n\r" +
"" + dtPSDDetails.Rows[0]["OtherSizesAvailable"].ToString();
}
}
catch { }
try
{
Document document = generator.CreateDocument();
document.UseCmykColor = true;
// Create a renderer for PDF that uses Unicode font encoding
PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(true);
// Set the MigraDoc document
pdfRenderer.Document = document;
// Create the PDF document
pdfRenderer.RenderDocument();
// Save the PDF document...
//Get the root of the site
String rootPath = Server.MapPath("~");
// build the temp folder and file name (folder temppdfs must exist at root)
rootPath = rootPath + "\\temppdfs\\";
string filename = rootPath + "test" + Guid.NewGuid().ToString("N").ToUpper() + ".pdf";
pdfRenderer.Save(filename);
// ...and start a viewer.
Process.Start(filename);
// Send PDF to browser
MemoryStream stream = new MemoryStream();
document.Save();
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", stream.Length.ToString());
Response.BinaryWrite(stream.ToArray());
Response.Flush();
stream.Close();
Response.End();
}
catch (Exception ex)
{
throw ex;
}
}
}
In this section I get my errors
pdfRenderer.Save(filename);
// ...and start a viewer.
Process.Start(filename);
IT works in visusal studio but not on the website server.
I understand that it has access when running locally
how can I make it work on the server?
I have tried this section
// Send PDF to browser
MemoryStream stream = new MemoryStream();
//document.Save();
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", stream.Length.ToString());
Response.BinaryWrite(stream.ToArray());
Response.Flush();
stream.Close();
Response.End();
I have tried this and when I click the button I get a pdf viewer, but not the PDF I have saved
I desparately neeed this to work, I am not worried if it does not save the PDF, I just want it to come up on screen so it creates it on the fly
HELP
