Memory issue when saving doc as PDF

Technical support for Spire.Doc

Moderator: iceblue support

Memory issue when saving doc as PDF

Postby jslots » Thu Jan 10, 2013 10:54 am

Dear E-IceBlue,

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
jslots
 
Posts: 48
Joined: Mon Jul 09, 2012 12:03 pm

Re: Memory issue when saving doc as PDF

Postby harry.support » Fri Jan 11, 2013 8:10 am

Dear Johan,

Sorry for this inconvenience caused by us.
We have reproduce your two issues. And the issue #1 has been fixed. We are working on #2. We plan to release a new hotfix of Spire.Office next week, it will fix your two issues. We will inform you immediately after releasing.

Regards,
Harry
Technical Support / Developer,
e-iceblue Support Team
User avatar
harry.support
 
Posts: 180
Joined: Mon Nov 08, 2010 3:11 pm

Re: Memory issue when saving doc as PDF

Postby jslots » Fri Jan 11, 2013 8:20 am

Thank you Harry,

I'm looking forward to the fixes. Thanks for your quick reply.

Have great day,
Johan
jslots
 
Posts: 48
Joined: Mon Jul 09, 2012 12:03 pm

Re: Memory issue when saving doc as PDF

Postby amy.zhao » Fri Jan 18, 2013 6:51 am

Hello Johan,

Thank you for waiting.
The new hotfix of spire.office which fixes your two issues has been released, please download Spire.Office Platinum (Hot Fix) Version:2.4.5 from http://www.e-iceblue.com/Download/download-office-for-net-now/spireoffice-platinum-hot-fix245.html?Itemid=0. Please use it to test your issues and tell us your test result. Thank you!
If you encounter any problem, please tell us.

Best regards,
Amy
E-iceblue support team
User avatar
amy.zhao
 
Posts: 2251
Joined: Wed Jun 27, 2012 8:50 am

Re: Memory issue when saving doc as PDF

Postby amy.zhao » Tue Jan 22, 2013 6:47 am

Hello Johan,

Did spire.office_2.4.5 fix all your problems? Could you please tell us your test result? Thank you!
If you encounter other problem, please don't hesitate to contact us.

Have a great day!

Best regards,
Amy
E-iceblue support team
User avatar
amy.zhao
 
Posts: 2251
Joined: Wed Jun 27, 2012 8:50 am

Re: Memory issue when saving doc as PDF

Postby jslots » Tue Jan 22, 2013 9:47 am

Hi Amy,

The hot fix is improving the memory-use, but it is still not what i expected. Saving the document as DOC is going fast and memory-usage is pretty low. Saving as PDF, with the new fix, is using significant more memory than the DOC, but less than the previous version. Also it is still taking several minutes to save the document. Creating the document is pretty fast, but saving is really slow!

When i increase the number of pages in the document to 1000 (in stead of 250 pages) it is still using so much memory that i get an out of memory exception (Usage is >1400 MB), and 1 CPU is continuously in use.

So, it looks like a little improvement, but i'm still having some issues on large documents, with memory-usage and very large saving-times.

Can you try to reproduce this?

Thanks in advance,
Johan
jslots
 
Posts: 48
Joined: Mon Jul 09, 2012 12:03 pm

Re: Memory issue when saving doc as PDF

Postby amy.zhao » Wed Jan 23, 2013 2:15 am

Hello Johan,

We are sorry that spire.office_2.4.5 didn't fix your problems completely.
Could you please share a large document? It will help us to reproduce your problem and solve it.
Thank you!

Best regards,
Amy
E-iceblue support team
User avatar
amy.zhao
 
Posts: 2251
Joined: Wed Jun 27, 2012 8:50 am

Re: Memory issue when saving doc as PDF

Postby jslots » Wed Jan 23, 2013 10:03 am

Hi Amy,

Do you want the code to create the document, or the document it self?
I created just a document with dummy-data in it, but i cant attach it. The document is to large to upload to your forum. (its > 256 kB)

I created a document with only 100 pages because of the size of the document, just to show you how my document looks like.

Do you need any further information?
Johan
You do not have the required permissions to view the files attached to this post.
jslots
 
Posts: 48
Joined: Mon Jul 09, 2012 12:03 pm

