Hi,
I have a problem with a document. After saving it as .pdf some additional pages appear
(attached files, source file has 4 pages and the pdf has 5).
I’m using Aspose.Words for editing files that are stored in an IMAGE column in MS SQL Server 2005 database.
Here is the code that creates a Document after the content is retrieved from the database.
MemoryStream _inputStream = new MemoryStream(content);
Document _document = new Document(_inputStream);
After that a watermark is added on each page.
public void AddWaterMark(string watermarkText)
{
// Create a watermark shape. This will be a WordArt shape.
// You are free to try other shape types as watermarks.
Shape watermark = new Shape(_document, ShapeType.TextPlainText);
// Set up the text of the watermark.
watermark.TextPath.Text = watermarkText;
watermark.TextPath.FontFamily = "Arial";
watermark.Width = 500;
watermark.Height = 100;
// Text will be directed from the bottom-left to the top-right corner.
watermark.Rotation = -40;
// Remove the following two lines if you need a solid black text.
watermark.Fill.Color = Color.Gray; // Try LightGray to get more Word-style watermark
watermark.StrokeColor = Color.Gray; // Try LightGray to get more Word-style watermark
// Place the watermark in the page center.
watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;
watermark.WrapType = WrapType.None;
watermark.VerticalAlignment = VerticalAlignment.Center;
watermark.HorizontalAlignment = HorizontalAlignment.Center;
// Create a new paragraph and append the watermark to this paragraph.
Paragraph watermarkPara = new Paragraph(_document);
watermarkPara.AppendChild(watermark);
int i = 0;
// Insert the watermark into all headers of each document section.
foreach (Section sect in _document.Sections)
{
// There could be up to three different headers in each section, since we want
// the watermark to appear on all pages, insert into all headers.
InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderPrimary);
InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderFirst);
InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderEven);
if (i == 0)
i++;
else
sect.HeadersFooters.LinkToPrevious(true);
}
}
private void InsertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, HeaderFooterType headerType)
{
HeaderFooter header = sect.HeadersFooters[headerType];
if (header == null)
{
// There is no header of the specified type in the current section, create it.
header = new HeaderFooter(sect.Document, headerType);
sect.HeadersFooters.Add(header);
}
// Insert a clone of the watermark into the header.
header.AppendChild(watermarkPara.Clone(true));
}
And finally the document is saved as pdf.
public FileNetContent SaveAsPdf()
{
MemoryStream _outputStream = new MemoryStream();
_document.Save(_outputStream, SaveFormat.Pdf);
_outputStream.Flush();
return _outputStream.ToArray();
}
I’m using Aspose.Words version 14.11.0.0. for .NET. The source and the resulting files that reproduce the issue are attached.
Please, help. Thanks in advance.