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.

Wed Aug 28, 2024 2:33 pm

1. Console app - .net framework 4.7.2
2. Library - FreeSpire.PDF

I am trying to convert a PDF to a image using the below code. It takes too long at the SaveAsImage function.

PdfDocument documemt = new PdfDocument();
documemt.LoadFromFile(Inputfile);
System.IO.Stream stream = documemt.SaveAsImage(0, PdfImageType.Bitmap);

Can you please look into this and help me.

Thank you.

praveenlobo7
 
Posts: 4
Joined: Wed Aug 28, 2024 1:16 pm

Thu Aug 29, 2024 7:25 am

Hello,

Thank you for your inquiry. To help us further investigate it, Please provide us with the original input Pdf document.You can upload here or send it to us via email( support@e-iceblue.com ). Thank you in advance.

Sincerely,
Amin
E-iceblue support team
User avatar

Amin.Gan
 
Posts: 277
Joined: Mon Jul 15, 2024 5:40 am

Tue Sep 03, 2024 4:19 am

Sure, i will send you the file.

I also have one more clarification.

Most of my PDF's are large and when I add the stamp, it's not straight. The stamp is always rotated by 90 degree. How do I get the stamp straight?

My file sizes for your reference:

1. 46.81 × 33.11 in (landscape)
2. 16.54 × 11.69 in (landscape)
3. 23.39 × 16.54 in (landscape)

Can you please help me?

praveenlobo7
 
Posts: 4
Joined: Wed Aug 28, 2024 1:16 pm

Tue Sep 03, 2024 9:21 am

Hello,

Thanks for your inquiry.From your current description, the reason for the 90 degree rotation of the stamp should be due to the rotation angle of the original PDF document page. For this situation, you can first clear the rotation of the page and then draw the stamp. I have attached my code for your reference, you can try running it. If this does not solve your problem, please send us your original input PDF document and test code for investigation.You can upload here or send it to us via email( support@e-iceblue.com ). Thank you in advance.

Code: Select all
PdfDocument document = new PdfDocument();
            // Load the PDF document from the specified file path
            document.LoadFromFile(@"in.pdf");

            PdfDocument newPdf = new PdfDocument();

            for (int i = 0; i < document.Pages.Count; i++)
            {
                PdfPageBase page = document.Pages[i];
                PdfPageBase newPage;
      // Determine if the page has rotated 90 degrees
                if (page.Rotation == PdfPageRotateAngle.RotateAngle90 )
                {
                    // Copy the size of the page
                    newPage = newPdf.Pages.Add(new SizeF( page.Size.Height,page.Size.Width),new PdfMargins(0));
                    newPage.Canvas.DrawTemplate(page.CreateTemplate(), new PointF(0, 0));
                    // Rotate the coordinate system by 90 degrees
                    newPage.Canvas.RotateTransform(-90);
                    newPage.Canvas.TranslateTransform(-page.Canvas.Size.Width, 0);
                    // Create a rubber stamp annotation with a specified rectangle for its size and position
                    PdfRubberStampAnnotation loStamp = new PdfRubberStampAnnotation(new RectangleF(new PointF(10, 10), new SizeF(260, 260)));

                    // Create an instance of PdfAppearance for the rubber stamp annotation
                    PdfAppearance loApprearance = new PdfAppearance(loStamp);

                    // Load an image file to be used as the stamp
                    PdfImage image = PdfImage.FromFile(@"logo.png");

                    // Create a template with specific dimensions
                    PdfTemplate template = new PdfTemplate(210, 210);

                    // Draw the loaded image onto the template
                    template.Graphics.DrawImage(image, 60f, 60f);

                    // Set the normal appearance of the stamp to use the created template
                    loApprearance.Normal = template;

                    // Assign the custom appearance to the rubber stamp annotation
                    loStamp.Appearance = loApprearance;

                    // Add the rubber stamp annotation to the page's annotations widget
                   newPage.AnnotationsWidget.Add(loStamp);
                    // Restores the last state of this Graphics.
                    newPage.Canvas.Restore();
                }
            }
            // Save the modified PDF document
           newPdf.SaveToFile(@"out.pdf", FileFormat.PDF);   

Sincerely,
Amin
E-iceblue support team
User avatar

Amin.Gan
 
Posts: 277
Joined: Mon Jul 15, 2024 5:40 am

Wed Sep 04, 2024 6:53 am

Hi Amin,

This did not work.

