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.

Fri Apr 28, 2023 6:59 am

Hi Team,

I want to add the Parallel watermark.ForEach, we are using netstandard2.0, and our longer documents have a 20000 page maximum.

using (PdfDocument doc = new PdfDocument())
{
doc.LoadFromStream(fileStream);

foreach (PdfPageBase page in doc.Pages)
{
RectangleF rectangleF = CalculateWatermarker(page.ActualSize.Width, page.ActualSize.Height);

int fontSize = (int)(Math.Min(rectangleF.Height, rectangleF.Width) / markerLines);

PdfFont font = new PdfFont(PdfFontFamily.Helvetica, settings.AutoScale ? fontSize : settings.FontSize);

PdfTilingBrush pdfTilingBrush = new PdfTilingBrush(
new SizeF(page.ActualSize.Width, page.ActualSize.Height));

pdfTilingBrush.Graphics.SetTransparency(settings.Opacity);
_ = pdfTilingBrush.Graphics.Save();

float centerX = page.ActualSize.Width / 2;
float centerY = page.ActualSize.Height / 2;

rectangleF.X = page.ActualSize.Width / 2 - rectangleF.Width / 2;
rectangleF.Y = page.ActualSize.Height / 2 - rectangleF.Height / 2;

double radian = Math.Atan(page.ActualSize.Height / page.ActualSize.Width);
float degree = (float)(radian * (180 / Math.PI));

pdfTilingBrush.Graphics.RotateTransform(-degree, new PointF(centerX, centerY));

pdfTilingBrush.Graphics.DrawString(text,
font, pdfBrush, rectangleF,
new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle));

pdfTilingBrush.Graphics.Restore();

page.Canvas.DrawRectangle(pdfTilingBrush,
new RectangleF(new PointF(0, 0), page.Canvas.ClientSize));
}

doc.SaveToStream(outputStream, FileFormat.PDF);

}

sureshkumar.arumugam
 
Posts: 11
Joined: Fri Apr 28, 2023 6:13 am

Fri Apr 28, 2023 8:26 am

Hi,

Thanks for your message.
Do you want to add watermarks to different pages of the same document in parallel? Sorry at present this Parallel for the same document is not feasible. Currently, our product can handle different documents with multiple threads. For your issue, we first suggest that you increase the available memory of the server. And our product supports splitting PDF into multiple PDF files, or you can first splitting your longer file, add watermarks in the Parallel by different files, and then merge them.
If my understanding is incorrect, you can share more details for further investigation. Thanks in advance.

Best Regards,
Herman
E-iceblue support team
User avatar

Herman.Yan
 
Posts: 115
Joined: Wed Mar 08, 2023 2:00 am

Fri Apr 28, 2023 9:28 am

yes same document will be parallel. Although our requirements do not permit the storage of files, it is possible to split the output into streams. Once applied, the watermark is combined with the stream to create a single stream.

sureshkumar.arumugam
 
Posts: 11
Joined: Fri Apr 28, 2023 6:13 am

Fri Apr 28, 2023 10:16 am

Hi,

Thanks for your feedback.
Please refer to the following sample code for testing
Code: Select all
            //split
            PdfDocument sourceDoc = new PdfDocument();
            sourceDoc.LoadFromStream(originalFileStream);
            PdfDocument newDoc_1 = new PdfDocument();
            PdfDocument newDoc_2 = new PdfDocument();
            //Insert the first page of source file to the first document
            newDoc_1.InsertPage(sourceDoc, 0);
            //Insert the rest pages of source file to the second document
            newDoc_2.InsertPageRange(sourceDoc, 1, sourceDoc.Pages.Count - 1);
            //Save the two documents as PDF files
            MemoryStream outputStream1 = new MemoryStream();
            newDoc_1.SaveToStream(outputStream1, FileFormat.PDF);
            MemoryStream outputStream2 = new MemoryStream();
            newDoc_2.SaveToStream(outputStream2, FileFormat.PDF);
            //merge
            Stream[] streams = new Stream[] { outputStream1, outputStream2 };
            PdfDocumentBase doc = PdfDocument.MergeFiles(streams);


If there are any issues with the testing, please feel free to consult at any time.

Best Regards,
Herman
E-iceblue support team
Last edited by Herman.Yan on Mon May 01, 2023 3:39 am, edited 1 time in total.
User avatar

Herman.Yan
 
Posts: 115
Joined: Wed Mar 08, 2023 2:00 am

Fri Apr 28, 2023 10:55 am

Without memorystream it is possible ?

sureshkumar.arumugam
 
Posts: 11
Joined: Fri Apr 28, 2023 6:13 am

Mon May 01, 2023 10:04 am

Hello,

