1) I have an issue when creating a large document and try to save it as PDF.
I made a dummy project. In this project i reproduced the problem. When i create a large document (~250 pages, every page has a table with 30 rows) the problem is only seen when saving as PDF. Saving as Doc/Docx is working like it should be. Fast and not much memory is used.
When i save the same document as PDF, it takes minutes to execute the document.savetostream() method. My CPU-load uses 1 Core to the maximum, and my memory-usage is raising skyhigh, which results in a out-of-memory-exception, and the process takes minutes!
There is no difference between saving to stream or saving to file.
I use spire.office-pack 2.4.4.
Here is my code of the dummy-project:
- Code: Select all
internal class Program
{
private static void Main(string[] args)
{
bool pdf = true;
Spire.License.LicenseProvider.SetLicenseFileName("license.elic.xml");
string filename = string.Format(@"C:\Temp\EDP\Tempdir\test.{0}", pdf ? "pdf" : "docx");
Export(filename, pdf);
Console.Write("\nFinished");
Console.ReadKey();
}
public static void Export(string fileName, bool pdf)
{
using (FileStream fs = new FileStream(fileName, FileMode.Create))
{
Document d = new Document();
bool headerfooter = true;
for (int i = 0; i < 250; i++)
{
Console.Write("\rPage:" + i );
Section section1 = CreateSection(d);
if(headerfooter)
{
InsertHeader(section1);
InsertFooter(section1,pdf);
}
headerfooter=false;
Table table = section1.AddTable(true);
for (int j = 0; j < 30; j++)
{
TableRow tr = AddNewDetailsRow(table, section1.PageSetup.ClientWidth);
tr.Cells[0].AddParagraph().AppendText(string.Format("This also a dummy string to test the use of memory {0},{1}", j, i));
tr.Cells[1].AddParagraph().AppendText(string.Format("This is a very long dummy string-value to test the memory-use of the application. Just to check how the memory-use behaves {0},{1}", i, j));
}
section1.AddParagraph().AppendText("");
}
FileFormat format = pdf ? FileFormat.PDF : FileFormat.Docx2010;
Console.WriteLine("\nSaving the document to stream");
d.SaveToStream(fs, format);
}
}
private static Section CreateSection(Document d)
{
if (d == null) return null;
Section section1 = d.AddSection();
section1.PageSetup.PageSize = PageSize.A3;
section1.PageSetup.Orientation = PageOrientation.Portrait;
return section1;
}
private static TableRow AddNewDetailsRow(Table table, float pagewidth)
{
TableRow tr = table.AddRow(2);
tr.Cells[0].Width = pagewidth / 10 * 3;
tr.Cells[1].Width = pagewidth / 10 * 7;
tr.Cells[0].CellFormat.Borders.Left.BorderType = BorderStyle.None;
tr.Cells[tr.Cells.Count - 1].CellFormat.Borders.Right.BorderType = BorderStyle.None;
return tr;
}
private static void InsertFooter(Section section, bool pdf)
{
HeaderFooter footer = section.HeadersFooters.Footer;
List<TextRange> textRanges = new List<TextRange>();
Table t = footer.AddTable(false);
TableRow tr1 = t.AddRow(3);
TableRow tr2 = t.AddRow(3);
float f = (section.PageSetup.ClientWidth)/3;
tr1.Cells[0].Width = f;
tr1.Cells[1].Width = f;
tr1.Cells[2].Width = f;
tr2.Cells[0].Width = f;
tr2.Cells[1].Width = f;
tr2.Cells[2].Width = f;
tr2.RowFormat.Borders.Top.BorderType = BorderStyle.Single;
//insert datetime in footer
Paragraph footerParagraphLeft = tr2.Cells[0].AddParagraph();
footerParagraphLeft.Format.HorizontalAlignment = HorizontalAlignment.Left;
textRanges.Add(footerParagraphLeft.AppendText(DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss")));
//insert pagenumber in footer
Paragraph footerParagraphRight = tr2.Cells[2].AddParagraph();
textRanges.Add(footerParagraphRight.AppendField("page number", FieldType.FieldPage));
textRanges.Add(footerParagraphRight.AppendText(" van "));
textRanges.Add(footerParagraphRight.AppendField("number of pages", FieldType.FieldNumPages));
footerParagraphRight.Format.HorizontalAlignment = HorizontalAlignment.Right;
Paragraph footerCenter = tr2.Cells[1].AddParagraph();
Image image = Image.FromFile(@"C:\temp\logo_medium.png");
DocPicture footerPicture = footerCenter.AppendPicture(image);
footerPicture.TextWrappingStyle = TextWrappingStyle.InFrontOfText;
float x = pdf ? f : 0;
footerPicture.HorizontalPosition = (section.PageSetup.ClientWidth/2) - (image.Width/2) - x;
footerPicture.VerticalOrigin = VerticalOrigin.Paragraph;
footerPicture.VerticalAlignment = ShapeVerticalAlignment.Top;
footerPicture.WidthScale = 50;
footerPicture.HeightScale = 50;
}
private static void InsertHeader(Section section)
{
//insert picture and text to header
HeaderFooter header = section.HeadersFooters.Header;
Paragraph titleParagraph = header.AddParagraph();
Paragraph subtitleParagraph = header.AddParagraph();
//border
subtitleParagraph.Format.Borders.Bottom.BorderType = BorderStyle.Single;
//Fill header with text
TextRange title = titleParagraph.AppendText("Title");
TextRange subtitle = subtitleParagraph.AppendText("Subtitle");
titleParagraph.Format.HorizontalAlignment = HorizontalAlignment.Left;
subtitleParagraph.Format.HorizontalAlignment = HorizontalAlignment.Left;
//header picture on right top of the page.
Paragraph p = header.AddParagraph();
DocPicture headerPicture = p.AppendPicture(Image.FromFile(@"C:\temp\logo_medium.png"));
headerPicture.TextWrappingStyle = TextWrappingStyle.InFrontOfText;
headerPicture.HorizontalOrigin = HorizontalOrigin.Margin;
headerPicture.HorizontalAlignment = ShapeHorizontalAlignment.Right;
headerPicture.VerticalOrigin = VerticalOrigin.Page;
headerPicture.VerticalPosition = section.PageSetup.Margins.Top;
headerPicture.HeightScale = 100;
headerPicture.WidthScale = 100;
}
}
You can adjust the boolean in the first row of the code to see the difference between PDF and docx.
- Code: Select all
bool pdf = true;
I tried to disable the footer and header, but that makes no difference. I thought is might be a problem with the images, but that was not the problem.
2) Besides this issue there is an issue with placing a image in the footer of the page. This works fine when saving as doc, but there is positioning issue when saving as PDF. As you can see in code, i added a table with 3 columns because i want to add 3 fields to the footer, at the same vertical position. The cells all have the same width.
When adding the image to the second column, it is not placed on the position i would expect (saving as doc results in the right position!)
The image is placed to much to the right of the page. When i subtract the width of the first column, the position is right.
- Code: Select all
float x = pdf ? f : 0;
footerPicture.HorizontalPosition = (section.PageSetup.ClientWidth/2) - (image.Width/2) - x;
this is a work around i found myself, but it would be nice if you fix this problem too.
Can you try to reproduce these problems?
Thanks in advance,
Johan