Skip to content

Commit 6d6d0be

Browse files
Merge pull request #1644 from syncfusion-content/260329-ug
260329-ug: Added the code sample for create PDF page based on the image size using PdfUnitConverter class
2 parents c97758f + acb477a commit 6d6d0be

File tree

2 files changed

+156
-2
lines changed

2 files changed

+156
-2
lines changed

Document-Processing/PDF/PDF-Library/NET/Working-with-Annotations.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5074,7 +5074,7 @@ The [PdfAnnotationIntent.FreeTextTypeWriter](https://help.syncfusion.com/cr/docu
50745074

50755075
{% tabs %}
50765076

5077-
{% highlight c# tabtitle="C# [Cross-platform]" %}
5077+
{% highlight c# tabtitle="C# [Cross-platform]" playgroundButtonLink="https://raw.githubusercontent.com/SyncfusionExamples/PDF-Examples/master/Annotation/Setting-Annotation-Intent/.NET/Setting-Annotation-Intent/Program.cs" %}
50785078

50795079
using Syncfusion.Drawing;
50805080
using Syncfusion.Pdf;
@@ -5192,7 +5192,7 @@ End Using
51925192

51935193
{% endtabs %}
51945194

5195-
You can download a complete working sample from GitHub.
5195+
You can download a complete working sample from [GitHub](https://github.com/SyncfusionExamples/PDF-Examples/tree/260329/Annotation/Setting-Annotation-Intent/.NET).
51965196

51975197
## Adding comments and review status to the PDF annotation
51985198

Document-Processing/PDF/PDF-Library/NET/Working-with-Images.md

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,160 @@ doc.Close(True)
723723

724724
You can download a complete working sample from [GitHub](https://github.com/SyncfusionExamples/PDF-Examples/tree/master/Images/Add-transparancy-and-rotation-to-the-image/).
725725

726+
## Unit conversion in image position
727+
728+
The [PdfUnitConverter](https://help.syncfusion.com/cr/document-processing/Syncfusion.Pdf.Graphics.PdfUnitConvertor.html) class provides precise measurement conversion capabilities for PDF layouts. When positioning images in a PDF document, the converter translates pixel dimensions to PDF points, enabling millimeter-perfect placement and sizing. This ensures images maintain their aspect ratio while rendering at exact locations and filling designated spaces like rectangles.
729+
730+
The code snippet to illustrate the same is given below.
731+
732+
{% tabs %}
733+
734+
{% highlight c# tabtitle="C# [Cross-platform]" %}
735+
736+
using Syncfusion.Drawing;
737+
using Syncfusion.Pdf;
738+
using Syncfusion.Pdf.Graphics;
739+
740+
//Create a new PDF document
741+
using (PdfDocument document = new PdfDocument())
742+
{
743+
using (FileStream stream = new FileStream("Image.png", FileMode.Open, FileAccess.Read))
744+
{
745+
//Load the image from the disk
746+
PdfBitmap image = new PdfBitmap(stream);
747+
748+
//Add the first section to the PDF document
749+
PdfSection section = document.Sections.Add();
750+
751+
//Initialize unit converter
752+
PdfUnitConverter converter = new PdfUnitConverter();
753+
754+
//Convert the image size from pixel to points
755+
SizeF size = converter.ConvertFromPixels(image.PhysicalDimension, PdfGraphicsUnit.Point);
756+
757+
//Set section size based on the image size
758+
section.PageSettings.Size = size;
759+
760+
// Set section orientation based on the image size (by default Portrait)
761+
if (image.Width > image.Height)
762+
section.PageSettings.Orientation = PdfPageOrientation.Landscape;
763+
764+
//Set a margin for the section
765+
section.PageSettings.Margins.All = 0;
766+
767+
//Add a page to the section
768+
PdfPage page = section.Pages.Add();
769+
770+
//Draw image
771+
page.Graphics.DrawImage(image, 0, 0);
772+
773+
//Save the document
774+
document.Save("Output.pdf");
775+
}
776+
}
777+
778+
{% endhighlight %}
779+
780+
{% highlight c# tabtitle="C# [Windows-specific]" %}
781+
782+
using System.Drawing;
783+
using Syncfusion.Pdf;
784+
using Syncfusion.Pdf.Graphics;
785+
786+
//Create a new PDF document
787+
using (PdfDocument document = new PdfDocument())
788+
{
789+
using (FileStream stream = new FileStream("Image.png", FileMode.Open, FileAccess.Read))
790+
{
791+
//Load the image from the disk
792+
PdfBitmap image = new PdfBitmap(stream);
793+
794+
//Add the first section to the PDF document
795+
PdfSection section = document.Sections.Add();
796+
797+
//Initialize unit converter
798+
PdfUnitConverter converter = new PdfUnitConverter();
799+
800+
//Convert the image size from pixel to points
801+
SizeF size = converter.ConvertFromPixels(image.PhysicalDimension, PdfGraphicsUnit.Point);
802+
803+
//Set section size based on the image size
804+
section.PageSettings.Size = size;
805+
806+
// Set section orientation based on the image size (by default Portrait)
807+
if (image.Width > image.Height)
808+
section.PageSettings.Orientation = PdfPageOrientation.Landscape;
809+
810+
//Set a margin for the section
811+
section.PageSettings.Margins.All = 0;
812+
813+
//Add a page to the section
814+
PdfPage page = section.Pages.Add();
815+
816+
//Draw image
817+
page.Graphics.DrawImage(image, 0, 0);
818+
819+
//Save the document
820+
document.Save("Output.pdf");
821+
}
822+
}
823+
824+
{% endhighlight %}
825+
826+
{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %}
827+
828+
Imports Syncfusion.Pdf
829+
Imports Syncfusion.Pdf.Graphics
830+
Imports System.Drawing
831+
832+
Module Program
833+
Sub Main()
834+
' Create a new PDF document
835+
Using document As New PdfDocument()
836+
837+
' Load the image from disk
838+
Using stream As New FileStream("Image.png", FileMode.Open, FileAccess.Read)
839+
Dim image As New PdfBitmap(stream)
840+
841+
' Add a section to the PDF document
842+
Dim section As PdfSection = document.Sections.Add()
843+
844+
' Initialize unit converter
845+
Dim converter As New PdfUnitConverter()
846+
847+
' Convert image size from pixels to points
848+
Dim size As SizeF = converter.ConvertFromPixels(image.PhysicalDimension, PdfGraphicsUnit.Point)
849+
850+
' Set section size based on image size
851+
section.PageSettings.Size = size
852+
853+
' Set orientation to landscape if image is wider than tall
854+
If image.Width > image.Height Then
855+
section.PageSettings.Orientation = PdfPageOrientation.Landscape
856+
End If
857+
858+
' Remove margins
859+
section.PageSettings.Margins.All = 0
860+
861+
' Add a page to the section
862+
Dim page As PdfPage = section.Pages.Add()
863+
864+
' Draw the image at position (0, 0)
865+
page.Graphics.DrawImage(image, 0, 0)
866+
867+
' Save the document
868+
document.Save("Output.pdf")
869+
End Using
870+
End Using
871+
End Sub
872+
End Module
873+
874+
{% endhighlight %}
875+
876+
{% endtabs %}
877+
878+
You can download a complete working sample from GitHub.
879+
726880
## Converting multi page TIFF to PDF
727881

728882
Multi frame TIFF image can be converted to PDF document. This can be done by accessing each frame of the multi frame TIFF image and rendering it in each page of the PDF document using [PdfBitmap](https://help.syncfusion.com/cr/document-processing/Syncfusion.Pdf.Graphics.PdfBitmap.html) class.

0 commit comments

Comments
 (0)