Thank for your feedback.
Due to you do not permit storage, you need save the result pdf file to stream after split source pdf file, it’s usually stored in MemoryStream, but it’s better if you have a better Stream option. In addition, you can use RecyclableMemoryStreamManager to optimize MemoryStream according to the online help document, the following is code for your reference.
Code: Select all
//split
            PdfDocument sourceDoc = new PdfDocument();
            sourceDoc.LoadFromStream();
            PdfDocument newDoc_1 = new PdfDocument();
            PdfDocument newDoc_2 = new PdfDocument();

            //Insert the first page of source file to the first document
            newDoc_1.InsertPage(sourceDoc, 0);

            //Insert the rest pages of source file to the second document
            newDoc_2.InsertPageRange(sourceDoc, 1, sourceDoc.Pages.Count - 1);

            //Save the two documents as PDF files
            //Create RecyclableMemoryStreamManager to optimize MemoryStream
            RecyclableMemoryStreamManager manager = new RecyclableMemoryStreamManager();
            MemoryStream outputStream1 = manager.GetStream();
            newDoc_1.SaveToStream(outputStream1, FileFormat.PDF);
         
            MemoryStream outputStream2 = manager.GetStream();
            newDoc_2.SaveToStream(outputStream2, FileFormat.PDF); 

            //merge
            Stream[] streams = new Stream[] { outputStream1, outputStream2 };
            PdfDocumentBase doc = PdfDocument.MergeFiles(streams);



Sincerely,
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Tue May 16, 2023 10:51 am

we have 10000 pages ,how we can handle to apply the watermark

sureshkumar.arumugam
 
Posts: 11
Joined: Fri Apr 28, 2023 6:13 am

Wed May 17, 2023 10:28 am

Hello,

Thanks for your feedback.
What is the size of your document? It should need your system has a sufficient available memory to process this large document. Did you have any problem when directly loading your document with the following code?
Code: Select all
    PdfDocument sourceDoc = new PdfDocument();
    sourceDoc.LoadFromFile(@"Test.pdf");

Anyway, I will do further investigation and provide a sample code for you to test your demand. I will update you once it is finished. Thanks for your patience.

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1533
Joined: Wed Apr 25, 2018 3:20 am

Thu May 18, 2023 5:35 am

Hello,

Thanks for your patience.
You can refer to the following sample code to test your case. If there is any issues, you can upload your file to the DropBox or OneDrive, and then share the download link with us for further testing.
Code: Select all
  static void Main(string[] args)
     {
     //load original pdf
     PdfDocument sourceDoc = new PdfDocument();
     sourceDoc.LoadFromFile(@"Test.pdf");

     //split PDF and make each PDF with 100 pages
     List<MemoryStream> streams = SplitPdf(sourceDoc, 100);

     //add watermark
     Parallel.ForEach(streams, stream =>
     {
         AddWatermarkToPdfStream(stream);
     });

     //merge
     MergeStreamsAsPdfFile(streams, "final.pdf");
 }
 static List<MemoryStream> SplitPdf(PdfDocument doc, int pageSize)
 {
     List<MemoryStream> streams = new List<MemoryStream>();

     for (int i = 0; i < doc.Pages.Count; i += pageSize)
     {
         int count = i + (pageSize - 1);
         if (count > doc.Pages.Count-1)
         {
             count = doc.Pages.Count - 1;
         }           
         PdfDocument newDoc = new PdfDocument();
         newDoc.InsertPageRange(doc, i,count);     
         //save to streams
         MemoryStream stream = new MemoryStream();
         newDoc.SaveToStream(stream);
         newDoc.SaveToFile(String.Format("{0}.pdf",count),FileFormat.PDF);
         streams.Add(stream);
         newDoc.Close();
     }
     return streams;
 }

 private static void AddWatermarkToPdfStream(MemoryStream stream)
 {
     PdfDocument doc = new PdfDocument();
     doc.LoadFromStream(stream);
     for (int i = 0; i < doc.Pages.Count; i++)
     {
         PdfPageBase page = doc.Pages[i];
         //Draw text watermark
         PdfTilingBrush brush
             = new PdfTilingBrush(new SizeF(page.Canvas.ClientSize.Width / 2, page.Canvas.ClientSize.Height / 3));
         brush.Graphics.SetTransparency(0.3f);
         brush.Graphics.Save();
         brush.Graphics.TranslateTransform(brush.Size.Width / 2, brush.Size.Height / 2);
         brush.Graphics.RotateTransform(-45);
         brush.Graphics.DrawString("Spire.Pdf Demo",
             new PdfFont(PdfFontFamily.Helvetica, 24), PdfBrushes.Violet, 0, 0,
             new PdfStringFormat(PdfTextAlignment.Center));
         brush.Graphics.Restore();
         brush.Graphics.SetTransparency(1);
         page.Canvas.DrawRectangle(brush, new RectangleF(new PointF(0, 0), page.Canvas.ClientSize));
     }
     //Save pdf file
     stream.Seek(0, SeekOrigin.Begin);     
     doc.SaveToStream(stream);
     doc.Close();
 }
 static void MergeStreamsAsPdfFile(List<MemoryStream> streams, string final)
 {
     Stream[] pdfStreams = streams.ToArray();
     PdfDocumentBase doc = PdfDocument.MergeFiles(pdfStreams);
     doc.Save(final, Spire.Pdf.FileFormat.PDF);
 }

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1533
Joined: Wed Apr 25, 2018 3:20 am

Return to Spire.PDF

cron