Convert every page of a pdf file to an image together with a watermark

Hey,


I would like to create a pdf from an office document where each corresponding page is an image. In addition I would like to add a watermark to the pdf and then save each page as an image to the new pdf.

For example, I’m using the following code to convert from document to document, but the input is a document which means an office file before the conversion to pdf. So how and when do I add the watermark ?

<span style=“font-size:10.0pt;font-family:“Courier New”;
color:blue”>public<span style=“font-size:10.0pt;font-family:“Courier New””>
static Document
InsertDocumentAsImages(Document srcDoc)

{

// Load the blank base template to use

Document dstDoc = new Document();

// Reset page margins

dstDoc.FirstSection.PageSetup.LeftMargin = 0;

dstDoc.FirstSection.PageSetup.RightMargin = 0;

dstDoc.FirstSection.PageSetup.TopMargin = 0;

dstDoc.FirstSection.PageSetup.BottomMargin = 0;

DocumentBuilder builder = new DocumentBuilder(dstDoc);

// Iterate through each page in the source document

for (int page = 0; page < srcDoc.PageCount; page++)

{

// Get the page details of the current page.

PageInfo pageInfo = srcDoc.GetPageInfo(page);

// Find the margins of the current page. If the document has sections with different margins

// some extra logic is required here.

PageSetup ps = dstDoc.FirstSection.PageSetup;

// The total size of the vertical margins in the destination document.

double marginHeight = ps.TopMargin + ps.BottomMargin;

// The space free on the page minus margins.

double freeHeight = ps.PageHeight - marginHeight;

// The scale required to insert the page into the destination document and to have it

// fit within the vertical margins.

float scale = (float)(freeHeight / ps.PageHeight);

// Set the resolution of the image here.

float resolution = 96f;

// Get the transformed size

Size pageSize = pageInfo.GetSizeInPixels(scale, resolution);

// Stores the page rendered to image.

MemoryStream imageStream = new MemoryStream();

using (Bitmap img = new Bitmap(pageSize.Width, pageSize.Height))

{

img.SetResolution(resolution, resolution);

using (Graphics gr = Graphics.FromImage(img))

{

// You can apply various settings to the Graphics object.

gr.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;

// Fill the page background.

gr.FillRectangle(Brushes.White, 0, 0, pageSize.Width, pageSize.Height);

// Render the page using the zoom.

srcDoc.RenderToScale(page, gr, 0, 0, scale);

}

img.Save(imageStream, ImageFormat.Bmp);

}

// Insert the page image into the base document.

builder.InsertImage(imageStream);

}

return dstDoc;

}


Thanks !

garkler

Hi Gil,

Once you have converted the Word document to PDF, you may use Aspose.Pdf.Kit to add watermark to the PDF file. You can use PdfFileStamp class for that matter. Please see the following samples to get an idea of different types of watermarks you can add in a PDF file:

Add Text Watermark
Add Image Watermark
Add Background Watermark

I hope this helps. If you have any further questions, please do let us know.
Regards,

Hey Shahzad,

Unfortunatley this does not answer my question,
For a simple conversion i’ve managed to add a watermark, but in the code above the object document is received and then stripped, I want every page saved as an image with the watermark.

With the code above that enables saving each page seperatley the parameter is Document, which means the office document i want to convert to pdf.
Which means that this is before i create the pdf itself.

I hope I managed to explain my question,

Thanks
garkler

Hey,

just so i can make myself clearer, I would like to create something similiar to your evaluation watermark on the pdf.

In addition, i managed to use google-docs to covert the pdf back to text, and this is what i want to avoid by creating each page as an image.

Thanks,
garkler

Hi Garkler,

I’m very sorry, I couldn’t completely understand your requirement. As far as I understand, you’re currently trying to convert a text based Word document to an image based Word document; that is, in the output Word document each page actually contains an image. What I couldn’t understand is that where you want to add the watermark. Do you want to add the watermark on those images created from source Word document? Or, you want to add watermark when the image based (destination) Word document is converted to PDF?

If you could elaborate your requirement with the help of few steps that would be helpful.

We’re sorry for the inconvenience and appreciate your patience and cooperation.
Regards,

Hey,


I’m sorry i wasn’t clear enough.
I would like to convert the word document to pdf including a watermark where every page is an image.

I know this is possible because i would like to reproduce exactly what’s being done if i have the aspose dll’s in evaluation. No matter what I do I get :
"Evaluation Only. Created with Aspose.Words. Copyright 2003-2010 Aspose Pty Ltd."

I would like to use this with my dynamic text

Thanks,
Garkler


Hi Garkler,

As I shared earlier, PdfFileStamp is the only way you can add stamp or watermark to an existing PDF file, whether the PDF contains text or images. However, if you want to add the watermark while converting Word document to PDF using Aspose.Words then please post a query in Aspose.Words forum so that the related team would be able to help you out.

We’re sorry for the inconvenience.
Regards,