Welcome Guest! To enable all features, please Login or Register.

Notification

Icon
Error

Options
View
Last Go to last post Unread Go to first unread post
#1 Posted : Tuesday, July 28, 2020 2:54:59 AM(UTC)
Abdul Rahman

Groups: Registered
Posts: 54


I'm using document writer to save the rotated raster image as PDF. However the resulting PDF loses its quality after doing rotation.

Here is my code:
Code:

var docWriter = new DocumentWriter();

var pdfOptions = docWriter.GetOptions(DocumentFormat.Pdf) as PdfDocumentOptions;
pdfOptions.DocumentType = PdfDocumentType.Pdf;
//pdfOptions.OneBitImageCompression = OneBitImageCompressionType.Flate;
//pdfOptions.ColoredImageCompression = ColoredImageCompressionType.FlateJpeg;
//pdfOptions.ImageOverText = true;
//pdfOptions.ImageOverTextSize = DocumentImageOverTextSize.Original;
//pdfOptions.ImageOverTextMode = DocumentImageOverTextMode.Strict;
//pdfOptions.QualityFactor = 50;
//pdfOptions.DocumentResolution = 0;
docWriter.SetOptions(DocumentFormat.Pdf, pdfOptions);

using RasterCodecs codecs = new RasterCodecs();
codecs.Options.Load.Name = $"anything.pdf";
var originalImage = codecs.Load(file);
var info = codecs.GetInformation(file, true);

for (int i = 0; i < 10; i++)
{
    var memoryStream = new MemoryStream();
    docWriter.BeginDocument(memoryStream, DocumentFormat.Pdf);

    for (int j = 1; j <= info.TotalPages; j++)
    {
        var page = new DocumentWriterRasterPage();
        var image = codecs.Load(i == 0 ? file : @$"{currentPath}\Image To Pdf\docwriterrotate.pdf", j);

        RotateCommand command = new RotateCommand();
        command.Angle = 90 * 100;
        command.FillColor = new RasterColor(255, 255, 255);
        command.Flags = RotateCommandFlags.Resize | RotateCommandFlags.Bicubic;
        command.Run(image);

        page.Image = image;
        docWriter.AddPage(page);
        page.Image.Dispose();
    }

    docWriter.EndDocument();

    // save the document writer stream
    using var fileStream = File.Create(@$"{currentPath}\Image To Pdf\docwriterrotate.pdf");
    memoryStream.Seek(0, SeekOrigin.Begin);
    memoryStream.CopyTo(fileStream);
    memoryStream.Dispose();
}


I tried changing multiple options like quality factor, compression, image over text, etc.. but couldn't get the best output pdf.

I have attached the original PDF used for rotation.

Original PDF Size - 30kb
After Rotate and saving as PDF using document writer - 10kb

I need an output PDF file with size same or less than original file size without losing orignial file quality. Please assist on how to achieve this.

File Attachment(s):
Demo.pdf (31kb) downloaded 1 time(s).
 

Try the latest version of LEADTOOLS for free for 60 days by downloading the evaluation: https://www.leadtools.com/downloads

Wanna join the discussion? Login to your LEADTOOLS Support accountor Register a new forum account.

#2 Posted : Tuesday, July 28, 2020 10:37:37 AM(UTC)
Matthew Bresson

Groups: Registered, Tech Support, Administrators
Posts: 67

Was thanked: 2 time(s) in 2 post(s)

Hello Abdul,

The reason the PDF quality is decreasing is due to you rasterizing the PDF to rotate it. If you want to keep the PDF quality you will need to look into using the LEADDocument and DocumentPage class, since the PDF sample you are using is a vector PDF.
https://www.leadtools.co...dh/dox/leaddocument.html
https://www.leadtools.co...dh/dox/documentpage.html

The DocumentPage class contains a rotate method so that you do not need to gather the RasterImage of the document.
https://www.leadtools.co...documentpage-rotate.html
Your code could look something like this:

Code:

         MemoryStream ms = new MemoryStream();

         using (var document = DocumentFactory.LoadFromFile(_file, new LoadDocumentOptions()))
         {
            document.IsReadOnly = false;
            DocumentPage page = document.Pages[0];

            page.Rotate(90);
            using (DocumentConverter converter = new DocumentConverter())
            {
               var jobData = new DocumentConverterJobData();
               // The loaded document is our input 
               jobData.Document = document;
               // We want PDF as output 
               jobData.DocumentFormat = DocumentFormat.Pdf;

               jobData.OutputDocumentStream = ms;

               var job = converter.Jobs.CreateJob(jobData);
               converter.Jobs.RunJob(job);
            }
         }
         ms.Position = 0;
         File.WriteAllBytes(@"FILE PATH TO OUTPUT PDF", ms.GetBuffer());


Here is the resulting PDF:
File Attachment(s):
test(docConverter).pdf (5kb) downloaded 1 time(s).

If you have any questions about the information above please feel free to reach back out to me.

Thanks
Matt Bresson
Developer Support Engineer
LEAD Technologies, Inc.
LEADTOOLS
 
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.

Powered by YAF.NET | YAF.NET © 2003-2020, Yet Another Forum.NET
This page was generated in 0.054 seconds.