We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Problem with the size file

I´m working in a POC to create a pdf document but I have two problems, first, I need to use a svg image per each page, if I try to use the image as background the image shows wrong(show the text at top-left) then I tried I import the svg image to create a document and copy the page to another document but this method is very slow compared to use image as background (page.BackgroundImage or page.Artifacts.Add(BackgroundArtifact) ), if I try to convert the image to png and use it the pdf file size increase a lot(actually no matter the type of image).
second, in general the file size is bigger compared with our previous app, the size is between the double and triple at least.
could you help me to found a way to reduce the size of the file?
if it’s possible How can i reduce the time to write a pdf(I only use text, hyperlink and images and backgrounds)?

@armando.rodriguez

Please check the below documentation article(s) in order to reduce the PDF file size by optimizing it:

Furthermore, the performance of the API can be measured on the basis of subsequent runs. First time when program launches, the API loads necessary resources like fonts, etc. in the memory to process the provided document. Therefore, the time taken by processing is comparatively less on subsequent runs.

If you still face any issues, please share your sample code snippet with us so that we can test it in our environment and address the issue of processing delay accordingly.

Actually I use the process to Optimize the file size, but the Optimize process only reduce the file size 4 mb but My goal is reduce the pdf file almost 50mb, My idea is to find the best way to create a file that only use text, background and image of barcodes and the file size could be as small as possible.

Document doc = (Document)pdfDocument;

Page nPag = doc.Pages.Add();
nPag.PageInfo.Margin.Top = 0;
nPag.PageInfo.Margin.Bottom = 0;
nPag.PageInfo.Margin.Left = 0;
nPag.PageInfo.Margin.Right = 0;

nPag.SetPageSize(w, h);
nPag.PageInfo.IsLandscape = landscape;

///here exist a for each to write all the text

TextBuilder textBuilder = new TextBuilder(page);
TextFragment textFragment = new TextFragment(tb.Text);

bool isLandscape = (page.Rect.Width > page.Rect.Height);
bool typePage = (isLandscape) ? page.Rect.Width > 1000 : page.Rect.Height > 1000;

if (typePage)
{
if (isLandscape)
{
yval = page.Rect.URY > (tb.Y / 3.198) ? (page.Rect.URY - (tb.Y / 3.198)) : (page.Rect.URY - (tb.Y / 4.132)); // 4.127 is the minor value // 3.165
yval = yval < 0 ? page.Rect.URY - (tb.Y / 3.198) : yval;
yval = yval * 0.987;
xval = tb.X / 3.255;
}
else
{
yval = page.Rect.URY > (tb.Y / 3.039) ? (page.Rect.URY - (tb.Y / 3.039)) : (((tb.Y / 3.039) - page.Rect.URY)); // 3.001 is the minor value
yval = yval < 0 ? page.Rect.URY - (tb.Y / 3.039) : yval;
yval = yval * 0.987;
xval = tb.X / 3.255;
}
}
else
{
yval = page.Rect.URY > (tb.Y / 4.15) ? (page.Rect.URY - (tb.Y / 4.15)) - 8 : (((tb.Y / 4.15) - page.Rect.URY)) + 5;
yval = yval < 0 ? page.Rect.URY - (tb.Y / 4.15) : yval;
xval = tb.X / 4.15;
}

textFragment.Position = new Position(xval, yval); // tb.X/4.15, page.Rect.URY - (tb.Y / 4.1)

// Set text properties
textFragment.TextState.FontSize = tb.Font.Size;
textFragment.TextState.Font = FontRepository.FindFont(tb.Font.Name);
textFragment.TextState.FontStyle = (FontStyles)tb.Font.Style;// FontStyles.Regular;
textFragment.TextState.ForegroundColor = tb.TextColor == System.Drawing.Color.Black? Aspose.Pdf.Color.Black: Aspose.Pdf.Color.MediumBlue;
textFragment.Margin = new MarginInfo(0, 0, 0, 0);

textBuilder.AppendText(textFragment);

///here exist an end for each to write all the text

///code to add background
Page tempPage = (Page)pdfPage; // this page from other document and it’s created using an svg file
doc.Pages.Add(tempPage);

/// for to add barcodes to page
BarcodeGenImage bcIma = new BarcodeGenImage();
MemoryStream ima = bcIma.GenBarcodeImage(big);

System.Drawing.Rectangle imageRect = new System.Drawing.Rectangle((int)(big.X / 3.255), (int)(big.Y / 3.039), (int)(big.Width / 3.255), (int)(big.Height / 3.039));
Aspose.Pdf.Rectangle pdfRect = new Aspose.Pdf.Rectangle(imageRect.Left, page.Rect.Height - imageRect.Bottom, imageRect.Right, page.Rect.Height - imageRect.Top);
page.AddImage(ima, pdfRect);

// end for to add barcodes to page
doc.Save(doc.FileName);

///code to create fail using a svg stream
public object SVGtoPDFPage(Stream SVGfile)
{
Page page;
SvgLoadOptions svgLoadOptions = new SvgLoadOptions();

using (Document pdfDocument2 = new Document(SVGfile, svgLoadOptions))
{
	page = pdfDocument2.Pages[1];
            
            //delete annotations
            page.Annotations.Delete();
}
    return page;

}

@armando.rodriguez

Can you please share the final PDF document as well generated using above shared code at your end?

Hi, @asad.ali

this is the file I generated with my code

@armando.rodriguez

We have logged an issue as PDFNET-53602 in our issue tracking system to check why API is not able to reduce the file size more than 4MB. We will look into details of the ticket and let you know as soon as it is resolved. Meanwhile, can you please share the code snippet that you have tried so far to reduce the file size.

Also, please mention how much time did API take at your end to optimize the PDF document you shared with us? We have noticed while testing that API was taking longer than expected during document optimization with your PDF.

Hi @asad.ali,

this is my code to try to reduce the file size.

private void OptimizePDFFile(Document doc)
{
var optimizeOptions = new Aspose.Pdf.Optimization.OptimizationOptions
{
RemoveUnusedObjects = true,
RemoveUnusedStreams = true,
AllowReusePageContent = true,
};

        doc.OptimizeResources(optimizeOptions);
        doc.Optimize();
    }

this code takes 15min to end and other 15min to create the pdf file

@armando.rodriguez

Another issue as PDFNET-53608 has been logged in our issue tracking system to investigate the delay that API is making while optimization. We will let you know once the logged tickets are resolved. Please spare us some time.

We are sorry for the inconvenience.