Re: Barcode Resolution - Size

Hi,

I have another problem. Here is my code :

FileStream inFileStream1 = File.OpenRead("C:\\Temp\\file.pdf");
MemoryStream doc1 = new MemoryStream();
doc1.SetLength(inFileStream1.Length);
inFileStream1.Read(doc1.GetBuffer(), 0, (int)inFileStream1.Length);
using (Aspose.Pdf.Document doc = new Aspose.Pdf.Document(doc1))
{
//Check first page to verify size and adjust if not 8.5 x 11 inches
PageInfo pageInfo = new PageInfo();
if (doc.Pages[1].MediaBox.URY != pageInfo.Height || doc.Pages[1].MediaBox.URX != pageInfo.Width)
{
pageInfo.Height = (int)doc.Pages[1].MediaBox.URY;
pageInfo.Width = (int)doc.Pages[1].MediaBox.URX;
}

using (Aspose.Pdf.Facades.PdfFileMend mendor = new Aspose.Pdf.Facades.PdfFileMend(doc))
{
Stream imageMemoryStream = new MemoryStream();
float halfWidth = 0.0f, halfHeight = 0.0f, x = 0.0f, y = 0.0f;

// Instantiate linear barcode object
BarCodeBuilder barCodeBuilder = new BarCodeBuilder();
barCodeBuilder.CodeText = "FRE0400 - 0000284A";
barCodeBuilder.CodeTextFont = new System.Drawing.Font(barCodeBuilder.CodeTextFont.Name, 10f);
barCodeBuilder.CodeTextSpace = 1f;
barCodeBuilder.xDimension = 0.2f;
barCodeBuilder.BarHeight = 6.5f;
barCodeBuilder.SymbologyType = Symbology.Code128;
barCodeBuilder.Resolution = new Resolution(300f, 300f, ResolutionMode.Customized);
barCodeBuilder.BarCodeImage.Save(imageMemoryStream, System.Drawing.Imaging.ImageFormat.Bmp); // Jpeg and BMP are the fastest
barCodeBuilder.BarCodeImage.Save("C:\\Temp\\barcode.bmp");
halfWidth = (float)ConvertUtil.PixelToPoint(barCodeBuilder.BarCodeImage.Width) / 2f;
halfHeight = (float)ConvertUtil.PixelToPoint(barCodeBuilder.BarCodeImage.Height) / 2f;

// Add image to the input PDF file on the first page at specified location
x = (float)pageInfo.Width - halfWidth;
y = (float)pageInfo.Height - halfHeight;

float LLX = x - halfWidth;
float LLY = y - halfHeight;
float URX = x + halfWidth;
float URY = y + halfHeight;

mendor.AddImage(imageMemoryStream, 1, LLX, LLY, URX, URY);

Stream outStream = new MemoryStream();
doc.Save(outStream);

// Rewind the stream position back to zero so it is ready for next reader
outStream.Position = 0;

FileStream outFileStream1 = new FileStream("C:\\Temp\\file_barcode1.pdf", FileMode.Create);
(outStream as MemoryStream).WriteTo(outFileStream1);

}
}

When I'm looking at barcode.bmp the resolution is 300 DPI which is good.
But when I'm looking at file_barcode1.pdf, the resolution is only 96 DPI. Do you have any idea why ?

Thank you !
Hi Nicolas,

Thank you for contacting support. Please provide us your source PDF in this forum thread. It'll help us to replicate the problem on our side. We'll investigate and reply you appropriately. Your response is awaited.

Hi,


Here’s the source pdf file.

Thank you.

Hi Nicolas,


Thank you for providing the source PDF. We’re working over your query and will get back to you soon.

Hi Nicolas,


Thank you for being patient. We have tested your code against the latest versions of Aspose.Pdf and Aspose.BarCode APIs. The output PDF looks perfect to me. We have attached the output PDF to this reply for your kind reference. In case, this does not help, then please let us know how you are checking resolution of the barcode image available in the PDF.

Hi,


Thank you for your help, but I don’t see any document attached to your answer.
Hi Nicolas,

We're sorry for missing an attachment. We have now attached an output PDF along with the zip archive of a barcode image to this reply.

