Hi,
On using the Aspose.pdf component to convert html to pdf. The title is displayed vertically on the pdf.
Code used:
Aspose.Pdf.Generator.Pdf ReportPdf = new Aspose.Pdf.Generator.Pdf();
Aspose.Pdf.Generator.Section section = ReportPdf.Sections.Add();
Aspose.Pdf.Generator.Text CurPage = new Aspose.Pdf.Generator.Text(HtmlContentStringItem);
CurPage.IsFirstParagraph = true;
CurPage.IsHtmlTagSupported = true;
CurPage.UseTextInfoStyle = true;
CurPage.TextInfo.LineSpacing = 1;
section.Paragraphs.Add(CurPage);
ReportPdf.Save(convertedObjstream);
Please find the attachment of input html file and Output pdf document.
Hi Robert,
We are in need of a custom page stitch and that is the reason we use Aspose.Pdf in addition to Aspose.Words which is used for other pdf conversion.
The following is the code we use to stitch the html fragments as pages in to the pdf. But this code fails in conversion of normal html which has been shown on the previous attachments.
MemoryStream convertedObjstream = new MemoryStream();
try
{
Aspose.Pdf.Generator.Pdf ReportPdf = new Aspose.Pdf.Generator.Pdf();
Aspose.Pdf.Generator.Section section = ReportPdf.Sections.Add();
string[] stringSeparators = new string[] { MammoTrackingStringSeperator };
string[] HtmlContentStringList;
HtmlContentStringList = HTMLStr.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries);
foreach (string HtmlContentStringItem in HtmlContentStringList)
{
Aspose.Pdf.Generator.Text CurPage = new Aspose.Pdf.Generator.Text(HtmlContentStringItem);
CurPage.IsFirstParagraph = true;
CurPage.IsHtmlTagSupported = true;
CurPage.UseTextInfoStyle = true;
CurPage.TextInfo.LineSpacing = 1;
section.Paragraphs.Add(CurPage);
}
// This operation is put in try-catch block to handle situations when operation fails for some reason.
try
{
LogInfo(“DocumentConversion.ConvertHTMLStringToPDF - Before : wordDoc.Save(convertedObjstream, saveOptions);”);
ReportPdf.Save(convertedObjstream);
convertedObjstream.Position = 0;
_outPutStream = new byte[convertedObjstream.Length];
LogInfo(“DocumentConversion.ConvertHTMLStringToPDF - Before : convertedObjstream.Read(_outPutStreamBytes, 0, (int)convertedObjstream.Length);”);
convertedObjstream.Read(_outPutStream, 0, (int)convertedObjstream.Length);
if (convertedObjstream != null && convertedObjstream.Length > 0)
{
success = true;
}
}
catch (Exception e)
{
_error = e.Message;
// trap the exeception
string errorMsg = "DocumentConversion - C# ConvertHTMLStringToPDF: Exception :\n\n" + e.Message + "\n\nStack Trace:\n" + e.StackTrace;
LogInfo(errorMsg);
}
}
finally
{
convertedObjstream.Dispose();
GC.Collect();// this is not a good practice but any how since we do a single operation and we need to free up memory we call this method
}
Thanks,
Robert
Hi Robert,
Document doc = new
Document();<o:p></o:p>
StreamReader r = File.OpenText("Test 1.html");
String html = r.ReadToEnd();
string[] stringSeparators = new string[] { "#$NP" };
string[] HtmlContentStringList;
HtmlContentStringList = html.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries);
foreach (string HtmlContentStringItem in HtmlContentStringList)
{
Aspose.Pdf.Page page = doc.Pages.Add();
HtmlFragment htmlFragment = new HtmlFragment(HtmlContentStringItem);
page.Paragraphs.Add(htmlFragment);
}
doc.Save("htmlfragment.pdf");
Hi Robert,
Aspose.Pdf.Document doc = new Aspose.Pdf.Document();
StreamReader r = File.OpenText("Test 1.html");
String html = r.ReadToEnd();
string[] stringSeparators = new string[] { "#$NP" };
string[] HtmlContentStringList;
HtmlContentStringList = html.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries);
foreach (string HtmlContentStringItem in HtmlContentStringList)
{
Aspose.Pdf.Page page = doc.Pages.Add();
HtmlFragment htmlFragment = new HtmlFragment(HtmlContentStringItem);
page.Paragraphs.Add(htmlFragment);
}
doc.Save("htmlfragment.pdf");
Working good:
SaveOptions saveOptions = SaveOptions.CreateSaveOptions(SaveFormat.Pdf);
saveOptions.PrettyFormat = true;
MemoryStream docStream = new MemoryStream(_inputDocumentStream);
MemoryStream convertedObjstream = new MemoryStream();Aspose.Words.Document wordDoc = new Aspose.Words.Document(docStream);
wordDoc.Save(convertedObjstream, saveOptions);
Hi Robert,
as PDFNEWNET-39762 in our issue tracking system. We will
further look into the details of this problem and will keep you updated on the
status of correction. Please be patient and spare us little time. We are sorry
for this inconvenience.
The issues you have found earlier (filed as PDFNET-39762) have been fixed in Aspose.PDF for .NET 21.4.