Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Fri Jul 19, 2019 1:25 pm

So, when trying to merge multiple PDF's into a single pdf, where both pdf's exist in memory, both have pages, both have settings, in all cases, AppendPage and InsertPage throw error, object not set to an instance of an object, here is my large grouping of code that includes every type of direction I tried, with remarks, in c# using ASP.Net, please also note, that if you want to try the pdfdoc[I], you have to get rid of the check if I==0 block or comment it out all together, let me know if you need me to post each type of try in different posts.
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
//====================================================================================================


JonathanWood
 
Posts: 26
Joined: Fri Feb 26, 2016 2:19 am

Mon Jul 22, 2019 5:59 am

Hi,

Thanks for your inquiry and sorry for late reply as weekend.
I did an initial test with the latest Spire.PDF Pack Version:5.7, but didn't encounter the exception when executing AppendPage and InsertPage. I would suggest that you download the latest version and refer to the following simply way to merge multiple PDF documents. If your issue still happens, please provide your HTML strings to help us further look into your case. Meanwhile, the autoDetectPageBreak property allows detect page break automatically. Please set it to be true.
Code: Select all
List<string> htmlCode = new List<string>();
htmlCode.Add(File.ReadAllText("htmlString1.txt"));
htmlCode.Add(File.ReadAllText("htmlString2.txt"));
htmlCode.Add(File.ReadAllText("htmlString3.txt"));

PdfDocument[] pdfdoc = new PdfDocument[htmlCode.Count];
PdfHtmlLayoutFormat htmlLayoutFormat = new PdfHtmlLayoutFormat(); //useless
PdfPageSettings pdfSettings = new PdfPageSettings();
pdfSettings.Width = 1205;
pdfSettings.Height = 800;
pdfSettings.Orientation = PdfPageOrientation.Landscape;
htmlLayoutFormat.Layout = Spire.Pdf.Graphics.PdfLayoutType.Paginate;
htmlLayoutFormat.IsWaiting = false;

for (int i = 0; i < htmlCode.Count; i++)
{
    pdfdoc[i] = new Spire.Pdf.PdfDocument();
    pdfdoc[i].PageSettings = pdfSettings;
    Thread thread = new Thread(() => { pdfdoc[i].LoadFromHTML(htmlCode[i], true, pdfSettings, htmlLayoutFormat); });
    thread.SetApartmentState(ApartmentState.STA);
    thread.Start();
    thread.Join();
}

//merge PDF documents
for (int i = 1; i < htmlCode.Count; i++)
{
    pdfdoc[0].AppendPage(pdfdoc[i]);
}

string reportid = Guid.NewGuid().ToString();
pdfdoc[0].SaveToHttpResponse(reportid + ".pdf", this.Response, HttpReadType.Save);

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1385
Joined: Tue Sep 27, 2016 1:06 am

Mon Jul 22, 2019 1:36 pm

I have sent you a response email on this.
Until the htmlLayoutFormat works, as in until this works:
Code: Select all
 PdfHtmlLayoutFormat htmlLayoutFormat = new PdfHtmlLayoutFormat();
htmlLayoutFormat.Layout = Spire.Pdf.Graphics.PdfLayoutType.Paginate; // does not work
htmlLayoutFormat.FitToHtml = Clip.Both; // does not work
htmlLayoutFormat.IsWaiting = false;
// the pdfSettings correctly sets to Letter size but is useless with PdfHtmlLayoutFormat not working
PdfPageSettings pdfSettings = new PdfPageSettings();
pdfSettings.Size= PdfPageSize.Letter;



page break is incorrect because the page isn't downscaled to fit 8 1/2 by 11, no scaling or clipping happens, so the page breaks are wrong.
that is with the most recent build of Spire.PDF and hasn't been fixed since the version over a year ago.

JonathanWood
 
Posts: 26
Joined: Fri Feb 26, 2016 2:19 am

Tue Jul 23, 2019 3:17 am

Dear Jonathan,

Thanks for your feedback.
The issue that the htmlLayoutFormat setting (FitToPage, TrimPage, FitToHtml) didn't work has already been posted to our Dev team for investigating and fixing. In the meanwhile, I also notice the Paginate property didn’t take effect. It has been posted to our Dev team too. If there is any update, I will inform you immediately.
In addition, I suggest that you try the Plugin method to do the conversion as the method has a better performance. Did you try my testing application on your side, will the issue happen? Thanks in advance for your feedback.

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1385
Joined: Tue Sep 27, 2016 1:06 am

Tue Jul 23, 2019 12:43 pm

I tried your code, I added the folder to my web server, made it a web app, ran it, clicked the button and got the following error:
Server Error in '/HTMLtoPDF' Application.
--------------------------------------------------------------------------------

The conversion provider cann't be found.
Please make sure:
1. the folder "plugins" under the same folder of Spire.Pdf.dll
2. exists "plugins\HTMLConverter.dll" and other "plugins\*.dll"
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The conversion provider cann't be found.
Please make sure:
1. the folder "plugins" under the same folder of Spire.Pdf.dll
2. exists "plugins\HTMLConverter.dll" and other "plugins\*.dll"



I verified that you have the HTMLConverter.dll in the plugins folder, I made zero changes to your example in your zip, all I did was unzip it, put it in the wwwroot folder, and told IIS it was an application so that it would load correctly. I have also checked all libraries to make sure nothing was blocked. Your project simply doesn't work. I suspect the real problem is the exact same problem I have that I mentioned before, there is no overload for the stream type.

JonathanWood
 
Posts: 26
Joined: Fri Feb 26, 2016 2:19 am

Wed Jul 24, 2019 3:15 am

Hi,

Thanks for your feedback.
When deploying the application on IIS, please set "Enable 32-Bit Applications" to be True in your IIS Application Pools Settings. After setting, I can deploy my application on IIS successfully. Please have a test on your side. If the issue still happens, please provide the following information to help us further investigate your issue.
1. If you don't deploy the application on IIS, just run it on local, will the issue happen?
2. Please provide your testing environment, such as Server 2012 R2.
3. Please share some screenshots showing your IIS settings.
I'm looking forward to your reply.

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1385
Joined: Tue Sep 27, 2016 1:06 am

Return to Spire.PDF

cron