Welcome Guest Search | Active Topics | Sign In | Register

lowering/raising images Options
micahhoover
Posted: Monday, November 21, 2011 4:37:06 PM
Rank: Newbie
Groups: Member

Joined: 11/21/2011
Posts: 5
I would like to be able to open an existing PDF and insert a background image (so the text is drawn over the image).

Is that possible with EO?

If so, how would I do that?

Thanks!


Micah
eo_support
Posted: Monday, November 21, 2011 5:06:23 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
Hi,

That is technically doable but often does not make any difference because most PDF file starts with a solid white background. So while technically you can put an image below the existing content, you would be putting the image below the solid white background that covers the whole page, thus completely covers your image.

If your PDF file does not have a solid background (If you create the PDF file with EO.Pdf, then files created through HTML to PDF interface always have solid background; files created through PDF Creator interface do not have solid background), then you can insert background image with the following code:

Code: C#
using EO.Pdf;
using EO.Pdf.Acm;
using EO.Pdf.Contents;
using EO.Pdf.Drawing;

//Load the existing pdf file
PdfDocument doc = new PdfDocument(pdfFileName);

//Load the image
PdfImage image = new PdfImage(imageFileName);
PdfImageContent imageContent = new PdfImageContent(image);

//Position the image. The following code positions
//the image at the top left corner of the page 
//and set the image size to be 1 inch by 1 inch. 
//See the link below for more details about 
//how to position a PdfImageContent object
imageContent.GfxMatrix.Translate(72, 648);
imageContent.GfxMatrix.Scale(72, 72);

//Insert the image at the bottom of the page
doc.Pages[0].Contents.Insert(0, imageContent);

//Save the image file
doc.Save(pdfFileName);


How to position a PdfImageContent object:
http://doc.essentialobjects.com/library/4/acm/pdf%20content%20api/image.aspx

Note that the above code only works if you already have a license. Without a license, EO.Pdf will try to render a license warning message. That code conflicts with the above code that replaces the bottom layer of the page contents and will cause an exception.

Hope this helps. Please let us know if you have any questions.

Thanks!
micahhoover
Posted: Tuesday, November 22, 2011 9:33:03 AM
Rank: Newbie
Groups: Member

Joined: 11/21/2011
Posts: 5
Thanks eo_support.

Your product is less expensive than the alternatives I am looking at, so I'd like to see this work.

On the doc.Save line I get this exception:

Unable to cast object of type 'EO.Pdf.Contents.PdfImageContent' to type 'EO.Pdf.Contents.PdfRawContent'.

The menu label is: "InvalidCastException".

Here is my source code:

Quote:
PdfImage image = new PdfImage(@"C:\projects\image.jpg");
PdfImageContent imageContent = new PdfImageContent(image);

imageContent.GfxMatrix.Translate(72, 648);
imageContent.GfxMatrix.Scale(72, 72);

PdfDocument doc = new PdfDocument(@"C:\projects\temp.pdf");
doc.Pages[0].Contents.Insert(0, imageContent);
doc.Save(@"C:\projects\temp_eo_output.pdf");

eo_support
Posted: Tuesday, November 22, 2011 10:18:47 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
Hi,

I have already told you this in my orginal reply:

Quote:
Note that the above code only works if you already have a license. Without a license, EO.Pdf will try to render a license warning message. That code conflicts with the above code that replaces the bottom layer of the page contents and will cause an exception.


The error will go away once you apply the license key.

Thanks
micahhoover
Posted: Tuesday, November 22, 2011 10:56:43 AM
Rank: Newbie
Groups: Member

Joined: 11/21/2011
Posts: 5
I can't see us paying for a license if we can't see demonstrated success with our intended use case.
eo_support
Posted: Tuesday, November 22, 2011 11:04:24 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
Hi,

We will see if we can give you a temp license or if we can modify our code so that the above code will work without a license.

As already mentioned, the code will not work for at all if you have solid white background. If that’s the case, then no matter whose product you use, your only choice is to put image on top of the existing content instead of below it. So you may want to try putting image on top (change the code from Insert(0, imageContent); to Add(imageContent)) while you are waiting for us. That will work much nicer and more reliable for you if your image supports transparency.

Thanks!
eo_support
Posted: Tuesday, November 22, 2011 12:26:32 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
Hi,

We have posted a new build that should allow you to try the above code without a license. Please see your private message for the download location.

Thanks!
micahhoover
Posted: Tuesday, November 22, 2011 5:16:12 PM
Rank: Newbie
Groups: Member

Joined: 11/21/2011
Posts: 5
Hmm ... that seems to work and it's $251 cheaper than the Aspose version.

I suppose we will end up buying the full version if our client likes the application we're making.
eo_support
Posted: Tuesday, November 22, 2011 5:27:28 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
Glad to hear that. Ours is not only cheaper, but far better. People are always "shocked" when they first see how good our HTML to PDF converter is. : ) Internally the converter uses the same interface the above code uses.

Please feel free to let us know if you need anything else.
micahhoover
Posted: Tuesday, November 22, 2011 5:28:32 PM
Rank: Newbie
Groups: Member

Joined: 11/21/2011
Posts: 5
Just ran into another wall ...

When I try the code as you originally posted:

Quote:
PdfDocument doc = new PdfDocument(@"C:\projects\temp.pdf");
doc.Pages[0].Contents.Insert(0, imageContent);


It works.

But if I want it to go to all pages ...

Quote:
int pages = doc.Pages.Count;
for (int i = 0; i < pages; i++)
{
PdfImage image2 = new PdfImage(@"C:\projects\image.jpg");
PdfImageContent imageContent2 = new PdfImageContent(image);
doc.Pages[i].Contents.Insert(0, imageContent2); // <---- notice the iteration here
}


The image fails to show up as a background on any pages in the PDF file.

Any suggestions?

Micah
eo_support
Posted: Tuesday, November 22, 2011 5:33:51 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
Hi,

Your code looks perfectly fine. As we have pointed out from the very begining, if your existing content has a solid background (it's possible that the first page is transparent and the second page is not), then it won't work. So that's the main suspect here. If you are not sure, send the PDF file to us and we will be happy to take a look. We will PM you the email address as to where to send.

Thanks!


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.