- Code: Select all
//====================================================================================================
// use Spire.PDF to dump to user
List<string> htmlCode = (List<string>)Session.Contents["reportcontents"];
PdfDocument pdfReport = new PdfDocument();
PdfDocument pdfTemp = new PdfDocument();
PdfDocument[] pdfdoc = new PdfDocument[htmlCode.Count];
PdfHtmlLayoutFormat htmlLayoutFormat = new PdfHtmlLayoutFormat(); //useless
PdfPageSettings pdfSettings = new PdfPageSettings();
//pdfSettings.Size= PdfPageSize.Letter; // want this but since scaling does not work have to force larger size
pdfSettings.Width = 1205; // had to use to get the results to show in pdf
pdfSettings.Height = 800;
pdfSettings.Orientation = PdfPageOrientation.Landscape;
pdfReport.PageSettings = pdfSettings;
//htmlLayoutFormat.FitToPage = Clip.Width; // does not work
//htmlLayoutFormat.TrimPage = Clip.Width; // does not work
htmlLayoutFormat.Layout = Spire.Pdf.Graphics.PdfLayoutType.Paginate; // does not work
htmlLayoutFormat.FitToHtml = Clip.Both; // does not work
htmlLayoutFormat.IsWaiting = false;
// ok so the pdfReport needs the first item always so....
for (int i = 0; i < htmlCode.Count; i++)
{
if (i == 0)
{
Thread thread = new Thread(() => { pdfReport.LoadFromHTML(htmlCode[i], false, pdfSettings, htmlLayoutFormat); });
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
}
else
{
//pdfdoc[i] = new PdfDocument();
//Thread thread = new Thread(() => { pdfdoc[i].LoadFromHTML(htmlCode[i], false, pdfSettings, htmlLayoutFormat); });
Thread thread = new Thread(() => { pdfTemp.LoadFromHTML(htmlCode[i], false, pdfSettings, htmlLayoutFormat); });
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
// still get "object not set to an instance of an object" when we have a document and it has pages...
// both pdfReport and pdfTemp exist, both have pages, but AppendPage and InsertPage give error
pdfReport.InsertPage(pdfTemp,0); // keeps blowing up, no matter if i use AppendPage or InsertPage
}
}
//===============================================================================================
// tried to do it here instead, outside of the pull loop above
//===============================================================================================
//for (int i = 0; i < pdfdoc.Count(); i++)
//{
// // getting object not set to an instance of an object for the pdfReport
// // but it exists and has zero pages, and pdfdoc[#] exists and has 1 page
// // so if pdfdoc has 4 documents in its collection, pdfdoc has a length of 4
// // base 0 to 3,
// // so why are we getting object not set to an instance of an object here?
// //==========================================================================
// // lets just pull out the first page as a page in page base and see
// PdfPageBase newPage = pdfdoc[i].Pages[0];
// if (i == 0)
// {
// pdfReport.AppendPage(pdfdoc[i]);
// }
// else
// {
// pdfReport.InsertPage(pdfdoc[i], 0);
// }
//}
//MemoryStream msDoc = new MemoryStream();
//pdfReport.SaveToStream(msDoc, FileFormat.PDF);
//pdfdoc.SaveToStream(msDoc, FileFormat.PDF);
string reportid = Guid.NewGuid().ToString();
//pdfdoc.SaveToHttpResponse(reportid + ".pdf", this.Response, HttpReadType.Save);
pdfReport.SaveToHttpResponse(reportid + ".pdf", this.Response, HttpReadType.Save);
// end output to PDF for the user
//====================================================================================================