Free Support Forum - groupdocs.com

Customising the watermark based on page orientation


#1

Hi, I’m looking to convert office type files and PDFs to PDF/A. As part of the conversion process a watermark is going to be added.

I’d like to customise the watermark based on the page orientation of the document being converted. For example, if the document is in portrait, then have the watermark angle set at -55, but if the page orientation is landscape then have the watermark angle set to -35.

Here is some sample code I’m using;

byte[] convertedBytes;

using (MemoryStream originalDocument = new MemoryStream(documentRequest.Content))
{
	var saveOptions = new PdfSaveOptions();
	saveOptions.PdfOptions.PdfFormat = PdfOptions.PdfFormatType.PdfA_1A;
	saveOptions.WatermarkOptions.Text = "WATERMARK TEXT";
	saveOptions.WatermarkOptions.Transparency = 0.75;
	saveOptions.WatermarkOptions.Color = Color.Gray;
	saveOptions.WatermarkOptions.Font = new Font("Arial Narrow", 53, FontStyle.Bold);

	// want to customise these properties depending on the page orientation
	saveOptions.WatermarkOptions.RotationAngle = -55;
	saveOptions.WatermarkOptions.Left = 35;
	saveOptions.WatermarkOptions.Top = 725;
	
	var convertedDocumentStream = conversionHandler.Convert(originalDocument, saveOptions);

	using (var convertedDocument = new MemoryStream())
	{
		convertedDocumentStream.Save(convertedDocument);

		convertedBytes = convertedDocument.ToArray();
	}
}

Is there a way I can do this? For example, could I load the document prior to conversion to discover its orientation?

Many thanks,

Alex.


#2

@axdaws,

Thank you for taking interest in GroupDocs.Conversion for .NET and posting your concerns. Please note that GroupDocs.Conversion for .NET is a back-end API that is totally UI-agnostic, platform and framework independent. Hence, you can integrate it in your any .NET project.

As far as your query is concerned, currently API doesn’t facilitate any such (built-in) functionality. However, you can avail this feature by implementing your own use case/logic You can detect whether or not a PDF file has portrait/landscape pages.

Rectangle currentPageRectangle = pdfReader.GetPageSizeWithRotation(<PageNumberHere>);
if (currentPageRectangle.Width > currentPageRectangle.Height)
{
     //page is landscape
}
else
{
     //page is portrait
} 

Once you detect the nature of page, you can set watermark angles accordingly. You can also find some other ways to find out PDF pages orientation.

However, we have logged this scenario as an investigation in our internal issue tracking system with ID:CONVERSIONNET-2393. As we get any update from the concerned team, we shall surely notify you.


#3

@axdaws,

We’re pleased to notify you that you can load or find document orientation before doing conversion. In version 18.3, we introduced a new property PageOrientation in DocumentInfo class, usage of the property is as follows:

var documentInfo = conversionHandler.GetDocumentInfo("source.docx");
Console.Write(documentInfo.PageOrientation);

We’d recommend you to integrate the latest release of the API in your project, utilize this new property and share your experience.