Re: Memory issue when saving doc as PDF

Postby amy.zhao » Thu Jan 24, 2013 4:04 am

Hello Johan,

Thanks for your document.
We created a document with 1000 pages according to your document and save it as PDF, but we don't reproduce your problem on
our side. Please make sure that Platform Target property of your project is Any CPU and Platform property of your project is also Active(Any CPU)(attach a new screenshot to point them them).
If you still have the problem, please tell us.

Best regards,
Amy
E-iceblue support team
You do not have the required permissions to view the files attached to this post.
User avatar
amy.zhao
 
Posts: 2251
Joined: Wed Jun 27, 2012 8:50 am

Re: Memory issue when saving doc as PDF

Postby jslots » Thu Jan 24, 2013 9:09 am

Hello Amy,

I checked the settings you provided me, but i was already using these settings.
So this is not the solution for my problem.

Do you have any other suggestions?

Thanks in advance,
Johan
jslots
 
Posts: 48
Joined: Mon Jul 09, 2012 12:03 pm

Re: Memory issue when saving doc as PDF

Postby amy.zhao » Thu Jan 24, 2013 9:55 am

Hello Johan,

Thanks for your quick reply.
We are sorry that the solution don't solve your problem.
Could you please tell us your system environment and system language?
We will try to create the same system environment as the one you encountered the problem on to reproduce your problem.
Thank you!

Best regards,
Amy
E-iceblue support team
User avatar
amy.zhao
 
Posts: 2251
Joined: Wed Jun 27, 2012 8:50 am

Re: Memory issue when saving doc as PDF

Postby jslots » Thu Jan 24, 2013 10:07 am

Hi Amy,

I am using Windows 7 professional (Version 6.1, Build 7601: SP1), 64-bit Operating system. Systemlanguage is English.
Besides that, i am using Visual studio 2010 (version 10.0.40219.1 SP1)

Do you need any other information?

Johan
jslots
 
Posts: 48
Joined: Mon Jul 09, 2012 12:03 pm

Re: Memory issue when saving doc as PDF

Postby amy.zhao » Fri Jan 25, 2013 6:10 am

Hello Johan,

Thanks for your information.
We are creating the system environment and will try our best to reproduce your problem. We will tell you as soon as have any progress.

Thanks & Best regards,
Amy
E-iceblue support team
User avatar
amy.zhao
 
Posts: 2251
Joined: Wed Jun 27, 2012 8:50 am

Re: Memory issue when saving doc as PDF

Postby amy.zhao » Thu Jan 31, 2013 10:01 am

Hello Johan,

We had finished the tests, but we still don't reproduce your problem.
We just released a new hotfix of Spire.Doc. Please try to download Spire.Doc _4.6.15 from the link http://www.e-iceblue.com/downloads/hot_fix/spire.doc_hotfix_4.6.15.zip and test it.
If you still have the problem, please tell us.

Thanks & Best Regards,
Amy
E-iceblue support team
User avatar
amy.zhao
 
Posts: 2251
Joined: Wed Jun 27, 2012 8:50 am

Re: Memory issue when saving doc as PDF

Postby jslots » Thu Feb 14, 2013 2:52 pm

Hi Amy,

Using the new fix is improving the performance a little bit. Out of memory issue is not detected anymore, but it still claims almost all free memory that is left, and it takes minutes to create a large document.

Besides this, i have another 2 issue i would like to share:

1) When i add a footerdistance to the pagesetup (section.PageSetup.FooterDistance = 5;) this only affected the first page of the worddocument.

Difference in footerdistance:
capture_02142013_144836.jpg



2) As you can see in the code, i added a border to the top of the table in the footer( tr2.RowFormat.Borders.Top.BorderType = BorderStyle.Single;)
The border is not displayed in the Word-document. The border IS displayed in de pdf.

Pdf-file:
capture_02142013_145000.jpg



Can you please try to reproduce these issues?
Thanks in advance,
Johan
You do not have the required permissions to view the files attached to this post.
jslots
 
Posts: 48
Joined: Mon Jul 09, 2012 12:03 pm

Next

Return to Spire.Doc

Who is online

Users browsing this forum: No registered users and 0 guests