Hi,


I don’t understand why you say that there is no problem. The barcode appears bigger than is supposed to. The specify BarHeigh code is 6.5 mm.

Thank you

Hi Nicolas,


We’re sorry for the inconvenience caused. We’re working over your query and will get back to you soon.

Hi Nicolas,


Thank you for being patient and sorry for the delayed response. You are getting a large BarCode image because of the value for LLX, LLY, URX and URY. In order to have a smaller size image, you may consider using following code line:

[.NET, C#]
mendor.AddImage(imageMemoryStream, 1, LLX, LLY, URX-80, URY-40);

Please let us know in case of any confusion or questions.

Hi,


I’m wondering why those arbitrary values (80, 40) ?
This may be a “patch” for the image dimension, but what about the resolution ? It still be at 96 dpi instead of 300…

Thank you.

Hi Nicolas,

We are sorry for the inconvenience. You may use AddImage option to add image in existing PDF document and set image width and height to control image resolution. Hopefully it will help you to accomplish your requirement. Please check the following C# code:

using (Aspose.Pdf.Document doc = new Aspose.Pdf.Document("Document(1).pdf"))
{
    //Check first page to verify size and adjust if not 8.5 x 11 inches
    PageInfo pageInfo = new PageInfo();
    if (doc.Pages[1].MediaBox.URY != pageInfo.Height || doc.Pages[1].MediaBox.URX != pageInfo.Width)
    {
        pageInfo.Height = (int)doc.Pages[1].MediaBox.URY;
        pageInfo.Width = (int)doc.Pages[1].MediaBox.URX;
    }

    Stream imageMemoryStream = new MemoryStream();
    float halfWidth = 0.0f, halfHeight = 0.0f, x = 0.0f, y = 0.0f;

    // Instantiate linear barcode object
    BarCodeBuilder barCodeBuilder = new BarCodeBuilder();
    barCodeBuilder.CodeText = "FRE0400 - 0000284A";
    barCodeBuilder.CodeTextFont = new System.Drawing.Font(barCodeBuilder.CodeTextFont.Name, 10f);
    barCodeBuilder.CodeTextSpace = 1f;
    barCodeBuilder.xDimension = 0.2f;
    barCodeBuilder.BarHeight = 6.5f;
    barCodeBuilder.SymbologyType = Aspose.BarCode.Symbology.Code128;
    barCodeBuilder.Resolution = new Aspose.BarCode.Resolution(300f, 300f, ResolutionMode.Customized);
    barCodeBuilder.BarCodeImage.Save(imageMemoryStream, System.Drawing.Imaging.ImageFormat.Bmp); // Jpeg and BMP are the fastest
    barCodeBuilder.BarCodeImage.Save("barcode.bmp");

    halfWidth = (float)ConvertUtil.PixelToPoint(barCodeBuilder.BarCodeImage.Width) / 2f;
    halfHeight = (float)ConvertUtil.PixelToPoint(barCodeBuilder.BarCodeImage.Height) / 2f;

    // Add image to the input PDF file on the first page at specified location
    x = (float)pageInfo.Width - halfWidth;
    y = (float)pageInfo.Height - halfHeight;

    // Set coordinates
    double lowerLeftX = x - halfWidth;
    double lowerLeftY = y - halfHeight;
    double upperRightX = x + halfWidth;
    double upperRightY = y + halfHeight;

    // Get the page where image needs to be added
    Page page = doc.Pages[1];

    // Load image into stream
    FileStream imageStream = new FileStream("barcode.bmp", FileMode.Open);

    // Create Rectangle
    Aspose.Pdf.Rectangle rectangle = new Aspose.Pdf.Rectangle(lowerLeftX, lowerLeftY, upperRightX, upperRightY);

    page.AddImage(imageStream, rectangle, 1000, 500, true);
    Stream outStream = new MemoryStream();

    doc.Save(outStream);

    // Rewind the stream position back to zero so it is ready for next reader
    outStream.Position = 0;
    FileStream outFileStream1 = new FileStream("file_barcode.pdf", FileMode.Create);
    (outStream as MemoryStream).WriteTo(outFileStream1);
}

Best Regards,