I have emailed you the file. Could you please check and get back to me?

Thanks

praveenlobo7
 
Posts: 4
Joined: Wed Aug 28, 2024 1:16 pm

Wed Sep 04, 2024 7:05 am

Hello,

Thank you for sharing the file via email.I have checked the PDF document in your attachment and found that its rotation angle is 0. I tested the following code, and the results showed that it is normal. I have attached my output PDF document for your reference. If the running results on your side are still different, please provide a screenshot of the effect of your result document, and share us with your testing environment (E.g. Win10, 64 bit, .NET 8 ) for further investigation.
Code: Select all
 PdfDocument document = new PdfDocument();
            // Load the PDF document from the specified file path
            document.LoadFromFile(@"SendToSpire.pdf");

            PdfDocument newPdf = new PdfDocument();

            for (int i = 0; i < document.Pages.Count; i++)
            {
                PdfPageBase page = document.Pages[i];
                PdfPageBase newPage;
                if (page.Rotation == PdfPageRotateAngle.RotateAngle90 )
                {
                    // Copy the size of the page
                    newPage = newPdf.Pages.Add(new SizeF( page.Size.Height,page.Size.Width),new PdfMargins(0));
                    newPage.Canvas.DrawTemplate(page.CreateTemplate(), new PointF(0, 0));
                    // Rotate the coordinate system by 90 degrees
                    newPage.Canvas.RotateTransform(-90);
                    newPage.Canvas.TranslateTransform(-page.Canvas.Size.Width, 0);
                    addStamp(newPage);
                    // Restores the last state of this Graphics.
                    newPage.Canvas.Restore();
                }
                // true
                if (page.Rotation == PdfPageRotateAngle.RotateAngle0)
                {
                    newPage = newPdf.Pages.Add(new SizeF(page.Size.Width, page.Size.Height), new PdfMargins(0));
                    newPage.Canvas.DrawTemplate(page.CreateTemplate(), new PointF(0, 0));
                    addStamp(newPage);
                }
            }
            // Save the modified PDF document
           newPdf.SaveToFile(@"out.pdf", FileFormat.PDF);
        }
      
        public static void addStamp(PdfPageBase newPage)
        {
            // Create a rubber stamp annotation with a specified rectangle for its size and position
            PdfRubberStampAnnotation loStamp = new PdfRubberStampAnnotation(new RectangleF(new PointF(10, 10), new SizeF(260, 260)));

            // Create an instance of PdfAppearance for the rubber stamp annotation
            PdfAppearance loApprearance = new PdfAppearance(loStamp);

            // Load an image file to be used as the stamp
            PdfImage image = PdfImage.FromFile(@"logo.png");

            // Create a template
            PdfTemplate template = new PdfTemplate(210, 210);

            // Draw the loaded image onto the template
            template.Graphics.DrawImage(image, 60f, 60f);

            // Set the normal appearance of the stamp to use the created template
            loApprearance.Normal = template;

            // Assign the custom appearance to the rubber stamp annotation
            loStamp.Appearance = loApprearance;

            // Add the rubber stamp annotation to the page's annotations widget
            newPage.AnnotationsWidget.Add(loStamp);

        }

Sincerely,
Amin
E-iceblue support team
User avatar

Amin.Gan
 
Posts: 277
Joined: Mon Jul 15, 2024 5:40 am

Wed Sep 04, 2024 7:31 am

Thanks Amin. I will try this and get back to you.

Just a quick clarification:

Why are we doing this? Creating a new page? can't we just stamp on the existing page?

if (page.Rotation == PdfPageRotateAngle.RotateAngle0)
{
newPage = newPdf.Pages.Add(new SizeF(page.Size.Width, page.Size.Height), new PdfMargins(0));
newPage.Canvas.DrawTemplate(page.CreateTemplate(), new PointF(0, 0));
addStamp(newPage);
}

praveenlobo7
 
Posts: 4
Joined: Wed Aug 28, 2024 1:16 pm

Thu Sep 05, 2024 7:06 am

Hello,

Thanks for your inquiry.If your document does not have a rotation angle, there is no need to create a new page. The method I provided is universal (if there is a rotation angle), and you can make changes according to your actual situation.If you have any other questions, please feel free to write to me.

Sincerely,
Amin
E-iceblue support team
User avatar

Amin.Gan
 
Posts: 277
Joined: Mon Jul 15, 2024 5:40 am

Return to Spire.PDF