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.
Sun May 31, 2020 11:36 am
Hi...
I use a spire pdf lib to insert an image and Text on landscape pdf page using this code
- Code: Select all
string ImagePath = Server.MapPath("~/img.jpg");
string PDFPath = Server.MapPath("~/Landscape.pdf");
string OutPDFPath = Server.MapPath("~/Result.pdf");
//Create a pdf document.
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(PDFPath);
PdfPageBase page = doc.Pages[0];
// Create one page
//PdfPageBase page = doc.Pages.Add();
page.Canvas.DrawString("Hello, World!", new PdfFont(PdfFontFamily.Helvetica, 30f), new PdfSolidBrush(Color.Black), 10, 10);
//Draw the image
PdfImage image = PdfImage.FromFile(ImagePath);
float width = image.Width * 0.75f;
float height = image.Height * 0.75f;
float x = (page.Canvas.ClientSize.Width - width) / 2;
page.Canvas.DrawImage(image, x, 60, width, height);
//Save pdf file.
doc.SaveToFile(OutPDFPath);
doc.Close();
//Launching the Pdf file.
System.Diagnostics.Process.Start(OutPDFPath);
The Image and text after insert appear rotate horizontal...
Login to view the files attached to this post.
-

hamzasukkar
-
- Posts: 16
- Joined: Wed Mar 25, 2020 8:47 am
Mon Jun 01, 2020 3:25 am
Hi,
Thanks for your inquiry.
The input document you provided has been rotated 90 degrees, so the image and text after inserting appear rotate horizontal. However, you can refer to the following sample code to rotate it back firstly and then insert them. The attament is my testing result. If there is any other question, just feel free to write back.
- Code: Select all
//load original pdf
PdfDocument doc = new PdfDocument();
doc.LoadFromFile("Landscape.pdf");
//create a new pdf
PdfDocument newPdf = new PdfDocument();
foreach (PdfPageBase page in doc.Pages)
{
PdfPageBase newPage;
//judge if the page rotated
if (page.Rotation == PdfPageRotateAngle.RotateAngle90)
newPage = newPdf.Pages.Add(new SizeF(page.Size.Height, page.Size.Width), new PdfMargins(0));
else if (page.Rotation == PdfPageRotateAngle.RotateAngle270)
newPage = newPdf.Pages.Add(new SizeF(page.Size.Height, page.Size.Width), new PdfMargins(0));
else
newPage = newPdf.Pages.Add(page.Size, new PdfMargins(0));
page.CreateTemplate().Draw(newPage, new PointF(0, 0));
newPage.Canvas.DrawString("Hello, World!", new PdfFont(PdfFontFamily.Helvetica, 30f), new PdfSolidBrush(Color.Black), 10, 10);
//load the image
PdfImage image = PdfImage.FromFile("008.JPG");
float width = image.Width * 0.75f;
float height = image.Height * 0.75f;
float x = (page.Canvas.ClientSize.Width - width) / 2;
newPage.Canvas.DrawImage(image, x, 60, width, height);
}
newPdf.SaveToFile("Result.pdf", Spire.Pdf.FileFormat.PDF);
Sincerely,
Sara
E-iceblue support team
Login to view the files attached to this post.
-


Sara.Yang
-
- Posts: 33
- Joined: Wed May 06, 2020 1:05 am
Tue Jun 02, 2020 9:09 am
Thank You very much it's work.......
-

hamzasukkar
-
- Posts: 16
- Joined: Wed Mar 25, 2020 8:47 am
Tue Jun 02, 2020 9:14 am
Hello,
Glad to hear that!
If you have further questions, please feel free to let us know.
Have a nice day!
Sincerely,
Rachel
E-iceblue support team
-


rachel.lei
-
- Posts: 1571
- Joined: Tue Jul 09, 2019 2:22 am