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

Free Support Forum - aspose.com

Display of Attachments in a Table

Hello Aspose,

I would like to join an attachment and a image so that it looks like one image (the pin appears in top/left corner of the image). Please see the upload PDF.

To reach this I did this:

private static void Main(string[] args)
{
  Pdf pdf1 = new Pdf();
  Section sec1 = pdf1.Sections.Add();

Table table1 = new Table();
sec1.Paragraphs.Add( table1 );
table1.DefaultCellBorder = new BorderInfo( (int) BorderSide.All, new Aspose.Pdf.Generator.Color( “LightSkyBlue” ) );
table1.ColumnAdjustment = ColumnAdjustmentType.AutoFitToContent;

Row row1 = table1.Rows.Add();
Cell cell1Row1 = row1.Cells.Add();
Attachment fileAttachment = new Attachment();
cell1Row1.Paragraphs.Add( fileAttachment );
fileAttachment.AttachmentType = AttachmentType.File;
fileAttachment.AttachedFileName = @“F:\tfs\Entwicklung\bin\x86\Debug\DemoImage”;
fileAttachment.AttachedFileType = “jpg”;
fileAttachment.FileIconType = FileIconType.PushPin;
fileAttachment.IconColor = new Color( “Brown” );
fileAttachment.NoteContent = “Attachment 1”;

Cell cell2Row1 = row1.Cells.Add();
Image fileAttachment2 = new Image();
cell2Row1.Paragraphs.Add( fileAttachment2 );
fileAttachment2.ImageInfo.ImageFileType = ImageFileType.Jpeg;
fileAttachment2.ImageInfo.File = @“F:\tfs\Entwicklung\bin\x86\Debug\DemoImage”;

pdf1.Save( args[1] );
}

The result is exaclty what I want, but it seems to me that this is not your expected result. So I’m afraid of a future bug-fix.
Could you please tell me or how to reach that correctly.
Regards
Gerd

Hi Gerd,


Thanks for your inquiry. You are using old generator(Aspose.Pdf.Generator) for your requirement and It will be obsolete in near future. Moreover along with enhancement in new generator (Aspose.Pdf) wer are fixing bug of old generators in new generator only.

In reference to your requirements in new generator, I am afraid currently file attachment can not be add in a document without its rectangle, so we have logged an enhancement ticket PDFNEWNET-38110 for further investigation and resolution. However you can achieve your requirements in steps. Initially add images and attachments into a PDF document, later find image dimensions and link it to already attached attachment. Please check following sample code snippet for the purpose.

Aspose.Pdf.Document pdf = new Document();<o:p></o:p>

Page sec = pdf.Pages.Add();

Aspose.Pdf.Table table1 = new Aspose.Pdf.Table();

sec.Paragraphs.Add(table1);

table1.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.Box, Aspose.Pdf.Color.LightSkyBlue);

//table1.ColumnAdjustment = ColumnAdjustment.AutoFitToContent;

table1.ColumnWidths = "50";

Aspose.Pdf.Row row1 = table1.Rows.Add();

Aspose.Pdf.Cell cell1Row1 = row1.Cells.Add();

Aspose.Pdf.Image fileAttachment2 = new Aspose.Pdf.Image();

cell1Row1.Paragraphs.Add(fileAttachment2);

fileAttachment2.File = myDir+"barcode.jpg";

// Setup new file to be added as attachment

FileSpecification fileSpecification = new FileSpecification(myDir+"barcode.jpg", "barcode image");

// Add attachment to document's attachment collection

pdf.EmbeddedFiles.Add(fileSpecification);

// Save new output

MemoryStream ms=new MemoryStream();

pdf.Save(ms);

pdf = new Document(ms);

ImagePlacementAbsorber abs = new ImagePlacementAbsorber();

// Load the contents of first page

pdf.Pages.Accept(abs);

foreach (ImagePlacement imagePlacement in abs.ImagePlacements)

{

Aspose.Pdf.Rectangle rec1 = imagePlacement.Rectangle;

// Create attachment annoation

FileAttachmentAnnotation fileAttachment = new FileAttachmentAnnotation(imagePlacement.Page, rec1, pdf.EmbeddedFiles[1]);

fileAttachment.Opacity = .001;

// Add attachment to first page of PDF

imagePlacement.Page.Annotations.Add(fileAttachment);

// Set title for attachment

fileAttachment.Title = "Attached file";

}

pdf.Save(myDir+"NewGenerator_out.pdf");

Please feel free to contact us for any further assistance.


Best Regards,