Problem with password protected pdf

I have a PDF file secured with the Security password method (open password: no, permission password: yes). I can open the file (without entering the password) in any browser (Firefox, Chrome) and any PDF program (Adobe, Foxit).

Then I add a barcode to the file and save it. From that moment on, I can’t open the PDF file in Chrome and Foxit (because it asks for a password), but in Firefox and Adobe I can still open it without a password along with the barcode. In Adobe, the new file still displays the Security password method (open password: no, permission password: yes).

What could be the reason for this?

@gatzec

Could you please provide the code you are using to add the barcode to the PDF file? This will help us understand the issue better.

Here is the code snippet used to apply barcode to PDF.

public void InsertBarcodeToDocument(String sBarCode, String sPdf)
	{
		BarCodeBuilder oBarCode = new BarCodeBuilder();
		<barcode details cut ... />
		try {
			com.aspose.pdf.Document oDocBuilder = new com.aspose.pdf.Document(sPdf);
			float iPageWidth = (float)(oDocBuilder.getPages().get_Item(1).getCropBox().getWidth());
			float iPageHeight = (float)(oDocBuilder.getPages().get_Item(1).getCropBox().getHeight());
			for (int pdfPage = 1; pdfPage<= oDocBuilder.getPages().size(); pdfPage++)
			{
				if (oDocBuilder.getPages().get_Item(pdfPage).getResources().getFonts() != null)
				{
					for (int fontPdf = 1; fontPdf<= oDocBuilder.getPages().get_Item(pdfPage).getResources().getFonts().size(); fontPdf++)
					{
						com.aspose.pdf.Font pageFont = oDocBuilder.getPages().get_Item(pdfPage).getResources().getFonts().get_Item(fontPdf);
						if (!pageFont.isEmbedded()) {
							pageFont.setEmbedded(true);
						}
					}
				}
			}
			BufferedImage oBarCodeImage = oBarCode.generateBarCodeImage();
			ByteArrayOutputStream outStream = new ByteArrayOutputStream();
			oBarCode.save(outStream, com.aspose.barcode.BarCodeImageFormat.Png);
			ImageIO.write(oBarCodeImage, "png", outStream);
			InputStream oInputStream = new ByteArrayInputStream(outStream.toByteArray());
			float lowerX = (float)((iPageWidth - iFactor*iImageWidth)/2);
			float lowerY = (float)(iPageHeight - 6 - (iImageHeight*iFactor) );
			float upperX = (float)(lowerX + iFactor*iImageWidth);
			float upperY = (float)(iImageHeight*iFactor + lowerY);
			com.aspose.pdf.Page oPage = oDocBuilder.getPages().get_Item(1);
			oPage.getResources().getImages().add(oInputStream);
			oPage.getContents().add(new com.aspose.pdf.Operator.GSave());
			com.aspose.pdf.Rectangle rectangle = new com.aspose.pdf.Rectangle(lowerX, lowerY, upperX, upperY);
			com.aspose.pdf.Matrix matrix = new com.aspose.pdf.Matrix(new double[] { rectangle.getURX() - rectangle.getLLX(), 0, 0, rectangle.getURY()- rectangle.getLLY(), rectangle.getLLX(), rectangle.getLLY() });
			oPage.getContents().add(new com.aspose.pdf.Operator.ConcatenateMatrix(matrix));
			com.aspose.pdf.XImage ximage = oPage.getResources().getImages().get_Item(oPage.getResources().getImages().size());
			oPage.getContents().add(new com.aspose.pdf.Operator.Do(ximage.getName()));
			oDocBuilder.optimize();
			oDocBuilder.save(sPdf);
			oDocBuilder.close();
			oInputStream.close();   
		} catch (Exception e) {		
				System.out.println(e);	
		}	
	}

@gatzec

If possible, could you please share your sample source and output PDF document for our reference? We will test the scenario in our environment and address it accordingly.

Unfortunately, the source file contains sensitive customer data and I cannot share it publicly. By the way, I noticed another interesting thing. I wanted to prepare a custom PDF file for you with only the owner password set using the command:

qpdf --encrypt "" "secret" 256 -- aspose.pdf aspose_encrypted.pdf

Interestingly, aspose handled such a file perfectly.

@gatzec

The issue can be related to specific PDF document. That is why we requested if you could please share the problematic file with us so that we can observe it in our environment and address it accordingly.

It turns out that the file from the client is generated by the mPDF library. I managed to prepare such a file (source.pfd) using the code:

$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML('Aspose test with pdf made by Mpdf 8.1.2');
$mpdf->SetProtection(array(), '', 'secret');
$mpdf->Output();

Then after adding the barcode in Aspose I get a file (output.pdf) that cannot be opened in Chrome or Foxit.

source.pdf (14,5 KB)

output.pdf (23,4 KB)

@gatzec

We tested with 24.12 version of the API by adding sample image instead of barcode and saved the document. The output PDF document was able to be opened in Chrome without asking for the password. Please find the attached file.
output.pdf (70.4 KB)

Can you please try using the latest version and let us know in case you face this issue still.

Thanks for the tip. Updating the API version actually fixes the problem.

@gatzec

Its nice to know that the latest version resolved the issue at your end. Please keep using the API and feel free to post a new topic in case you face any other issues or need any kind of assistance.

1 Like